|
/*
1、程序目的:使用定時(shí)器學(xué)習(xí) 倒計(jì)時(shí) 紅綠燈原理 主要程序和倒計(jì)時(shí)一樣
2、硬件要求:數(shù)碼管、晶振12M
*/
#include <reg52.h>
bit red,green,yellow,turnred;
code unsigned char tab[]=
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//共陽(yáng)數(shù)碼管 0-9
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個(gè)位
void delay(unsigned int cnt)
{
while(--cnt);
}
main()
{
TMOD |=0x01;//定時(shí)器設(shè)置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打開中斷
TR0=1;
P1=0xfc;//紅燈亮
red =1;
while(1)
{
P0=Dis_Shiwei;//顯示十位
P2=0xFD;
delay(300);//短暫延時(shí)
P0=Dis_Gewei; //顯示個(gè)位
P2=0xFE;
delay(300);
}
}
/********************************/
/* 定時(shí)中斷 */
/********************************/
void tim(void) interrupt 1 using 1
{
static unsigned char second=60,count; //初值99
TH0=0xd8;//重新賦值
TL0=0xf0;
count++;
if (count==100)
{
count=0;
second--;//秒減1
if(second==0)
{ //這里添加定時(shí)到0的代碼 ,可以是燈電路,繼電器吸合等,或者執(zhí)行一個(gè)程序
if(red)
{
red=0;yellow=1;
second=5;
P1=0xF3;//黃燈亮5秒
}
else if(yellow && !turnred)
{
yellow=0;green=1;
second=50;
P1=0xCF;//綠燈亮50秒
}
else if(green)
{
yellow=1;green=0;
second=5;
P1=0xF3;//黃燈亮5秒
turnred=1;
}
else if(yellow && turnred)
{
red=1;yellow=0;
P1=0xFC;//紅燈亮60秒
second=60;
turnred=0;
}
}
Dis_Shiwei=tab[second/10];//十位顯示值處理
Dis_Gewei=tab[second%10]; //個(gè)位顯示處理
}
}
|
-
-
10-交通燈.rar
2018-12-7 16:24 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
2.06 KB, 下載次數(shù): 2, 下載積分: 黑幣 -5
交通燈
|