|
寫了一個(gè)程序需要51單片機(jī)檢測(cè)I/O口的上升沿,然后打開定時(shí)器,最后將計(jì)時(shí)的結(jié)果顯示在LCD12864的程序,然后經(jīng)過除1000000的Ctime2一直為0。經(jīng)過測(cè)試,檢測(cè)I/O口,12864顯示,以及浮點(diǎn)數(shù)轉(zhuǎn)換為字符數(shù)組(直接給一個(gè)數(shù)賦值,可以正常轉(zhuǎn)換)的程序均可以正常運(yùn)行,現(xiàn)在就排查到應(yīng)該是定時(shí)器中斷的問題,然后用中間變量time2和time1顯示,發(fā)現(xiàn)結(jié)果均不超過65536,就想可能是中斷的問題,希望大神能看看
單片機(jī)源程序如下:
- #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;
- //定時(shí)器123的值
- unsigned int time1;
- unsigned int time2;
- unsigned int time3;
- //溢出次數(shù)
- unsigned int overFlow1;
- unsigned int overFlow2;
- unsigned int overFlow3;
- //數(shù)據(jù)
- 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顯示浮點(diǎn)數(shù)
- void float_to_str(char *str,double num);
- void main(void)
- {
- //定時(shí)器初始化
- initTimer();
- EA=1;
- ET0=1;
- ET1=1;
- input1=1;
- input2=1;
- input3=1;
- input4=1;
- overFlow1=0;
- overFlow2=0;
- overFlow3=0;
- //檢測(cè)
- 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");
- */
- //檢測(cè)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;
- }
- }
- //檢測(cè)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;
- //轉(zhuǎn)換為秒
- 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);//測(cè)試中間變量
- 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;
- }
復(fù)制代碼
|
|