基于c51单片机的交通灯设计proteus仿真源码(含实训报告)

这个是我大学时候的实训设计,做了挺长时间,中间也遇到了很多的问题,但是最后以近满分通过了考核,现在毕业了,分享出来供大家学习使用,代码是用C语言写的,需要下载keil4打开,或者用其他软件重新创建工程,仿真是用的Proteus,其他软件可能打不开。

功能要求及实现

设计内容及要求

设计思路

单片机控制器及焊接终板

由于要求中需要用一个单片机控制另一个单片机,所以就需要用到通讯功能,所以需要另外一个单片机控制器,或者是把同学的单片机烧录相关程序。

通信模块红绿灯紧急通行-时间暂停-一方红灯

主控代码


include <reg51.h> //头文件
#define uchar unsigned char
#define uint unsigned int //宏定义
sbit led_red_s=P1^4;
sbit led_ye_s=P1^5;
sbit led_gre_s=P1^6;
sbit led_red_e=P3^4;
sbit led_ye_e=P3^5;
sbit led_gre_e=P3^6;
sbit s_led1=P1^0;
sbit s_led2=P1^1;
sbit e_led1=P1^2;
sbit e_led2=P1^3;
sbit sy=P3^7;
bit sg;
int i;
uchar stc[2],stc1[2];
char e=25,s=28;
uchar code table[10]={0x00,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9};
uint sec,sec1;
uchar a,b,c;
void ding1()
{
SCON=0X50;
PCON|=0X80;
TMOD&=0X0F;
TMOD|=0X20;
TL1=0XF3;
TH1=0XF3;
ET1=0;
TR1=1;
EA=1;
ES=1;
}
void ding0(void)
{
TMOD&=0XF0;
TMOD|=0X01;
ET0=1;
TH0=0X3C; //定时器0置初值 0.05S
TL0=0XB0;
TF0=0;
TR0=1;
EA=1;
PT0=0;
}
void EXTI0_Init()
{
IT0 = 1;
EX0 = 1;
EA = 1;
}
void EXTI1_Init()
{
IT1 = 1;
EX1 = 1;
EA = 1;
}
void Time0(void)interrupt 1 using 1
{
TH0=0X3C;
TL0=0XB0;
sec++;
sec1++;
if(a == 0)
{
if(e > 3)
{
if(sec >= 20)
{
sec = 0;
e--;
}
}
else
{
if(sec == 10)
led_gre_s =! led_gre_s;
if(sec >= 20)
{
led_gre_s =! led_gre_s;
sec = 0;
e--;
if(e == -1)
{
e = 2;
led_ye_s = 1;
led_gre_s = 0;
led_red_s = 0;
a = 1;
}

}
}
}
else if(a == 1 && sec >= 20)
{
sec = 0;
e--;
if(e==-1)
{
e=28;
led_ye_s=0;
led_gre_s=0;
led_red_s=1;
a=2;
}
}
else if(a == 2 && sec >= 20)
{
sec=0;
e--;
if(e==-1)
{
e=25;
led_ye_s=0;
led_gre_s=1;
led_red_s=0;
a=0;
}
}

if(b==0)
{
if(sec1 >= 20)
{
sec1 = 0;
s--;
if(s<=-1)
{
s=25;
b=1;
led_red_e=0;
led_ye_e=0;
led_gre_e=1;
}
}
}
else if(b==1)
{
if(s>3)
{
if(sec1>=20)
{
sec1 = 0;
s--;
}
}
else
{
if(sec1 == 10)
{
led_gre_e =! led_gre_e;
}
if(sec1 >= 20)
{
led_gre_e =! led_gre_e;
sec1 = 0;
s--;
if(s <= -1)
{
s = 2;
b = 2;
led_red_e = 0;
led_ye_e = 1;
led_gre_e = 0;
}
}
}
}
else if(b == 2 && sec1 >= 20)
{
sec1 = 0;
s--;
if(s <= -1)
{
s = 28;
b = 0;
led_red_e = 1;
led_ye_e = 0;
led_gre_e = 0;
}
}
}
void delay(int a)
{
while(a--);
}
unsigned char Receive()
{
unsigned char dat;
while(RI == 0);
RI=0;
ACC=SBUF;
return dat;
}
void display(void) //显示子程序
{
for(i=0;i<4;i++)
{
switch(i)
{
case(0):
s_led1 = 0;
s_led2 = 1;
e_led1 = 1;
e_led2 = 1;
P0 = stc[1];
delay(9);
break;
case(1):
s_led1 = 1;
s_led2 = 0;
e_led1 = 1;
e_led2 = 1;
P0 = stc[0];
delay(9);
break;
case(2):
s_led1 = 1;
s_led2 = 1;
e_led1 = 0;
e_led2 = 1;
P0 = stc1[1];
delay(9);
break;
case(3):
s_led1 = 1;
s_led2 = 1;
e_led1 = 1;
e_led2 = 0;
P0 = stc1[0];
delay(9);
break;
}
}
stc[0] = table[e%10];
stc[1] = table[e/10];
stc1[0] = table[s%10];
stc1[1] = table[s/10];
}
void duan() interrupt 4
{
char NUM;
if(RI == 1)
{
NUM = SBUF;
if(NUM == 'h')//hpf
{
sg=sy;
TR0=0;
if(sg==0)
{ led_red_e = 1;
led_ye_e = 0;
led_gre_e = 0;
led_red_s = 1;
led_ye_s = 0;
led_gre_s = 0;
}
else if(sg==1)
{led_red_e = 1;
led_ye_e = 0;
led_gre_e = 0;
led_red_s = 1;
led_ye_s = 0;
led_gre_s = 0;
}}
else if(NUM == 'o')//off05
{
led_red_e = 1;
led_ye_e = 0;
led_gre_e = 0;
led_red_s = 0;
led_ye_s = 0;
led_gre_s = 1;
TR0=1;
}
RI = 0;
}
}
void main()
{
led_red_s=0;
led_ye_s=0;
led_gre_s=1;
led_red_e=1;
led_ye_e=0;
led_gre_e=0;
EXTI0_Init();
EXTI1_Init();
ding0();
ding1();
while(1) //主循环
display();
}
}
void Int0_Routine() interrupt 0
{
sg=sy;
led_red_e = 1;
led_ye_e = 0;
led_gre_e = 0;
led_red_s = 0;
led_ye_s = 0;
led_gre_s = 1;
TR0=0;
}
void Int1_Routine() interrupt 2
{
sg=sy;
led_red_e = 0;
led_ye_e = 0;
led_gre_e = 1;
led_red_s = 1;
led_ye_s = 0;
led_gre_s = 0;
TR0=0;
}

紧急通行通讯代码

#include "reg52.h"
//#include "stdio.h"
unsigned char Flag;
void UART_SendByte(unsigned char Byte) //发送一个字节到串口
{ 
SBUF=Byte;
while(TI==0);
TI=0;
}
void UART_SendString(unsigned char *String)
{
unsigned char i=0;
while(String[i] != '\0') //循环检查发送字符,直到遇到字符串结束符 '\0'
{
UART_SendByte(String[i]);
i++;
}
}
void UART_Init()
{
SCON=0x50; //启用串口,设置为模式1
PCON |= 0x80; //设置波特率发生器为高速模式
TMOD &= 0x0F;//配置定时器 T1 的模式为 8 位自动重装载模式 
TMOD |= 0x20; 
TL1 = 0xF3; //设置 T1 的初值和重载值为 0xF3 
TH1 = 0xF3; 
ET1 = 0; //启用串口中断 
TR1 = 1; //启动定时器 T1 
EA=1; //使能总中断
ES=1; //使能串口中断
}
void EXTI0_Init()//初始化外部中断
{
IT0 = 1; //设置电平触发
EX0 = 1; //使能中断0
EA = 1;
}
void EXTI1_Init()//初始化外部中断1
{
IT1 = 1;
EX1 = 1;
EA = 1;
}
void main()
{
UART_Init();
EXTI0_Init();
EXTI1_Init();
while(1)
{
if(Flag == 1)
{
UART_SendByte('h');//hpf
Flag = 0;
}
if(Flag == 2)
{
UART_SendString("off05");
Flag = 0;
}

}
}
void Int0_Routine() interrupt 0 //中断0触发时,将 Flag 设置为 1
{
Flag = 1;
} 
void Int1_Routine() interrupt 2
{
Flag = 2;
} 
void UART() interrupt 4 //将TI标志位清零
{
// TI=0;
}

最终焊接布线成品展示

 

 

仿真+代码+实训报告下载

抱歉,只有登录并在本文发表评论才能阅读隐藏内容
点赞
  1. 小弟 小弟说道:
    QQbrowser Windows 10
    拿着就能用,爽歪歪
  2. Crackaction说道:
    Google Chrome Windows 10
    Your dedication to continuous improvement is inspiring. Keep up the fantastic work! reFX Nexus Crack Free Download for PC 64-bit
  3. Google Chrome Windows 10
    感谢分享
  4. tead网说道:
    Google Chrome Windows 10
    感谢分享啊

发表回复

电子邮件地址不会被公开。必填项已用 * 标注