سؤال

I need to read some data from accelerometer LIS3DSH in STM32F4 Discovery. I have this main code:

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(" | ");
    }
}

But it recieves only first value. For example:

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|

I use USART2 for reading this data. Can somebody say how to update data in real time? For example if I turn over the board the data will change?

هل كانت مفيدة؟

المحلول 2

I found it. The power of sensor was not turned on. It solves in register settings (0x2). Thanks everybody for help.

نصائح أخرى

We could start searching the error here everywhere. There are some things that are confusing me tho in your question.

  1. You are saying you are using uart2 but are initilating the spi.
  2. You are using a library LIS3DSH_lib which already provides LIS3DSH_Get_Y_Out etc.. So why do you use getreg??? Just do it like with the x coordinate.
  3. In the lib that I found the spi init is made in the LIS3DSH_Init(). So throw your own spi init out of there.
  4. Make sure you have a LIS3DSH and then dont use LIS302DL_OUT_Z macros. The new discoveries use the LIS302DL. In case you have a LIS302DL use this lib. I can assure that this one works.

Solution: Try to use This lib and throw out all get reg and set reg code and your other spi_init.

Something like that:

#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(" | ");
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top