/*********************************************************************************************/ /* 我的思路是這樣的:一個51的MCU+一個4位數碼管+一個74LS138譯碼器+12M晶振.完成一個計秒和分的 */ /*計時器。設置兩個按鍵:開始和復位其中開始鍵可以開始計時,又可以暫停計時,再按一次,又接著計 */ /*時。反正就是開始—暫停—開始—暫停—接著計時,只要不按復位。我想讓它初始畫面只顯示一個0,先*/ /*計時秒,到10秒以上在選通第三位數碼管,到1分鐘以上在選通第二位數碼管,(其中第二位數碼管的H */ /*段必須亮,作用是區分——分鐘和秒鐘),到10分鐘以上在選通第一位數碼管。就像這樣顯示順序 */ /* 0 ——0秒 */ /* 10 ——10秒 */ /* 19 ——19秒 */ /* 1.30 ——1分30秒 */ /* 9.00 ——9分0秒 */ /* 10.00 ——10分0秒 */ /* 60.59 ——60分59秒 */ /*循環計時。復位鍵清0 ,重新按開始鍵計時開始。復位鍵我用硬件復位電路了. */ /*********************************************************************************************/ #include<reg52.h> //頭頭 #define uchar unsigned char #define uint unsigned int sbit P32=P3^2; //外中斷0 uchar code duma[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x6d,0x73,0x78,0xdc,0x50};//段選:0123456789sptar uchar code dumapoint[]= {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//帶點的段選:0123456789 uchar code weima[]= {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7}; //我的位選:我用前4個數碼管 void display(uchar); //顯示聲明 uchar cnt,sec,min,flag; //變量聲明 uint num; //======================= void delay(uint i) //毫秒延時函數 { uint a,b; for(a=i;a>0;a--) for(b=121;b>0;b--); } //======================= void gate() //中斷控制函數 { TMOD=0x01; //T0定時方式 TH0=(65536-50000)/256; //初值50毫秒 TL0=(65536-50000)%256; EA=1; //開總中斷 ET0=1; //T0中斷允許位 TR0=1; //開定時器 IT0=0; //外中斷T0 EX0=1; //外中斷允許位 PX0=1; //外中斷高優先級 } //======================= void main() //主函數 { char x; gate(); //中斷控制 while(1) //這里我寫的很笨方法,可想了好久, { 算法不簡練,希望朋友們不吝賜教. if(cnt==20){sec++;num++;cnt=0;} //秒增加 if(sec==60){min++;sec=0;} //分增加 if(min==60)min=0; //封頂一個小時 if(num<10){x=0;} //先讓秒個位亮 if(num>=10){x=1;} //再讓秒個十位亮 if(num>=60){x=2;} //再讓分個位亮和秒位亮 if(num>=600){x=3;} //再4位都亮 if(num==3600){num=0;} //一個小時封頂 display(x); } } //======================== void time0() interrupt 1 //計時器0中斷 { TH0=(65536-50000)/256; //初值 TL0=(65536-50000)%256; cnt++; //加數 } //======================== void ie0() interrupt 0 //外中斷0 { delay(10); if(!P32) TR0=~TR0; //停止計時 flag=TR0; //開關 while(!P32) { switch(flag) { case 0: P2=duma[10]; //這個鍵按下去顯示stop P1=weima[0]; delay(4); P2=duma[12]; P1=weima[1]; delay(4); P2=duma[0]; P1=weima[2]; delay(4); P2=duma[11]; P1=weima[3]; delay(4); break; case 1: P2=duma[10]; //這個鍵按下去顯示start P1=weima[0]; delay(4); P2=duma[12]; P1=weima[1]; delay(4); P2=duma[13]; P1=weima[2]; delay(4); P2=duma[14]; P1=weima[3]; delay(4); break; default:break; } } } //======================== void display(char x) //顯示函數 { switch(x) { case 0:P2=duma[sec%10]; //主要是顯示個位 P1=weima[3]; delay(4); break; case 1:P2=duma[sec%10]; //顯示十位和個位 P1=weima[3]; delay(4); P2=duma[sec/10]; P1=weima[2]; delay(4); break; case 2:P2=duma[sec%10]; //顯示百十個位 P1=weima[3]; delay(4); P2=duma[sec/10]; P1=weima[2]; delay(4); P2=dumapoint[min%10]; P1=weima[1]; delay(4); break; case 3:P2=duma[sec%10]; //顯示千百十個位 P1=weima[3]; delay(4); P2=duma[sec/10]; P1=weima[2]; delay(4); P2=dumapoint[min%10]; P1=weima[1]; delay(4); P2=duma[min/10]; P1=weima[0]; delay(4); break; default:break; } }






接著玩串口通信
[此貼子已經被作者于2009-10-29 12:10:58編輯過]
|