单片机定时器0实现信号交通灯程序
/*
*功能:定时器0实现信号交通灯
*
*日期:2013-05-07-08:41
*作者:徐冉
*特别说明:本程序代码已经通过调试,仅供学习使用;
*
*/
/***********AT89C52-RC单片机实验板***********/
/*****************51hei-开发板*********************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit LED_hong = P1^0;//红信号灯
sbit LED_lv = P1^2; //绿信号灯
sbit wela = P2^7;
sbit dula = P2^6;
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
};
uchar num = 30, counter;
void init();
void display(uint num);
void delay(uint xms);
void main()
{
init();
while(1)
{
if(counter == 50)
{
counter = 0;
num--;
if(num == 0)
{
num = 30;
LED_hong = ~LED_hong;
LED_lv = ~LED_lv;
}
}
display(num);
}
}
void init()
{
LED_hong = 0;
LED_lv = 1;
TMOD = 0x01;
TH0 = 0xB8;
TL0 = 0x00;
TR0 = 1;
EA = 1;
ET0 = 1;
}
void display(uint num)
{
uchar shi, ge;
shi = num / 10 % 10;
ge = num % 10;
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfe;
wela = 0;
P0 = 0x00;
delay(1);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xfd;
wela = 0;
P0 = 0x00;
delay(1);
}
void delay(uint xms)
{
uint i, j;
for(i = xms; i > 0; i--)
for(j = 125; j > 0; j--);
}
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
}
单片机定时器0信号交通 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)