基于51单片机地篮球24秒倒计时器.docxVIP

  • 14
  • 0
  • 约1.22千字
  • 约 3页
  • 2020-07-18 发布于江苏
  • 举报
基于 51 单片机的篮球 24 秒倒计时器 #include reg51.h #define uchar unsigned char sbit wei0=P1^0; sbit wei1=P1^1; sbit wei2=P1^2; sbit gnd=P2^5; sbit key2=P2^0; uchar count;//用于记录发生了多少次中断的计数器 uchar sec=240; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //注:用 code 是把后面的数据存在程序存贮器中,如果不用 code,数据就会存在随机存贮 器中。 void delayms(int x) { int y,z; for(y=x;y0;y--) for(z=110;z0;z--); } void display(int dat) { uchar bai,shi,ge; bai=dat/100; shi=dat%100/10; ge=dat%10; wei0=0; P0=table[bai]; delayms(1); wei0=1; wei1=0; P0=table[shi]0x7f; delayms(1); wei1=1; wei2=0; P0=table[ge]; delayms(1); wei2=1; P0=0xff; // 这一句和下一句是为了降低数码管亮度,同学们不必写。 delayms(20); } void keyscan() { if(key2==0) { delayms(5);// 延时消抖 while(!key2);// 松手检测 TR0=~TR0; } } void main() { gnd=0; TMOD=0x01;// 启用定时器 0 的工作方式 1 TH0=(65536-50000)/256;// 装初值 TL0=(65536-50000)%256;// 装初值 EA=1;// 打开总中断 ET0=1;// 打开定时器 0; TR0=0;// 启动定时器 0; while(1) { keyscan(); /*if(count==2) { count=0; sec--; if(sec==0) sec=240; } */ display(sec); } } void timer0 () interrupt 1// 定时器 0 的中断优先等级是 1 { TH0=(65536-50000)/256;// 重新装初值 TL0=(65536-50000)%256;// 重新装初值 count++; if(count==2) { count=0; sec--; if(sec==0) sec=240; } // display(sec); }

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档