写了一个程序需要51单片机检测I/O口的上升沿,然后打开定时器,最后将计时的结果显示在LCD12864的程序,然后经过除1000000的Ctime2一直为0。经过测试,检测I/O口,12864显示,以及浮点数转换为字符数组(直接给一个数赋值,可以正常转换)的程序均可以正常运行,现在就排查到应该是定时器中断的问题,然后用中间变量time2和time1显示,发现结果均不超过65536,就想可能是中断的问题,希望大神能看看
单片机源程序如下:
- #include<reg52.h>
- #include<math.h>
- #include <string.h>
- #include<stdlib.h>
- #include<intrins.h>
- #include<stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- //输入I/O口
- sbit input1=P1^0
- sbit input2=P1^1
- sbit input3=P1^2
- sbit input4=P1^3
- sbit test1=P1^4;
- sbit res=P3^2;//S4
- //之前电平
- bit oldBit1;
- bit oldBit2;
- bit oldBit3;
- bit oldBit4;
- bit newBit1;
- bit newBit2;
- bit newBit3;
- bit newBit4;
- //偏左,偏右
- bit bias12;
- bit bias34;
- //定时器123的值
- unsigned int time1;
- unsigned int time2;
- unsigned int time3;
- //溢出次数
- unsigned int overFlow1;
- unsigned int overFlow2;
- unsigned int overFlow3;
- //数据
- float velocity=0;
- float enterAngle=0;
- float exitAngle=0;
- unsigned int length=8;
- float width=5.2;
- float Ctime1=0;
- float Ctime2=0;
- float Ctime3=0;
- float c=1000000;
- unsigned char idata enterAngle1[6];
- unsigned char idata exitAngle1[6];
- unsigned char idata velocity1[6];
- unsigned char idata testT[6];
- //声明
- void initTimer(void);
- void Delay(uint MS);
- void LcmInit(void);
- void LcmClearTXT(void);
- void PutStr(uchar row,uchar col,uchar*puts);//LCD显示字符串
- void PutFloat(uchar row,uchar col,uchar*puts);//LCD显示浮点数
- void float_to_str(char *str,double num);
- void main(void)
- {
- //定时器初始化
- initTimer();
- EA=1;
- ET0=1;
- ET1=1;
- input1=1;
- input2=1;
- input3=1;
- input4=1;
- overFlow1=0;
- overFlow2=0;
- overFlow3=0;
- //检测
- oldBit1=input1;
- oldBit2=input2;
- oldBit3=input3;
- oldBit4=input4;
- test1=0;
- //LCD
- Delay(100);
- LcmInit();
- LcmClearTXT();
- /*
- PutStr(0,0,"èë½Ç£o");
- PutStr(1,0,"3ö½Ç£o");
- PutStr(2,0,"Ëù¶è£o");
- PutStr(0,7,"¡ã");
- PutStr(1,7,"¡ã");
- PutStr(2,6,"cm/s");
- */
- //检测1
- while(1)
- {
- newBit1=input1;
- newBit2=input2;
- if((newBit1==1)&&(oldBit1==0))//左偏
- {
- Delay(1);
- TR0=1;
- Delay(1);
- TR1=1;
- bias12=1;
- break;
- }
- if((newBit2==1)&&(oldBit2==0))//右偏
- {
- Delay(1);
- TR0=1;
- Delay(1);
- TR1=1;
- bias12=0;
- break;
- }
- oldBit1=newBit1;
- oldBit2=newBit2;
- }
- if(bias12==1)
- {
- while(1)
- {
- newBit2=input2;
- if((newBit2==1)&&(oldBit2==0))
- {
- Delay(1);
- TR0=0;
- break;
- }
- oldBit2=newBit2;
- }
- }
-
- if(bias12==0)
- {
- while(1)
- {
- newBit1=input1;
- if((newBit1==1)&&(oldBit1==0))
- {
- Delay(1);
- TR0=0;
- break;
- }
- oldBit1=newBit1;
- }
- }
- //检测2
- time1=(TH0<<8)+TL0+(overFlow1<<16);
- overFlow3=0;
- TH0=0;
- TL0=0;
- while(1)
- {
- newBit3=input3;
- newBit4=input4;
- if((newBit3==1)&&(oldBit3==0))//右偏
- {
- Delay(1);
- TR0=1;
- Delay(1);
- TR1=0;
- bias34=1;
- break;
- } //óòÆ«
- if((newBit4==1)&&(oldBit4==0))
- {
- Delay(1);
- TR0=1;
- Delay(1);
- TR1=0;
- bias34=0;
- break;
- }
- oldBit3=newBit3;
- oldBit4=newBit4;
- }
-
- time2=20+(TH1*256)+TL1+overFlow2*(65536);
-
- if(bias34==1)
- {
- while(1)
- {
- newBit4=input4;
- if((newBit4==1)&&(oldBit4==0))
- {
- Delay(1);
- TR0=0;
- break;
- }
- oldBit4=newBit4;
- }
- }
-
- if(bias34==0)
- {
- while(1)
- {
- newBit3=input3;
- if((newBit3==1)&&(oldBit3==0))
- {
- Delay(1);
- TR0=0;
- break;
- }
- oldBit3=newBit3;
- }
- }
- time3=TH0<<8+TL0+overFlow3<<16;
- //转换为秒
- Ctime1=time1/c;//c为1000000
- Ctime2=time2/c;
- Ctime3=time3/c;
- /* velocity=length/Ctime2;
- enterAngle=atan((length*Ctime1)/(Ctime2*width));
- exitAngle=atan((length*Ctime3)/(Ctime2*width));//3¤¶èμ¥λcm,ê±¼äμ¥λs
- */
- //LCD2
- if(bias12==0)//óòÆ«
- {
- PutStr(3,0,"èë¸ËóòÆ«");
- }
- else if(bias12==1)//×óÆ«
- {
- PutStr(3,0,"èë¸Ë×óÆ«");
- }
-
- if(bias34==0)//óòÆ«
- {
- PutStr(3,4,"3ö¸ËóòÆ«");
- }
- else if(bias34==1)//×óÆ«
- {
- PutStr(3,4,"3ö¸Ë×óÆ«");
- }
- //×a»»
- /* float_to_str(enterAngle1,enterAngle);
- float_to_str(exitAngle1,exitAngle);
- float_to_str(velocity1,velocity);
- PutStr(0,0,enterAngle1);
- PutStr(1,0,exitAngle1);
- PutStr(2,0,velocity1); */
- float_to_str(testT,Ctime2);//测试中间变量
- PutStr(0,0,testT);
- while(res==1);
- }
- //oˉêy
- void initTimer(void)
- {
- TMOD=0x11;
- TH0=0;
- TL0=0;
- TH1=0;
- TL1=20;
- }
- //ÖD¶Ï
- void Timer0Int() interrupt 1
- {
- overFlow1++;
- overFlow3++;
- TH0=0;
- TL0=0;
- }
- void Timer1Int() interrupt 3
- {
- overFlow2++;
- TH1=0;
- TL1=0;
- }
复制代码
|