标题:
使用单片机定时器控制交通灯设计
[打印本页]
作者:
周春雨0321
时间:
2018-12-10 09:27
标题:
使用单片机定时器控制交通灯设计
这是我课程设计需要的源代码和原理图,发给大家
0.png
(12.6 KB, 下载次数: 24)
下载附件
2018-12-10 15:58 上传
单片机源程序如下:
#include <reg51.h>
#define SEG P2
#define SCANP P1
#define count_M1 50000
#define TH_M1 (65535-count_M1)/256;
#define TL_M1 (65535-count_M1)%256;
int count_T0=0;
#define count_M2 250
#define TH_M2 (256-count_M2)
#define TL_M2 (256-count_M2)
char count_T1=0;
char code TAB[10]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x98};
char disp[2]={0x82,0xc0};
bit red,green,yellow,turnred; //定义红绿黄灯的标志位
char scan=0;
sbit hong=P3^2;
sbit huang=P3^1;
sbit lv=P3^0;
main()
{
IE=0x8a;
TMOD=0x21;
TH0=TH_M1;TL0=TL_M1;
TR0=1;
TH1=TH_M2;TL1=TL_M2;
TR1=1;
red=1;
green=0;
yellow=0;
hong=0;
while(1);
}
void delay()
{
unsigned char a,b;
for(a=100;a>0;a--)
for(b=255;b>0;b--);
}
void TO_1s(void)interrupt 1
{
static char seconds = 60;
TH0=TH_M1;TL0=TL_M1;
if(++count_T0==20)
{
count_T0=0;
seconds--;
if(seconds==0)
{if(red==1&green==0&yellow==0)
{red=0;
green=1;
yellow=0;
hong=1;
lv=0;
seconds=30;
delay();
}
else if(red==0&green==1&yellow==0)
{
green=0;
red=0;
yellow=1;
lv=1;
huang=0;
seconds=3;
delay();
}
else if(red==0&green==0&yellow==1)
{
red=1;
green=0;
yellow=0;
hong=0;
huang=1;
seconds=60;
delay();
}
}
disp[1]=TAB[seconds%10];
disp[0]=TAB[seconds/10];
}
}
void T1_8ms(void)interrupt 3
{
if(++count_T1==32)
{
count_T1=0;
if(++scan==3)scan=1;
SEG=0xff;
SCANP=~scan;
SEG=disp[scan-1];
}
}
复制代码
所有资料51hei提供下载:
使用定时器的交通灯.rar
(26.51 KB, 下载次数: 12)
2018-12-10 09:27 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1