当前位置:首页 > 单片机 > 单片机
[导读] ___________________________________________ 功能:99秒计时 时间2010—7—18_________________________________________#include code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d

___________________________________________

功能:99秒计时

时间2010—7—18

_________________________________________

#include


code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsigned char Dis_Shiwei;
unsigned char Dis_Gewei;

void delay(unsigned int cnt)
{
while(--cnt);
}

main()
{

TMOD |=0x01; //10ms in 12M crystal,工作在模式一,16位定时器
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打开中断
TR0=1; //打开定时开关

while(1)
{
P0=Dis_Shiwei;
P2=0;
delay(300);
P0=Dis_Gewei;
P2=1;
delay(300);
}

}

void tim(void) interrupt 1 using 1
{
statICunsigned char second,count;
TH0=0xd8;
TL0=0xf0;

count++;
if (count==100) //100x10ms
{
count=0;
second++;
if(second==100)
second=0;
Dis_Shiwei=tab[second/10];
Dis_Gewei=tab[second%10];

}

}


/*

定时器设定为:TH0=0xd8;TL0=0xf0;经确定时10ms。从程序开始执行,每当主程序中的16位数发生溢出则触发中断,主程序保护现场并调用中断子程序,也可以写为:TH0=(65536-10000)/256;TL0=(65536-10000)%256。秒脉冲的产生要求精确定时1s,实际情况可能有延误。


1 0000 0000 0000 0000

1101 1000 1111 0000

————————————

0010 0111 0001 0000

Time=16(1+16+32+64+512)=16*625=10000us=10ms

主函数中的关于定时器和中断的控制位:

TMOD |=0x01; //模式选择
TH0=0xd8; //高8位
TL0=0xf0; //低8位
IE= 0x82; //打开中断
TR0=1; //打开定时开关(定时器运行控制位,置“1”启动定时器0)

TMOD |= 0x01; //使用模式1,16位定时器
TH0=0x00;
TL0=0x00;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开


*/

______________________________________________

功能:99秒倒计时

时间:2010—7—18

______________________________________________


#include


code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

unsigned char Dis_Shiwei;
unsigned char Dis_Gewei;

void delay(unsigned int cnt)
{
while(--cnt);
}

main()
{

TMOD |=0x01; //10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
IE= 0x82;
TR0=1;

while(1)
{
P0=Dis_Shiwei;
P2=0;
delay(300);
P0=Dis_Gewei;
P2=1;
delay(300);
}

}

void tim(void) interrupt 1 using 1
{
statICunsigned char second=99,count;
TH0=0xd8;
TL0=0xf0;
count++;
if (count==100)
{
count=0;
second--;
if(second==0)
{
second=99;
}
Dis_Shiwei=tab[second/10];
Dis_Gewei=tab[second%10];

}
}


本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭