MCU learning https://bbs.21ic.com/?516895 [收藏] [复制] [RSS] 关注生活,自学单片机!

日志

基于定时器的24s倒计时--C51版

已有 766 次阅读2009-11-1 15:10 |个人分类:从生活中学单片机|系统分类:单片机| 定时, C51

原理图未变...


参考代码:


//**********************************************************
//Deion:using mode 1 of timer 0 to delay 50 ms
//Coder:Benjamin Lau
//Time:2009-11-01
//**********************************************************
#include <reg51.h>
typedef unsigned char uchar;
uchar ct_1s = 0;
uchar decimal = 2;
uchar unit = 4;
uchar code sevenseg[10] = {0x0c0,0x0f9,0x0a4,0x0b0,0x99,0x92,0x82,0x0f8,0x80,0x90};
//Timer 0 interrupt service program
void display(void);
void compute(void);
void timer_0(void) interrupt 1 using 1
{
 TH0 = 0X3C;
 TL0 = 0X0B0;
 ct_1s ++;
 
 }
void main(void)
{
 TMOD = 0X01;//TIMER0,MODE 1
 TH0 = 0x3c;
 TL0 = 0X0B0;//the preset number of 50ms
 ET0 = 1;
 EA = 1;//Enable the interrupt
 TR0 = 1;//turn the timer0 on
 while(1)
 {
  compute();
  display();
  }
}
//compute the result of decimal and the unit
void compute(void)
{
 if(ct_1s == 20)
  {
   unit -- ;
   ct_1s = 0;
   }
 if(decimal != 0)
  {
   if(unit == 255)
    {
     decimal --;
     unit = 9;
     }
   }
 else
  {
   if(unit == 255)
    {
     unit = 0;
     TR0 = 0;
     }
   }
 }
//display the result
void display(void)
{
 P0 = sevenseg[decimal];
 P2 = sevenseg[unit];
 }


路过

鸡蛋

鲜花

握手

雷人

发表评论 评论 (1 个评论)

回复 ... 2009-11-2 12:40
[emot]8[/emot]