/*===Chip:STC12C5A32S2=================================*/
/*===Software:Keil 4 C51===================================*/
/*===Author:梁鹏=========================================*/
/*===Organization:广西民族大学物电学院07自动化=======================*/
/*===Date:2010年05月26日=================================*/
/*===Version:1.0=========================================*/
/*===声明:本程序属原创作品,仅供学习交流,不得用于商业用途,如需
转载,请注明出处,谢谢合作!==============================*/
///*----------文件名:Traffic_light.c-------------*/
#include<reg52.h>
#include<intrins.h>
#include<LCD1602.H>
#define ON 0
#define OFF 1
#define yellow_time 3
//===========================
sbit MODE_KEY=P3^3; //调整时间的模式按键
sbit TIME_ADD=P3^4; //时间增加5s的按键
sbit TIME_DEC=P3^5; //时间减少5s的按键
sbit LOCK_KEY=P3^6; //封锁/恢复交通按键
sbit North_turn_R= P0^0; //南北左拐红灯
sbit North_turn_G= P0^1; //南北左拐绿灯
sbit North_Y = P0^2; //南北黄灯
sbit North_R = P0^3; //南北直行红灯
sbit North_G = P1^0; //南北直行绿灯
sbit East_turn_R = P1^1; //东西左拐红灯
sbit East_turn_G = P1^2; //东西左拐绿灯
sbit East_Y = P1^3; //东西黄灯
sbit East_R = P1^4; //东西直行红灯
sbit East_G = P1^5; //东西直行绿灯
//===========================
void delay_ms(uint); //延时子函数
void init(); //程序定时中断外部中断初始化
void system_on(); //系统上电,两只黄灯闪烁3秒的子函数
void display_lcd(); //LCD显示子函数
void display_light(); //LED灯的显示
void judge(); //各状态切换的判断函数
void yellow_light(); //黄灯闪烁3秒的子函数
void GoStraight(); //直行通路的子函数
void TurnLeft(); //左拐通路的子函数
//===========================
bit direction; //0->南北方向;1->东西方向;
bit flash_flags; //闪烁状态变量
bit change_flags; //计时结束变量
bit Lock_flags; //0->交通不封锁;1->交通封锁
uint ovf_cnt; //定时中断计数变量
uchar mode_flags; //按键改变变量
uchar run_flags; //运行时的状态变量
uchar time_temp; //计时时间缓存
uchar north_green_time=10;//南北直行绿灯时间初始化
uchar east_green_time=10; //东西直行绿灯时间初始化
/*左转绿灯时间与通行绿灯时间相等*/
/*-红灯时间为绿灯时间加黄灯时间-*/
/*------黄灯时时间固定为3秒-----*/
//uchar north_red_time;
//uchar east_red_time;
//uchar north_turn_time;
//uchar east_turn_time;
//===========================
//红灯切换到绿灯时,黄灯不用闪烁
//绿灯切换到红灯时,黄灯需要闪烁
//黄灯闪烁时,绿灯没有熄灭
//红灯时间=绿灯时间+黄灯时间
//左拐时间=直行绿灯时间
//============================================
//********************************************
void main()
{
init(); //定时中断外部中断初始化
LCD_init(); //LCD1602初始化
/*首次运行时,黄灯闪烁三秒*/
LCD_str(1,1,"System on ...");
time_temp=yellow_time;
system_on(); //两只黄灯闪烁子函数
while(1)
{
judge();
/*LED灯的显示*/
display_light();
/*1602显示*/
display_lcd();
}
}
//*********************************************
//=============================================
void init()
{
TMOD|=0x02; //定时器0方式2,自动重载
TH0=5;TL0=5; //250us中断一次
ET0=1; //允许定时器0中断
TCON|=0x01; //外部中断0下降沿触发
EX0=1; //允许外部中断0
EA=1; //允许全局总中断
TR0=1; //启动定时器0
}
//===========================
void system_on() //上电后,东西南北路口黄灯闪烁3秒
{
do{
if(flash_flags) {North_Y=OFF;East_Y=OFF;}
else {North_Y=ON; East_Y=ON; }
}while(!change_flags);//首次计时未完成不改变状态
}
//===========================
void display_lcd()
{
if((!mode_flags)&&(!Lock_flags)){//正常显示状态
switch(run_flags){
case 0:case 2:case 4:case 6:case 8: //黄灯闪烁状态
if(!direction){
if(flash_flags) {LCD_str(1,1," ");}
else {LCD_str(1,1,"Status Change...");}
}else{
if(flash_flags) {LCD_str(1,1," ");}
else {LCD_str(1,1,"Status Change...");}
}
break;
case 1:case 5: //直行状态
if(!direction) {LCD_str(1,1,"North GoStraight");}
else {LCD_str(1,1,"East GoStraight");}
break;
case 3:case 7: //左拐状态
if(!direction) {LCD_str(1,1,"North Turn Left ");}
else {LCD_str(1,1,"East Turn Left ");}
break;
}
LCD_str(2,6," "); //第二行清屏
}
if((mode_flags==1)&&(!Lock_flags)){//更改南北直通时间状态
LCD_str(1,1,"North Green Time");
LCD_str(2,6,"To: ");
LCD_int_(north_green_time);
LCD_str_("S ");
}
if((mode_flags==2)&&(!Lock_flags)){//更改东西直通时间状态
LCD_str(1,1,"East Green Time");
LCD_str(2,6,"To: ");
LCD_int_(east_green_time);
LCD_str_("S ");
}
if(!Lock_flags){/*当前状态剩余时间的显示*/
LCD_int(2,1,time_temp);
LCD_char(' ');
LCD_str(2,4,"S ")
}
if(Lock_flags){ //封锁交通时显示
if(flash_flags){
LCD_str(1,1," ");
LCD_str(2,1," ");
}else{
LCD_str(1,1," Please Wait... ");
LCD_str(2,1," Locking... ");
}
}
}
//===========================
void display_light()
{
if(!Lock_flags){//交通未被封锁时,交通灯按以下程序显示
/*以下由run_flags状态决定灯的亮灭*/
switch(run_flags){
case 0:case 2:case 4:case 6:case 8:
yellow_light(); break;
case 1:case 5:
GoStraight(); break;
case 3:case 7:
TurnLeft(); break;
default: run_flags=1;
}
/*红灯的亮灭*/
if(!direction){ //南北通行时,东西绿灯全灭,红灯全亮
East_R=ON; East_turn_R=ON;
East_G=OFF; East_turn_G=OFF;
East_Y=OFF;
}else{ //东西通行时,南北绿灯全灭,红灯全亮
North_R=ON; North_turn_R=ON;
North_G=OFF;North_turn_G=OFF;
North_Y=OFF;
}
}else{//封锁交通时,所有红灯亮
North_R=ON; North_turn_R=ON;
North_G=OFF;North_turn_G=OFF;
North_Y=OFF;East_Y=OFF;
East_R=ON; East_turn_R=ON;
East_G=OFF; East_turn_G=OFF;
}
}
//===========================
void yellow_light() /*黄灯闪烁的子函数*/
{
if(!direction){
//North_G=OFF; //南北直行绿灯灭
//North_turn_G=OFF;
if(flash_flags) {North_Y=OFF;}
else {North_Y=ON; }
}else{
//East_G=OFF;
//East_turn_G=OFF;
if(flash_flags) {East_Y=OFF; }
else {East_Y=ON; }
}
}
//===========================
void GoStraight(){ /*直行通路的子函数*/
if(!direction){
North_Y=OFF; //南北黄灯灭
North_turn_R=ON; //南北左拐红灯亮
North_R=OFF; //南北直行红灯灭
North_G=ON; //南北直行绿灯亮
}else{
East_Y=OFF;
East_turn_R=ON;
East_R=OFF;
East_G=ON;
}
}
//===========================
void TurnLeft() /*左转通路的子函数*/
{
if(!direction){
North_G=OFF; //南北直行绿灯灭
North_R=ON; //南北直行红灯亮
North_turn_R=OFF; //南北左拐红灯灭
North_turn_G=ON; //南北左拐绿灯亮
}else{
East_G=OFF;
East_R=ON;
East_turn_R=OFF;
East_turn_G=ON;
}
}
//===========================
void judge() /*状态切换判断函数*/
{
if(change_flags){ //状态计时结束标志
change_flags=0; //状态标志清零
run_flags++; //状态切换
if((run_flags==5)||(run_flags==9))direction=~direction;
switch(run_flags){ //状态时间赋值
case 0:case 2:case 4:case 6:case 8: //黄灯闪烁
time_temp=yellow_time;break;
case 1:time_temp=north_green_time;break;//南北直行
case 5:time_temp=east_green_time;break; //东西直行
case 3:time_temp=north_green_time;break;//南北左拐
case 7:time_temp=east_green_time;break; //东西左拐
default:run_flags=1;time_temp=north_green_time;
}
}
}
//===========================
void keys_scan() interrupt 0 /*按键扫描子函数*/
{
delay_ms(20);
if(!INT0){
if(!MODE_KEY){
if(++mode_flags>=3)mode_flags=0;
}
if(!TIME_ADD){
if(mode_flags==1)north_green_time+=5;
if(mode_flags==2)east_green_time+=5;
if(north_green_time>90)north_green_time=5;
if(east_green_time>90)east_green_time=5;
}
if(!TIME_DEC){
if(mode_flags==1)north_green_time-=5;
if(mode_flags==2)east_green_time-=5;
if(north_green_time>90)north_green_time=5;
if(east_green_time>90)east_green_time=5;
}
/*还可以在此处添加状态,用以改变左拐通行时间*/
if(!LOCK_KEY)Lock_flags=~Lock_flags;
}
INT0=1;
}
//===========================
void T0_isr() interrupt 1
{
if(ovf_cnt==1846)flash_flags=~flash_flags;
if(++ovf_cnt>=3691){
ovf_cnt=0;
flash_flags=~flash_flags;
if(!Lock_flags)if(time_temp--==1)change_flags=1;
}
}
//===========================
void delay_ms(uint xms)
{
uint i,j;
for(i=0;i<xms;i++)
for(j=0;j<990;j++);
}
本设计的源文件与hex文件下载:
http://www.51hei.com/ziliao/file/jiaotd1.rar,您的电路如果不同请自行更改端口.
/*--------------文件名:LCD1602.H----------------*/
#ifndef _LCD1602_H_
#define _LCD1602_H_
#define uint unsigned int
#define uchar unsigned char
#define LCM_P P2 //LCD的控制端口
#define LCM_DATA P0 //LCD的数据口
#define LCM_RS_0 LCM_P&=~(1<<5)
#define LCM_RS_1 LCM_P|= 1<<5
#define LCM_RW_0 LCM_P&=~(1<<6)
#define LCM_RW_1 LCM_P|= 1<<6
#define LCM_EN_0 LCM_P&=~(1<<7)
#define LCM_EN_1 LCM_P|= 1<<7
/*========================================*/
#define LCD_str(x,y,s) Locate(x,y);LCD_str_(s);
#define LCD_int(x,y,n) Locate(x,y);LCD_int_(n);
/*========================================*/
void LCD_init(); //LCM1602的初始化函数,在使用1602之前都必须调用
void Locate(uchar,uchar); //显示定位函数
void LCD_half(uchar); //送半字节函数
void LCD_char(uchar); //写一个字符函数
void LCD_cmd(uchar); //写命令函数
void LCD_int_(int); //写整型数据函数
void LCD_str_(uchar str[]);//写字符串数据函数
void LCD_delay(uint); //延时子函数
/******************************************/
/*-函数功能:液晶使用初始化---------------*/
/*-入口参数:无*/
/******************************************/
void LCD_init()
{
LCD_cmd(0x01);
LCD_delay(10);
LCD_cmd(0x28); //4位数据、双行显示、5*7(0x38为八位)
LCM_EN_1;_nop_();_nop_();_nop_();
LCM_EN_0; /*此处必须加上这两句*/
LCD_delay(10);
LCD_cmd(0x28);
LCD_cmd(0x06);
LCD_cmd(0x0c);
LCD_cmd(0x01);
LCD_delay(10);
}
/******************************************/
/*-函数功能:显示数据定位函数-------------*/
/*-入口参数:行坐标x、列坐标y-------------*/
/******************************************/
void Locate(uchar x,uchar y)
{
x&=0x01;
LCD_cmd((x==0)?(y+0xbf):(y+0x7f));
}
/******************************************/
/*-函数功能:送半字节函数-----------------*/
/*-入口参数:要写到液晶指令或数据寄存器的-*/
/* 字节的高四位或低四位---------*/
/******************************************/
void LCD_half(uchar dataw_)
{
LCM_DATA=(LCM_DATA&0x0f)|(dataw_);
LCM_EN_1;_nop_();_nop_();_nop_();
LCM_EN_0;
LCD_delay(1);//实际使用中加上10ms的延时
}
/******************************************/
/*-函数功能:写一位数据函数---------------*/
/*-入口参数:数据内容---------------------*/
/******************************************/
void LCD_char(uchar dataw)
{
LCM_RS_1;LCM_RW_0;LCM_EN_0;_nop_();
LCD_half(dataw&0xf0);
LCD_half(dataw<<4);
}
/*========================================*/
void LCD_cmd(uchar cmd)
{
LCM_RS_0;LCM_RW_0;LCM_EN_0;_nop_();
LCD_half(cmd&0xf0);
LCD_half(cmd<<4);
}
/*========================================*/
void LCD_str_(uchar *str)
{
while(*str)LCD_char(*str++);
}
/*========================================*/
void LCD_int_(int num)
{
uchar counter=0,num_str[10],neg_flags=0;
if(num<0){num=-num,neg_flags=1;}
do{
num_str[counter++]=num%10+0x30;
num/=10;
}while(num!=0);//循环结束后,counter的值比数长多1
if(neg_flags){num_str[counter++]='-';}
while(counter)
{
LCD_char(num_str[--counter]);
}//counter的值必须先减一
}
/*========================================*/
void LCD_delay(uint xus)
{
uint i=0,j=0;
for(i=0;i<xus;i++)
for(j=0;j<123;j++);
}
/*========================================*/
#endif
