簡體   English   中英

STM32F4 加速度計

[英]STM32F4 accelerometer

我需要從 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 來讀取這些數據。 有人能說一下如何實時更新數據嗎? 例如,如果我翻轉電路板,數據會改變嗎?

我們可以開始到處搜索錯誤。 在您的問題中,有些事情讓我感到困惑。

  1. 您是說您正在使用uart2但正在啟動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,請使用此 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(" | ");
    }
}

我找到了。 傳感器的電源沒有打開。 它在寄存器設置 (0x2) 中解決。 謝謝大家的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM