標題: 紅綠交通信號燈代碼 [打印本頁]
作者: shiwei3828123 時間: 2018-6-17 23:25
標題: 紅綠交通信號燈代碼
#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]; //個位顯示處理
}
}
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |