2014-02-19 150 views
1

我需要从STM32F4发现中的加速度计LIS3DSH中读取一些数据。我有这个主代码:STM32F4加速度计

uint8_t writeData(uint8_t data) { 

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) 
     ; 
    SPI_I2S_SendData(SPI1, data); 

    while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) 
     ; 
    return SPI_I2S_ReceiveData(SPI1); 
} 

void setReg(uint8_t address, uint8_t value) { 
    GPIO_ResetBits(GPIOE,GPIO_Pin_3); 
    writeData(address); 
    writeData(value); 
    GPIO_SetBits(GPIOE,GPIO_Pin_3); 
} 

uint8_t getReg(uint8_t address) { 
    uint8_t data=0; 
    address|=(1<<7); 
    GPIO_ResetBits(GPIOE,GPIO_Pin_3); 
    writeData(address); 
    data = writeData(0x00); 
    GPIO_SetBits(GPIOE,GPIO_Pin_3); 
    return data; 
} 

int main(void) 
{ 
    char str[4]; 

    usart_init(); 
    spi_init(); 

    // Turn on accelerometer 
    //setReg(LIS302DL_CTRL_REG1, (1<<PD_CTRL_REG1)); 
    LIS3DSH_Init(); 

    // Read data from three registers 
    // and write it to UART 
    while(1) 
    { 
     delay(); 

     itoa((int8_t) LIS3DSH_Get_X_Out(1),&str); 
     send_str(&str); 
     send_str(":"); 
     itoa((int8_t) getReg(LIS302DL_OUT_Y),&str); 
     send_str(&str); 
     send_str(":"); 
     itoa((int8_t) getReg(LIS302DL_OUT_Z),&str); 
     send_str(&str); 
     send_str(" | "); 
    } 
} 

但它只收到第一个值。例如:

5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128| 
5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128|5:32:128| 
5:32:128|5:32:128| 

我使用USART2来读取这些数据。有人可以说如何实时更新数据吗?例如,如果我翻板,数据会改变?

+0

也许你在过分的价值... – KernelPanic

+0

@MarkoFrelih不,函数getReg返回这个不变的值 – Viodentia

+1

@MarkoFrelih我发现了这个问题。传感器的电源未打开。它在寄存器设置中解决。 – Viodentia

回答

0

我发现了它。传感器的电源未打开。它在寄存器设置(0x2)中解决。谢谢大家的帮助。

0

我们可以在此处开始搜索错误。在你的问题中有些东西令我困惑。

  1. 你是说你正在使用UART2但initilating的SPI
  2. 您正在使用的图书馆LIS3DSH_lib已经提供LIS3DSH_Get_Y_Out等等。那么,为什么你使用getreg?就像用x坐标一样。
  3. 在lib中,我发现spi init是在LIS3DSH_Init()中生成的。所以把你自己的spi init 中拿出来。
  4. 确保你有一个LIS3DSH,然后不要使用LIS302DL_OUT_Z宏。新的发现使用LIS302DL。如果你有一个LIS302DL使用this lib。我可以保证这个工作。

解决方案:尝试使用This lib并丢弃所有获取注册表并设置注册码和其他spi_init。

类似的东西:

#include LIS3DSH.h 
int main(void) 
{ 
    char str[4]; 

    usart_init(); 
    LIS3DSH_Init(); 

    while(1) 
    { 
     delay(); 

     itoa((int8_t) LIS3DSH_Get_X_Out(1),&str); 
     send_str(&str); 
     send_str(":"); 
     itoa((int8_t) LIS3DSH_Get_Y_Out(1),&str); 
     send_str(&str); 
     send_str(":"); 
     itoa((int8_t) LIS3DSH_Get_Z_Out(1),&str); 
     send_str(&str); 
     send_str(" | "); 
    } 
}