一、设计背景
马路上大量车辆的行驶需要遵守一定的交通规则,红绿灯便是最重要的一个管理交通的手段。红绿灯(交通信号灯)系以规定之时间上交互更迭之光色讯号,设置于交岔路口或其他特殊地点,用以将道路通行权指定给车辆驾驶人与行人,管制其行止及转向之交通管制设施。为一由电力运转之交通管制设施,以红、黄、绿三色灯号或辅以音响,指示车辆及行人停止、注意与行进,设于交岔路口或其他必要地点。
二、设计要求
利用单片机设计一个红绿灯,实现了数显倒计时与灯光变换,其中30秒红灯,25秒绿灯,5秒黄灯,实现东西与南北灯光变换,符合红绿灯逻辑。(所有时间也可以修改。)
三、设计效果
如下图:
四、设计方案
芯片采用:MSP430F249、74HC573 交通灯采用:trafficlight组件 数显采用:两位数共阴极的数码管
设计图为:
五、芯片源码设计
代码设计:
#include <msp430f249.h>
#define AHout P5OUT = 0x01;
#define ALout P5OUT = 0x02;
#define BHout P5OUT = 0x04;
#define BLout P5OUT = 0x08;
#define EndOut P5OUT = 0x00;
#define LightOut P5OUT |= 0x10
#define Hred BIT0
#define Hyellow BIT1
#define Hgreen BIT2
#define Vred BIT3
#define Vyellow BIT4
#define Vgreen BIT5
#define Vertical 0
#define Horizontal 1
int num[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
int timeA,timeB;
int red,yellow,green;
int dir;
void SetLights(int light)
{
P6OUT = light;
LightOut;
EndOut;
}
void Init()
{
P4SEL &= 0x00;
P4DIR |= 0xff;
P5SEL &= 0x00;
P5DIR |= 0xff;
P6SEL &= 0x00;
P6DIR |= 0xff;
P1SEL &= ~0x03;
P1DIR |= 0x03;
P1OUT = 0x01;
P4OUT = 0xff;
P5OUT = 0xff;
P6OUT = 0xff;
TACTL = TASSEL_1 + MC_1;
CCR0 = 32768;
CCTL0 = CCIE;
TBCTL = TBSSEL_1 + MC_1;
TBCCR0 = 500;
TBCCTL0=CCIE;
dir=Vertical;
SetLights(Vred + Hgreen);
}
void ReSet()
{
red=30;
yellow=5;
green=25;
}
void SetData()
{
if(dir == Horizontal)
{
timeA=red;
if(green > 0)
{
timeB = green;
SetLights(Hred + Vgreen);
}
else
{
timeB = yellow;
SetLights(Hred + Vyellow);
}
}
else
{
timeB=red;
if(green > 0)
{
timeA = green;
SetLights(Vred + Hgreen);
}
else
{
timeA = yellow;
SetLights(Vred + Hyellow);
}
}
}
void WriteA(int high,int low)
{
P4OUT = high;
AHout;
EndOut;
P4OUT = low;
ALout;
EndOut;
}
void WriteB(int high,int low)
{
P4OUT = high;
BHout;
EndOut;
P4OUT = low;
BLout;
EndOut;
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A()
{
SetData();
WriteA(num[timeA/10],num[timeA%10]);
WriteB(num[timeB/10],num[timeB%10]);
red--;
green--;
if(green < 0)
{
yellow--;
if(yellow <= 0)
{
if(dir == Horizontal) dir = Vertical;
else dir = Horizontal;
ReSet();
}
}
}
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B()
{
P1OUT = ~P1OUT;
}
void main()
{
WDTCTL = WDTPW + WDTHOLD;
Init();
ReSet();
_BIS_SR(LPM3_bits + GIE);
}
六、资源下载与学习
CSDN:基于单片机的红绿黄灯设计(单片机实验交通灯设计)
下载后,解压并打开t32.dsn,将debug-exe-1.hex配置进芯片MSP430F249,点击运行即可,不会配置可以私信我
资源正在审核中,需要也可以私信我
|