交通信号灯,Proteus里面有专门的元件供仿真用,以下是程序和仿真图。
#include
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P3^0; //东西向指示灯
sbit YELLOW_A=P3^1;
sbit GREEN_A=P3^2;
sbit RED_B=P3^3; //南北向指示灯
sbit YELLOW_B=P3^4;
sbit GREEN_B=P3^5;
sbit KEY1=P1^0;
sbit KEY2=P1^1;
sbit KEY3=P1^2;
//延时倍数,闪烁次数,操作类型变量
uchar Flash_Count=0,Operation_Typea=1,LEDsng,LEDsns,LEDewg,LEDews,discnt;
uint Time_Count=0,time;
uchar ledtab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
void displaysn()
{
LEDsng=((time-Time_Count)/20)%10;
LEDsns=((time-Time_Count)/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
void displayew()
{
LEDewg=((time-Time_Count)/20)%10;
LEDews=((time-Time_Count)/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
//定时器0 中断函数
void T0_INT() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
switch(Operation_Type)
{
case 1: //东西向绿灯与南北向红灯亮
if((Time_Count%20)==0)displayew();
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=time) return;
Time_Count=0;
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
LEDewg=0x0;
LEDews=0x0;
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=3;
break;
case 3: //东西向红灯与南北向绿灯亮
if((Time_Count%20)==0)displaysn();
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=time) return;
Time_Count=0;
Operation_Type=4;
break;
case 4: //南北向黄灯开始闪烁,绿灯关闭
LEDsng=0x0;
LEDsns=0x0;
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10) return; //闪烁
Flash_Count=0;
Operation_Type=1;
break;
}
}
void t1_isr() interrupt 3
{
TR1=0;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
switch(discnt)
{
case 0:
P2=0x02;
P0=ledtab[LEDewg];
break;
case 1:
P2=0x01;
P0=ledtab[LEDews];
break;
case 2:
P2=0x08;
P0=ledtab[LEDsng];
break;
case 3:
P2=0x04;
P0=ledtab[LEDsns];
break;
default:discnt=0;break;
}
discnt++;
discnt&=0x03;
}
void delay()
{
uint i;
for(i=0;i<1000;i++);
}
//主程序
void main()
{
TMOD=0x11; //T0 方式1
EA=1;
ET0=1;
TR0=1;
TH1=(65536-3000)/256;
TL1=(65536-3000)%256;
TR1=1;
ET1=1;
time=100;
Time_Count=100;
Time_Count=0;
Operation_Type=1;
while(1)
{
if(KEY1==0) //按一下加1S
{
delay();
if(KEY1==0)
{
while(KEY1==0);
TR0=0;
time+=20;
LEDsng=(time/20)%10;
LEDsns=(time/20)/10;
LEDewg=0x10;
LEDews=0x10;
}
}
if(KEY2==0) //按一下减1S
{
delay();
if(KEY2==0)
{
while(KEY2==0);
TR0=0;
time-=20;
if(time==0)time=20;
LEDewg=(time/20)%10;
LEDews=(time/20)/10;
LEDsng=0x10;
LEDsns=0x10;
}
}
if(KEY3==0) //启动
{
delay();
if(KEY3==0)
{
while(KEY2==0);
TR0=1;
Time_Count=0;
}
}
}
}
要文件,恐怕是要付费的。
参考以下的,自己做一做,并不难:
http://hi.baidu.com/do_sermon/item/92e081f975f6120ad99e72ec
基于51单片机的交通灯C语言程序设计十字交叉路口的交通灯控制器,是很常见的课程设计题目。做而论道以前写过一个,功能十分简单。
最近,综合了一些百度知道中的题目要求,写出了一个稍稍复杂一点设计方案,使用了大家比较喜欢的C语言编写程序。
基本功能如下:
(1)东西、南北方向各设有一个绿、黄、红指示灯,两个显示数码管。
(2)两个方向交替允许通行,基本放行时间为25s,另外有黄灯闪烁5s。
(3)控制人员可以暂停自动的交替,使某个方向可以无限长时间的通行。
(4)暂停期间,控制人员可以调整通行的时间,程序中还设置了调整的上下限。
(5)暂停之后,灯光将按照设置的通行时间自动变换通行方向。
(6)在正常通行期间,可以强制变换通行方向。强制变换的时候,也是要先显示闪烁的黄灯,然后切换到指定的方向。
程序用PROTEUS仿真成功,并没有进行硬件实验。仿真截图画面如下。
插图链接:http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/album/item/2b4a22423e0d5c4972f05de6.html#
http://hi.baidu.com/do_sermon/item/255f69c7c8132850bcef6965
C语言的控制程序如下:
//------------------------------------------------
#include
#define uchar unsigned char
#define uint unsigned int
//----------------定义接口------------------------
#define K_IN P1 //指定按键输入端口
#define Star_Stop 1 //指定按键输入端口
#define Time_ADD 2 //指定按键输入端口
#define Time_SUB 4 //指定按键输入端口
#define Turn_EW 8 //指定按键输入端口
#define Turn_SN 16 //指定按键输入端口
sbit D_R = P3^0;
sbit D_Y = P3^1;
sbit D_G = P3^2;
//----------------定义变量------------------------
uchar Key_C; //新键值
uchar Key_L; //上次键值
uchar Trg; //触发(有键按下)triger
uchar Trg_L; //上次触发
uchar Key_P; //确实按下
uchar n_2ms, n_0D5s, D_SEC = 60; //60实际上是30秒
uchar code D_seg[] = { //共阳段码
0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; //0-9
bit s_g, run, hong;
//----------------延时----------------------------
void delay(uint z)
{
uint x, y;
for(x = z; x > 0; x--) for(y = 123; y > 0; y--);
}
//----------------读按键--------------------------
void KeyRead( void ) //读按键所在端口,送到全局变量
{
Key_L = Key_C; //上次按键数值
Trg_L = Trg;
Key_C = ~K_IN; //反相读入,有键按下,将读入1
Trg = Key_C & (Key_C ^ Key_L);
Key_P = Key_C & Trg_L; //确认按下
}
//----------------初始化--------------------------
void init(void)
{
TMOD = 0x01; //T0定时方式1
TH0 = (65536 - 2000) / 256; //2ms@12MHz
TL0 = (65536 - 2000) % 256;
TR0 = 1; //启动T0
ET0 = 1; //开T0中断
EA = 1; //开总中断
//---------------------
D_Y = 0; //关灯
D_R = hong; //红灯取决于当前的hong
D_G = ~D_R; //绿灯则相反
n_0D5s = D_SEC; //倒计时的秒数(2倍的数值)
}
//----------------主函数--------------------------
void main(void)
{
hong = 1; //hong = 1: 东西为红灯
init();
run = 1; //开机即运行
while(1) {
delay(8); //延时8ms
if (Star_Stop & Key_P) {
run = ~run;
if (!run) init();
}
if (Time_ADD & Key_P && !run) {
D_SEC += 2;
if (D_SEC > 120) D_SEC = 120;
n_0D5s = D_SEC;
}
if (Time_SUB & Key_P && !run) {
D_SEC -= 2;
if (D_SEC < 20) D_SEC = 20;
n_0D5s = D_SEC;
}
if (Turn_EW & Key_P && run && hong) n_0D5s = 11;
if (Turn_SN & Key_P && run && !hong) n_0D5s = 11;
}
}
//----------------T0中断函数----------------------
void T0_INT(void) interrupt 1
{
TL0 = (65536 - 2000) % 256; //重新写入初值
TH0 = (65536 - 2000) / 256; //2ms@12MHz
//----------------数码显示------------------------
s_g = ~s_g; //十位个位交替显示
if (s_g) {P2 = 0; P0 = D_seg[(n_0D5s / 2) / 10]; P2 = 1;}
else {P2 = 0; P0 = D_seg[(n_0D5s / 2) % 10]; P2 = 2;}
//----------------修改时间------------------------
n_2ms--;
if (n_2ms % 5 == 0) KeyRead(); //10ms读一次按键
if (n_2ms == 0) {
n_2ms = 250; //定时500ms
if (run) { //如果是正在运行
n_0D5s--; //就倒计时
if(n_0D5s == 1) n_0D5s = D_SEC + 1;//减到1就重新开始
if(n_0D5s == D_SEC + 1) {//如果是重新开始
if (hong) { //如果当前是红灯亮
hong = 0; //下面就改成绿灯亮
D_Y = 0; //关闭黄灯
D_G = 1; //打开绿灯
D_R = 0; //关闭红灯
}
else { //否则就改成红灯亮
hong = 1;
D_Y = 0; //关闭黄灯
D_G = 0; //关闭绿灯
D_R = 1; //打开红灯
} }
if(n_0D5s <= 11) {//如果是最后5秒
D_Y = ~D_Y; //黄灯闪烁
D_G = 0; //关闭绿灯
D_R = 0; //关闭红灯
} } } }
//----------------程序结束------------------------