資源描述:
《定時(shí)器中斷設(shè)計(jì)秒鐘》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、/*用定時(shí)器設(shè)計(jì)一個(gè)秒表00-59;開始顯示00按下key1加1,key2減1,key3清零key4啟動暫停按鈕*/#include#include#defineucharunsignedchar#defineuintunsignedintsbitkey1=P3^0;sbitkey2=P3^1;sbitkey3=P1^5;sbitkey4=P1^6;sbitwe0=P3^4;sbitwe1=P3^5;sbitwe2=P3^6;sbitwe3=P3^7;sbitled0=P2^0;sbitled1=P2^1;sbitled2=P2^2;sbitled
2、3=P2^3;sbitled4=P2^4;sbitled5=P2^5;sbitled6=P2^6;sbitled7=P2^7;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x07f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0123456789ABCDEFucharm,n;charnum;voiddelayms(uintx){uinti,j;for(i=x;i>0;i--)for(j=240;j>0;j--);}voidinit(){num=0;TMOD=0x01;//定時(shí)器0工作方
3、式1TH0=(65536-50000)/256;//裝入初值50msTL0=(65536-50000)%256;EA=1;//開總中斷ET0=1;//開定時(shí)器0中斷//TR0=1;//啟動定時(shí)器0中斷}voiddisplay(){ucharshi,ge;shi=num/10;ge=num%10;we3=0;P0=0xff;P0=~table[shi];delayms(1);we3=1;we2=0;P0=0xff;P0=~table[ge];delayms(1);we2=1;}keyscan(){if(key1==0){delayms(5);if(key1==0){led3=0;num++
4、;if(num==60)num=0;while(!key1);}led3=1;}if(key2==0){delayms(5);if(key2==0){led2=0;num--;if(num==-1)num=59;}while(!key2);led2=1;}if(key3==0){delayms(5);if(key3==0){led1=0;num=0;}while(!key3);led1=1;}if(key4==0){delayms(5);if(key4==0){led0=0;TR0=~TR0;}}while(!key4);led0=1;}voidmain(){init();while(1
5、){keyscan();display();}}voidtimer0()interrupt1{TH0=(65536-50000)/256;//裝入初值50msTL0=(65536-50000)%256;n++;if(n==20){n=0;num++;if(num==60){num=0;}}}