在I2C的基础上,添加ADXL345的模块,然后在添加打印模块, 可以调试出ADXL345的值
单片机源程序如下:
- # include "sys.h"
- # include "delay.h"
- # include "printf.h"
- # include "adxl345.h"
- # include "usart.h"
- # include "led.h"
- # include "key.h"
- # include "math.h"
- void Delay (u32 i)
- {
- while(i--);
- }
-
- int main(void)
- {
- u8 key;
- u8 t=0;
- short x,y,z;
- short angx,angy,angz;
- delay_init(); //延时初始化
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //按键初始化
- printf_init();
- I2C_INIT();
- while(ADXL345_Init())
- {
- printf(" 加速度传感器错误!\r\n");
- delay_ms(100);
- }
- while(1)
- {
- if(t%10==0)//每100ms读取一次
- {
- //得到X,Y,Z轴的加速度值(原始值)
- ADX_GetXYZ_Value(&x, &y,&z); //读取X,Y,Z三个方向的加速度值
-
- printf("x:%d \n",x); //显示加速度原始值
- printf("y: %d \n",y);
- printf("z: %d \r\n",z);
-
-
- //得到角度值,并显示
- angx=ADXL345_Get_Angle(x,y,z,1);
- angy=ADXL345_Get_Angle(x,y,z,2);
- angz=ADXL345_Get_Angle(x,y,z,0);
- printf(" 角度x:%d\r\n",angx); //显示角度值
- printf(" 角度y:%d\r\n",angy);
- printf(" 角度z:%d\r\n",angz);
- Delay(6000000);
- }
- key=KEY_Scan(0);
- if(key==KEY_UP)
- {
- LED1=0;//绿灯亮,提示校准中
- ADXL345_AUTO_Adjust(&x, &y, &z);//自动校准
- LED1=1;//绿灯灭,提示校准完成
- }
- delay_ms(10);
- t++;
- if(t==20)
- {
- t=0;
- LED0=!LED0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
ADXL345打印.rar
(286.76 KB, 下载次数: 251)
|