2014-02-19 202 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(" | "); 
    } 
}