#include<reg52.h>//包含头文件,一般情况不需要改动, //头文件包含特殊功能寄存器的定义 sbit LACTCH1=P2^2; sbit LACTCH2=P2^3; /*------------------------------------------------ 全局变量 ------------------------------------------------*/ bit red,green,yellow;//定义红绿黄灯标志位 code unsigned char tab[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴数码管 0-9 static unsigned char second=30,count; unsigned char Dis_Shiwei;//定义十位 unsigned char Dis_Gewei; //定义个位 /*------------------------------------------------ ------------------------------------------------*/ void delay(unsigned int cnt) { while(--cnt); } /*------------------------------------------------ 主程序 ------------------------------------------------*/ main() { TMOD=0x01;//定时器设置50ms in 12M crystal TH0=0x3c; TL0=0xb0; IE= 0x82; //打开中断 TR0=1; P1=0xfc; //红灯亮 red=1; while(1) { P0=Dis_Shiwei;//显示十位 LACTCH1=1; //锁存 LACTCH1=0; P0=0xfe; LACTCH2=1; // 锁存 LACTCH2=0; delay(300); //短暂延时 P0=Dis_Gewei; //显示个位 LACTCH1=1; //锁存 LACTCH1=0; P0=0xfd; LACTCH2=1; // 锁存 LACTCH2=0; delay(300); } } /*------------------------------------------------ 定时器0中断函数 ------------------------------------------------*/ void tim(void) interrupt 1 { TH0=0x3c;//重新赋值 TL0=0xb0; count++; if (count==20) { count=0; second--;//秒减1 if(second==0) { if(red) { red=0;green=1; second=27; P1=0xF3;//绿灯亮27秒 } else if(yellow) { yellow=0;red=1; second=30; P1=0xFC;//红灯亮30秒 } else if(green) { green=0;yellow=1; second=3; P1=0xCF;//黄灯亮3秒 } } Dis_Shiwei=tab[second/10];//十位显示值处理 Dis_Gewei=tab[second%10]; //个位显示处理 } }
|