jiasen110 jiasen110
?
关注数: 15 粉丝数: 118 发帖数: 1,752 关注贴吧数: 33
求大神帮忙看一下设置哪里出错了,为什么串口不中断呢? 51单片机,串口接收mpu6050陀螺仪的数据,波特率9600 #include <reg52.h> #define uchar unsigned char #define uint unsigned int float a[3],w[3],angle[3],T; unsigned char Re_buf[11],counter; unsigned char sign; unsigned char sign,counter=0; //延迟 void Delay1ms(uint i) { unsigned int j; for(;i>0;i--) for(j=0;j<125;j++); } //初始化 void UartInit(void) { TMOD = 0x20; // 定时器T1为方式2 PCON = 0x00; SCON = 0xd0; //串口方式3 TH1 = 0xfd; //波特率 9600 TL1 = 0xfd; TR1 = 1;//开启定时器1 ES = 1;//串口中断允许 EA = 1;//总中断允许 REN=1;//使能接收 } //中断 void UartReceive() interrupt 4 { if(RI) { RI=0; Re_buf[counter]=SBUF; if(counter==0&&Re_buf[0]!=0x55) return; //第0号数据不是帧头 counter++; if(counter==11) //接收到11个数据 { counter=0; //重新赋值,准备下一帧数据的接收 sign=1; } } } void main() { UartInit(); Delay1ms(3000); //延迟三秒,陀螺仪自动校准 P2=0x00 ;//将p2置于低电平,led熄灭 while(1) { if(sign) { sign=0; if(Re_buf[0]==0x55) //检查帧头 { switch(Re_buf [1]) { case 0x51: //计算加速度 a[0] = ((short)(Re_buf [3]<<8| Re_buf [2]))/32768.0*16; a[1] = ((short)(Re_buf [5]<<8| Re_buf [4]))/32768.0*16; a[2] = ((short)(Re_buf [7]<<8| Re_buf [6]))/32768.0*16; T = ((short)(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25; //温度 break; case 0x52: //计算角速度 w[0] = ((short)(Re_buf [3]<<8| Re_buf [2]))/32768.0*2000; w[1] = ((short)(Re_buf [5]<<8| Re_buf [4]))/32768.0*2000; w[2] = ((short)(Re_buf [7]<<8| Re_buf [6]))/32768.0*2000; T = ((short)(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;//温度 break; case 0x53: //计算角度 angle[0] = ((short)(Re_buf [3]<<8| Re_buf [2]))/32768.0*180; angle[1] = ((short)(Re_buf [5]<<8| Re_buf [4]))/32768.0*180; angle[2] = ((short)(Re_buf [7]<<8| Re_buf [6]))/32768.0*180; T = ((short)(Re_buf [9]<<8| Re_buf [8]))/340.0+36.25;//温度 break; } } if( angle[0]>=5|| angle[1]>=5|| angle[2]>=5 )//如果姿态变化超过5°,则点亮led { while(1) P2=0xff;//驱动LED } } //if(sign) } //while } //main
1 下一页