0

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

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 来读取这些数据。有人可以说如何实时更新数据吗?例如,如果我把板子翻过来,数据会改变吗?

4

2 回答 2

0

我们可以开始到处搜索错误。在您的问题中,有些事情让我感到困惑。

  1. 您是说您正在使用uart2但正在启动spi
  2. 您正在使用已经提供LIS3DSH_Get_Y_Out等的库LIS3DSH_lib。那么为什么要使用 getreg?就像使用 x 坐标一样。
  3. 在我发现 spi init 的库中是在LIS3DSH_Init()中创建的。所以把你自己的 spi init扔掉
  4. 确保您有 LIS3DSH,然后不要使用 LIS302DL_OUT_Z。新发现使用LIS302DL。如果你有一个 LIS302DL 使用这个 lib。我可以保证这个是有效的。

解决方案:尝试使用这个 lib并丢弃所有 get reg 和 set reg 代码以及您的其他 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(" | ");
    }
}
于 2014-02-19T14:23:44.423 回答
0

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

于 2014-02-21T13:41:57.083 回答