mpu6050采集加速度转换角度的程序?求大神注释啊
时间:10-02
整理:3721RD
点击:
#include "uart.h"
sbit Key1=P3^6; //采样时间
sbit Key2=P3^3; //开启关闭
#define uchar unsigned char
#define uint unsigned int
uchar ge,shi,bai,qian,wan; //显示变量
uchar Flag;
uchar angle_flag=0; //变量
void conversion(uint temp_data);
int angle_int;
int dis_data; //变量
unsigned char dis[4];
unsigned int Cycount=0;
unsigned int JSms=0;
unsigned int mscnt=0;
unsigned char cjflag=0;
//-----------------------------------
//*********************************************************
void conversion(uint temp_data)
{
unsigned char aa,bb,cc;
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
aa=temp_data/1000;
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
bb=temp_data/100;
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
cc=temp_data/10;
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
ge=temp_data+0x30;
angle_int=aa*100+bb*10+cc;
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x0DC;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void KeyScan(void) //按键扫描
{
if(Key1 == 0) //开启停止
{
DelayMs(10); //消抖
if(Key1 == 0)
{
Flag++;
if(Flag>1)
Flag=0;
}
while(Key1 == 0);//等待键松开
}
if(Key2 == 0) //采样时间
{
DelayMs(10); //消抖
if(Key2 == 0)
{
Cycount++;
if(Cycount>2)
Cycount=0;
if(Cycount==0)
{
LCD_Write_String(0,0,"caiyang: 10ms");
mscnt=0;
}
if(Cycount==1)
{
LCD_Write_String(0,0,"caiyang: 100ms");
mscnt=10;
}
if(Cycount==2)
{
LCD_Write_String(0,0,"caiyang:1000ms");
mscnt=100;
}
}
while(Key2 == 0);//等待键松开
}
}
//*********************************************************
//******主程序********
//*********************************************************
void main()
{
int K,Q,X,Temp;
float Roll,Strtemp;
long Ksum=0,Qsum=0;
unsigned char kcnt=0,qcnt=0;
Flag=0;
LCD_Init(); //液晶初始化ADXL345
InitMPU6050(); //初始化ADXL345
LCD_Write_String(0,0,"caiyang: 10ms");
LCD_Write_String(0,1,"angle : ");
InitTimer0();
while(1) //循环
{
KeyScan();
if(Flag==0)
{
if(cjflag==1)
{
X=GetData(ACCEL_XOUT_H);
K=GetData(ACCEL_ZOUT_H);
Q=GetData(ACCEL_YOUT_H);
Ksum=Ksum+K;
Qsum=Qsum+Q;
kcnt++;
if(kcnt>=10)
{
K=Ksum/kcnt;
Q=Qsum/kcnt;
kcnt=0;
Ksum=0;
Qsum=0;
if(Q<0)
{
Q=-Q;
if(K<0)
{
K=-K;
Roll=(float)(90+((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
else
{
Roll=(float)(90-((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
}
else
{
if(K<0)
{
K=-K;
Roll=360-(float)(90+((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
else
{
Roll=360-(float)(90-((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
}
Roll=360-Roll;
//倾角显示与处理
if(Roll>=0)
{
Strtemp=Roll;
LCD_Write_Char(8,1,' ');
}
if(Roll<0)
{
Strtemp=-Roll;
LCD_Write_Char(8,1,'-');
}
Temp=(int)(Strtemp*10);
LCD_Write_Char(9,1,Temp/1000+'0');
LCD_Write_Char(10,1,Temp%1000/100+'0');
LCD_Write_Char(11,1,Temp%100/10+'0');
LCD_Write_Char(12,1,'.');
LCD_Write_Char(13,1,Temp%10+'0');
cjflag=0;
}
}
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x0DC;
TL0 = 0x00;
JSms++;
if(JSms>=mscnt)
{
JSms=0;
cjflag=1;
}
}
sbit Key1=P3^6; //采样时间
sbit Key2=P3^3; //开启关闭
#define uchar unsigned char
#define uint unsigned int
uchar ge,shi,bai,qian,wan; //显示变量
uchar Flag;
uchar angle_flag=0; //变量
void conversion(uint temp_data);
int angle_int;
int dis_data; //变量
unsigned char dis[4];
unsigned int Cycount=0;
unsigned int JSms=0;
unsigned int mscnt=0;
unsigned char cjflag=0;
//-----------------------------------
//*********************************************************
void conversion(uint temp_data)
{
unsigned char aa,bb,cc;
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
aa=temp_data/1000;
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
bb=temp_data/100;
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
cc=temp_data/10;
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
ge=temp_data+0x30;
angle_int=aa*100+bb*10+cc;
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x0DC;
TL0 = 0x00;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void KeyScan(void) //按键扫描
{
if(Key1 == 0) //开启停止
{
DelayMs(10); //消抖
if(Key1 == 0)
{
Flag++;
if(Flag>1)
Flag=0;
}
while(Key1 == 0);//等待键松开
}
if(Key2 == 0) //采样时间
{
DelayMs(10); //消抖
if(Key2 == 0)
{
Cycount++;
if(Cycount>2)
Cycount=0;
if(Cycount==0)
{
LCD_Write_String(0,0,"caiyang: 10ms");
mscnt=0;
}
if(Cycount==1)
{
LCD_Write_String(0,0,"caiyang: 100ms");
mscnt=10;
}
if(Cycount==2)
{
LCD_Write_String(0,0,"caiyang:1000ms");
mscnt=100;
}
}
while(Key2 == 0);//等待键松开
}
}
//*********************************************************
//******主程序********
//*********************************************************
void main()
{
int K,Q,X,Temp;
float Roll,Strtemp;
long Ksum=0,Qsum=0;
unsigned char kcnt=0,qcnt=0;
Flag=0;
LCD_Init(); //液晶初始化ADXL345
InitMPU6050(); //初始化ADXL345
LCD_Write_String(0,0,"caiyang: 10ms");
LCD_Write_String(0,1,"angle : ");
InitTimer0();
while(1) //循环
{
KeyScan();
if(Flag==0)
{
if(cjflag==1)
{
X=GetData(ACCEL_XOUT_H);
K=GetData(ACCEL_ZOUT_H);
Q=GetData(ACCEL_YOUT_H);
Ksum=Ksum+K;
Qsum=Qsum+Q;
kcnt++;
if(kcnt>=10)
{
K=Ksum/kcnt;
Q=Qsum/kcnt;
kcnt=0;
Ksum=0;
Qsum=0;
if(Q<0)
{
Q=-Q;
if(K<0)
{
K=-K;
Roll=(float)(90+((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
else
{
Roll=(float)(90-((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
}
else
{
if(K<0)
{
K=-K;
Roll=360-(float)(90+((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
else
{
Roll=360-(float)(90-((atan2(K,Q)*180)/3.1416)); //X轴角度值
}
}
Roll=360-Roll;
//倾角显示与处理
if(Roll>=0)
{
Strtemp=Roll;
LCD_Write_Char(8,1,' ');
}
if(Roll<0)
{
Strtemp=-Roll;
LCD_Write_Char(8,1,'-');
}
Temp=(int)(Strtemp*10);
LCD_Write_Char(9,1,Temp/1000+'0');
LCD_Write_Char(10,1,Temp%1000/100+'0');
LCD_Write_Char(11,1,Temp%100/10+'0');
LCD_Write_Char(12,1,'.');
LCD_Write_Char(13,1,Temp%10+'0');
cjflag=0;
}
}
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x0DC;
TL0 = 0x00;
JSms++;
if(JSms>=mscnt)
{
JSms=0;
cjflag=1;
}
}
程序好使 就是自己看不懂,求注释
效果就是按键1 启动停止 按键2 设置采样时间(10ms 100ms 1000ms)。
主要不明白中断怎么产生10ms 100ms 这样的采样