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 を使用します。リアルタイムでデータを更新する方法を誰か教えてもらえますか? 例えばボードを裏返すとデータが変わる?