/*********************************************************************************************/ /* 我的思路是這樣的:一個(gè)51的MCU+一個(gè)4位數(shù)碼管+一個(gè)74LS138譯碼器+12M晶振.完成一個(gè)計(jì)秒和分的 */ /*計(jì)時(shí)器。設(shè)置兩個(gè)按鍵:開始和復(fù)位其中開始鍵可以開始計(jì)時(shí),又可以暫停計(jì)時(shí),再按一次,又接著計(jì) */ /*時(shí)。反正就是開始—暫停—開始—暫停—接著計(jì)時(shí),只要不按復(fù)位。我想讓它初始畫面只顯示一個(gè)0,先*/ /*計(jì)時(shí)秒,到10秒以上在選通第三位數(shù)碼管,到1分鐘以上在選通第二位數(shù)碼管,(其中第二位數(shù)碼管的H */ /*段必須亮,作用是區(qū)分——分鐘和秒鐘),到10分鐘以上在選通第一位數(shù)碼管。就像這樣顯示順序 */ /* 0 ——0秒 */ /* 10 ——10秒 */ /* 19 ——19秒 */ /* 1.30 ——1分30秒 */ /* 9.00 ——9分0秒 */ /* 10.00 ——10分0秒 */ /* 60.59 ——60分59秒 */ /*循環(huán)計(jì)時(shí)。復(fù)位鍵清0 ,重新按開始鍵計(jì)時(shí)開始。復(fù)位鍵我用硬件復(fù)位電路了. */ /*********************************************************************************************/ #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};//帶點(diǎn)的段選:0123456789 uchar code weima[]= {0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7}; //我的位選:我用前4個(gè)數(shù)碼管 void display(uchar); //顯示聲明 uchar cnt,sec,min,flag; //變量聲明 uint num; //======================= void delay(uint i) //毫秒延時(shí)函數(shù) { uint a,b; for(a=i;a>0;a--) for(b=121;b>0;b--); } //======================= void gate() //中斷控制函數(shù) { TMOD=0x01; //T0定時(shí)方式 TH0=(65536-50000)/256; //初值50毫秒 TL0=(65536-50000)%256; EA=1; //開總中斷 ET0=1; //T0中斷允許位 TR0=1; //開定時(shí)器 IT0=0; //外中斷T0 EX0=1; //外中斷允許位 PX0=1; //外中斷高優(yōu)先級(jí) } //======================= void main() //主函數(shù) { char x; gate(); //中斷控制 while(1) //這里我寫的很笨方法,可想了好久, { 算法不簡(jiǎn)練,希望朋友們不吝賜教. if(cnt==20){sec++;num++;cnt=0;} //秒增加 if(sec==60){min++;sec=0;} //分增加 if(min==60)min=0; //封頂一個(gè)小時(shí) if(num<10){x=0;} //先讓秒個(gè)位亮 if(num>=10){x=1;} //再讓秒個(gè)十位亮 if(num>=60){x=2;} //再讓分個(gè)位亮和秒位亮 if(num>=600){x=3;} //再4位都亮 if(num==3600){num=0;} //一個(gè)小時(shí)封頂 display(x); } } //======================== void time0() interrupt 1 //計(jì)時(shí)器0中斷 { TH0=(65536-50000)/256; //初值 TL0=(65536-50000)%256; cnt++; //加數(shù) } //======================== void ie0() interrupt 0 //外中斷0 { delay(10); if(!P32) TR0=~TR0; //停止計(jì)時(shí) flag=TR0; //開關(guān) while(!P32) { switch(flag) { case 0: P2=duma[10]; //這個(gè)鍵按下去顯示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]; //這個(gè)鍵按下去顯示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) //顯示函數(shù) { switch(x) { case 0:P2=duma[sec%10]; //主要是顯示個(gè)位 P1=weima[3]; delay(4); break; case 1:P2=duma[sec%10]; //顯示十位和個(gè)位 P1=weima[3]; delay(4); P2=duma[sec/10]; P1=weima[2]; delay(4); break; case 2:P2=duma[sec%10]; //顯示百十個(gè)位 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]; //顯示千百十個(gè)位 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; } }






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