求程序用8051单片机设计一交通信号灯模拟控制系统的设计
有现成的一篇
- ORG 00HMAIN:
- MOV P2,#05H
- MOV R1,#20
- LOAD:
-
- MOV A,R1
- MOV B,#10
- div AB
- MOV R2,A
- MOV R3,B
- SWAP A
- ADD A,R3
- MOV P0,A
- CALL DELAY
- DJNZ R1,LOAD
- MOV P0,#00H
- CALL DELAY
- MAIN2:
- MOV P2,#30H
- MOV R4,#05H
- LOAD_1:
- MOV P0,R4
- CALL DELAY
- MOV P0,#0FFH
- CALL TIME
- DJNZ R4,LOAD_1
- MOV P0,#0FFH
- CALL DELAYMAIN3:
- MOV P2,#0AH
- MOV R1,#20
- LOAD2:
-
-
- MOV A,R1
- MOV B,#10
- div AB
- MOV R2,A
- MOV R3,B
- SWAP A
- ADD A,R3
- MOV P0,A
- CALL DELAY
- DJNZ R1,LOAD2
- MOV P0,#00H
- CALL DELAY
- MAIN4:
- MOV P2,#30H
- MOV R4,#05HLOAD_2:
- MOV P0,R4
- CALL DELAY
- MOV P0,#0FFH
- CALL TIME
- DJNZ R4,LOAD_2
- MOV P0,#0
- CALL DELAY
- JMP MAIN TIME:
- MOV R2,#10
- D3:
- MOV R6,#10
- D4:
- MOV R7,#30
- DJNZ R7,$
- DJNZ R6,D4
- DJNZ R3,D3
- RET
-
- DELAY:
- MOV R5,#40
- D1:
- MOV R6,#50
- D2:
- MOV R7,#248
- DJNZ R7,$
- DJNZ R6,D2
- DJNZ R5,D1 RET
- END
我也学了点汇编,能看懂,但让我自己写我是写不出来
//这个很简易的
#include<reg51.h>
sbit P10=P1^0; //红
sbit P11=P1^1; //绿
void delay(int i) //廷时,你想要多少就可以设多少,要想准确就用定时/中断
{
int j,k;
for(j=0;j<i;j++)
for(k=0;k<110;k++);
}
void main()
{
while(1)
{
P10=0;
P11=1;
delay(1);
P10=1;
p11=0;
delay(1);
}
}
呵呵,强悍啊。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; //东西向指示灯
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3; //南北向指示灯
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
//延时倍数,闪烁次数,操作类型
变量
uchar Time_Count=0,Flash_Count=0,Operation_Type=1;
//定时器0 中断函数
void T0_INT() interrupt 1
{
TL0=-50000/256;
TH0=-50000%256;
switch(Operation_Type)
{
case 1: //东西向绿灯与南北向红灯亮 5s
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=2;
break;
case 2: //东西向黄灯开始闪烁,绿灯关闭
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: //东西向红灯与南北向绿灯亮 5s
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=100) return; //5s(100*50ms)切换
Time_Count=0;
Operation_Type=4;
break; case 4: //南北向黄灯开始闪烁,绿灯关闭
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 main()
{
TMOD=0x01; //T0 方式 1
IE=0x82;
TR0=1;
while(1);
}
百度一下交通灯,看看原理,我记得一般都是用定时器做的
好多大神啊,真心感谢
留言。。
学习!