/
Keil C 89S51 交通信号控制程序
(C) 版权所有 Dai_Weis@hotmailcom
/
#include "reg51h"
#define UINT unsigned int
#define ULONG unsigned long
#define UCHAR unsigned char
/
信号灯变量
南北方向绿灯
sbit n_bike_g = P1^0; //自行车
sbit n_right_g = P1^1; //右转
sbit n_up_g = P1^2; //直行
sbit n_left_g = P1^3; //左转 调头
南北方向红灯
sbit n_bike_r = P1^4; //自行车
sbit n_right_r = P1^5; //右转
sbit n_up_r = P1^6; //直行
sbit n_left_r = P1^7; //左转 调头
南北方向黄灯
sbit n_bike_y = P3^0; //自行车
sbit n_right_y = P3^1; //右转
sbit n_up_y = P3^2; //直行
sbit n_left_y = P3^3; //左转 调头
东西方向绿灯
sbit e_bike_g = P2^0; //自行车
sbit e_right_g = P2^1; //右转
sbit e_up_g = P2^2; //直行
东西方向红灯
sbit e_bike_r = P2^4; //自行车
sbit e_right_r = P2^5; //右转
sbit e_up_r = P2^6; //直行
东西方向黄灯
sbit e_bike_y = P3^4; //自行车
sbit e_right_y = P3^5; //右转
sbit e_up_y = P3^6; //直行
/
//延时
void delay(UINT t, UINT s)
{
while (t)
{
UINT i;
for (i = 0; i < s; i++)
{
}
t --;
}
}
//信号灯状态
void time_x(UCHAR P_P1, UCHAR P_P2, UCHAR P_P3)
{
P1 = P_P1;
P2 = P_P2;
P3 = P_P3;
delay(150, 65535);
}
void time_s(UCHAR P_P1, UCHAR P_P2, UCHAR P_P3, UCHAR P_P11, UCHAR P_P22)
{
UINT i;
for (i = 0; i < 3; i ++)
{
P1 = P_P1;
P2 = P_P2;
delay(5, 65535);
P1 = P_P11;
P2 = P_P22;
delay(5, 65535);
}
P1 = P_P1;
P2 = P_P2;
P3 = P_P3;
delay(10, 65535);
}
//主程序
void main()
{
P1 = P2 = P3 = 0x0;
while (1)
{
time_x(0xA5, 0x38, 0x0);
time_s(0xA4, 0x38, 0x1, 0xA5, 0x38);
time_x(0x96, 0x52, 0x0);
time_s(0x92, 0x52, 0x4, 0x96, 0x52);
time_x(0x5A, 0x52, 0x0);
time_s(0x50, 0x50, 0x2A, 0x5A, 0x52);
time_x(0xF0, 0x25, 0x0);
time_s(0xF0, 0x24, 0x20, 0xF0, 0x25);
time_x(0xD2, 0x16, 0x0);
time_s(0xD0, 0x10, 0x62, 0xD2, 0x16);
}
}
给你一个定时控制的信号系统,我只做的简单的测试,至于延时我用的软件,你自己想办法。^_^
Dai_Weis 于 2005-5-4 13:43:23
重新给你说明
/
Keil C AT89S51 交通信号控制程序
(C) 版权所有 Dai_Weis@hotmailcom
开发说明:
固定时间信号变换,南北设置调头、左传、直行、右转、自行车。
东西设置左传、直行、右转、自行车。
时序状态:
红 绿 红 绿
序号 左 前 右 自 左 前 右 自 前 右 自 前 右 自
1 1 0 1 0 0 1 0 1 1 1 1 0 0 0
2 1 0 0 1 0 1 1 0 1 0 1 0 1 0
3 0 1 0 1 1 0 1 0 1 0 1 0 1 0
4 1 1 1 1 0 0 0 0 0 1 0 1 0 1
5 1 1 0 1 0 0 1 0 0 0 1 1 1 0
/
另外修正个错误
while (1)
{
time_x(0xA5, 0x70, 0x0);
time_s(0xA4, 0x70, 0x1, 0xA5, 0x70);
ORG 0000H
MAIN:MOV DPTR,#0FF20H
MOV A,#03H
MOVX @DPTR,A
MOV 2AH,#1FH
MOV P1,#0CH
MOV R7,#19H
PRG1: LCALL DELAY1
DJNZ R7,PRG1
MOV P1,#0AH
MOV R7,#05H
PRG2: LCALL DELAY1
DJNZ R7, PRG2
MOV P1,#21H
MOV 2AH,#1FH
MOV R7, #19H
PGR3: LCALL DELAY1
DJNZ R7,PGR3
MOV P1,#11H
MOV R7,#05H
PGR4: LCALL DELAY1
DJNZ R7, PGR4
AJMP MAIN
DELAY1:DEC 2AH
MOV A,2AH
MOV B,#0AH
DIV AB
MOV DPTR,#TAB
MOVC A,@A+DPTR
MOV R2,A
MOV A, B
MOV DPTR ,#TAB
MOVC A,@A+DPTR
MOV R1,A
MOV R6,#64H
LOOP: MOV A,R2
MOV R4,#02H
LCALL DISPLAY
MOV A,R1
MOV R4,#01H
LCALL DISPLAY
DJNZ R6,LOOP
RET
TAB: DB 0C0H,0F9H,0A4H,0B0H,99H
DB 92H,82H,0F8H,80H,90H
DISPLAY:MOV R0,#0D0H
MOV R5,A
DISPLAY1:MOV A,R5
MOV DPTR,#0FF22H
MOVX @DPTR,A
MOV DPTR,#0FF21H
MOV A,R4
MOVX @DPTR,A
DJNZ R0,DISPLAY1
RET
END
对你的程序有点改动,你看一下行不
交通灯好简单啊 我不是自动化专业的都会
可惜找不到PLC方面的工作
TITLE=程序注释
Network 1 // 网络标题
// 网络注释
LD SM01
S M00, 1
Network 2
LD M00
A I00
S M01, 1
R M00, 1
Network 3
LD M06
A T43
S M01, 1
R M06, 1
Network 4
LD M01
A T38
S M02, 1
R M01, 1
Network 5
LD M02
A T39
S M03, 1
R M02, 1
Network 6
LD M03
A T40
S M04, 1
R M03, 1
Network 7
LD M04
A T41
S M05, 1
R M04, 1
Network 8
LD M05
A T42
S M06, 1
R M05, 1
Network 9
LD M06
A T43
S M00, 1
R M06, 1
Network 10
LD M01
O M02
O M03
= Q00
Network 11
LD M04
LDN T42
A SM05
A M05
OLD
= Q01
Network 12
LD M06
= Q02
Network 13
LD M04
O M05
O M06
= Q03
Network 14
LD M01
LD SM05
AN T39
A M02
OLD
= Q04
Network 15
LD M03
= Q05
Network 16
LD M01
TON T38, 200
Network 17
LD M02
TON T39, 30
Network 18
LD M03
TON T40, 20
Network 19
LD M04
TON T41, 200
Network 20
LD M05
TON T42, 30
Network 21
LD M06
TON T43, 20
unsigned char counter;
sbit red_nb=P2^0;
void main()
{
TMOD=
TH0
TL0
ET0
TR0
while(1) //根据楼主所给,有4种状态
{
state1;
state2;
state3;
state4;
}
}
void timer0() interrupt 1 //定时器0
{
TH0=0x3c;
TL0=0xb0;
counter++;
}
void state1() //控制南北红灯亮,东西绿灯亮,30s
{
unsigned char temp = 30;
red_nb=1;
green_dx=1;
while(1)
{
if( counter>19){counter=0;temp--} //每次counter=20,即1s,temp自减
if(temp) break; //当temp为0时,即30秒,退出状态1,进入状态2
}
}
没经过测试,完全是5分诱惑不大。。
以上就是关于急需一个单片机交通灯程序设计全部的内容,包括:急需一个单片机交通灯程序设计、单片机编程:设计十字路口交通灯,以最常用的两条线路的交通信号控制为例。、设计用PLC控制交通灯的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)