int main(void)
{
uint8_t ReadValue,i;
uint8_t buf[11]={0};
SystemInit(); //系统时钟初始化
/* Configure LED6 */
STM_EVAL_LEDInit(LED6);
Systick_Init();//滴答定时初始化
/* Initiate Blink Speed variable */
BlinkSpeed = 1;
GPIO_Configuration_Init();
USART_Cfig_Init(); //串口初始化
I2C_init(); //I2C 初始化
TIM_INT_Config();
MM7455_Init();
buf[0] = 0x21;
buf[1] = 'W';
buf[2] = 0x01;
buf[3] = 0x00;
buf[4] = 'A';
/* Assume base board in zero-g position when reading first value. */
Axis3_Test(&x, &y, &z);
xoff = 0-x;
yoff = 0-y;
zoff = 0-z;
while(1)
{
Delay(1000);
Axis3_Test(&x, &y, &z); //读取三轴的XYZ的值
x = x+xoff;
y = y+yoff;
z = z+zoff;
buf[5] = x;
buf[6] = y;
buf[7] = z;
for(i=0; i<(11-2);i++)
{
ReadValue ^=buf[i+1];
}
buf[10] = ReadValue;
UART_Send(USART2,buf, 11);
Delay(1000);
}
}
读取I2C芯片的数据函数
COBOL Code
void Axis3_Test(int8_t *x, int8_t *y, int8_t *z)
{
uint8_t Read_buf;
/* wait for ready flag */
while ((getStatus() & ACC_STATUS_DRDY) == 0);
/*
* Have experienced problems reading all registers
* at once. Change to reading them one-by-one.
*/
I2C_Read_one_Byte(&Read_buf, ACC_ADDR_XOUT8, MMA_ADRESS);
*x = (int8_t)Read_buf;
I2C_Read_one_Byte(&Read_buf, ACC_ADDR_YOUT8, MMA_ADRESS);
*y = (int8_t)Read_buf;
I2C_Read_one_Byte(&Read_buf, ACC_ADDR_ZOUT8, MMA_ADRESS);
*z = (int8_t)Read_buf;
}
Copyright © 2004-2024 华清远见教育科技集团 版权所有
京ICP备16055225号-5,京公海网安备11010802025203号