基于单片机STC89C52RC的交通灯



《基于单片机STC89C52RC的交通灯》由会员分享,可在线阅读,更多相关《基于单片机STC89C52RC的交通灯(13页珍藏版)》请在装配图网上搜索。
1、 基于单片机STC89C52RC的交通灯 摘要:本交通灯系统采用STC89C52RC作为核心控制器,控制红绿黄三盏灯,来达到模拟日常交通灯系统,其中倒计时时间通过1602显示出来。 关键词:交通灯 1602液晶 一、 设计方案 整个模拟交通灯系统用单片机STC89C52RC作为核心控制器,有序的控 制着红黄绿三盏灯的运行,实际交通灯系统所需的倒计时时间采用1602显示出来,并且其倒计时时间可通过独立式键盘进行调整。 STC89
2、C52
红黄绿LED
1602液晶
独立式键盘
系统框图
二、 主程序流程图
开始
系统初始化
等待中断
N 按键是否按下?
Y
调用按键处理函数
返回
三、 附录------源程序(C)
一、the main function of this system
/*
*用stc52实现简易交通灯
*/
#include 3、trins.h>
#include "define.h"
#include "lcd_1602.h"
#include "delay.h"
#include "some_inits.h"
#include "control.h"
/***************************************
*主函数void main()
****************************************/
void main()
{
sys_init();
while(1)
{
key_scan(); //扫描键盘
del 4、ay_ms(100);
}
}
二、include Some .h files
(1)defines.h
#ifndef _define_h
#define _define_h
#define uchar unsigned char
#define unint unsigned int
/* 液晶数据区 */
sfr lcd_bus=0x80; //液晶数据口
sbit E=P2^7;
sbit RW=P2^6;
sbit RS=P2^5;
/* 定义三盏led */
sbit red=P2^0;
sbit green=P2^1;
5、sbit yellow=P2^2;
/* 定义三个开关 */
sbit jia=P1^0; //加一键
sbit jian=P1^1; //减一键
sbit ok=P1^2; //确认键
uchar flag=1; //1,2,3:绿灯,黄灯,红灯
bit first=0;
/* 蜂鸣器 */
sbit bell=P1^4;
uchar sec_h; //秒针十位
uchar sec_l; //秒针个位
uchar g_h=1,g_l=0;//绿灯计时数据
uchar y_h=1,y_l=0;//黄灯计时数据
uchar r_h=1,r_l 6、=0;//红灯计时数据
#endif
(2)some_inits.h
#ifndef _some_inits_h
#define _some_inits_h
#include "lcd_1602.h"
#include "define.h"
//state some functions
void L1602_init();
void Time0_init();
/*系统初始化*/
void sys_init()
{
flag=1; //绿灯
green=1;
red=0;
yellow=0;
L1602_init();//1602液晶 7、初始化
Time0_init();//定时器0初始化
}
/*1602初始化*/
void L1602_init(void)
{
enable(0x01);
enable(0x38);
enable(0x0c);
enable(0x06);
enable(0xd0);
L1602_string(1,2,"traffic lights");
sec_h=g_h;
sec_l=g_l;
L1602_char(2,8,sec_h+0x30);
L1602_char(2,9,sec_l+0x30);
}
/* 定时器0初始化 */
8、
void Time0_init()
{
TMOD=0x01; //time0,time1工作在方式一
IE=0x8a; //开EA,允许ET0,允许ET1
TH0=0x4c; //50ms,晶振是11.0592M
TL0=0x00;
//TH1=0xfc; //1ms
//TL1=0x66;
TR0=1; //time0开始计数
}
#endif
(3)control.h
#ifndef _control_h_
#define _control_h_
#include 9、cd_1602.h"
#include "define.h"
#include "delay.h"
/* state some functions */
void inc();
void dec();
void fmq();
/* 键盘扫描函数 */
void key_scan()
{
if(ok==0)
{
delay_ms(5);
if(ok==0)
{
fmq();
enable(0x0c); //关光标
first=0;
TR0=1; //开始计数
}
else _nop_();
10、}
else if(jia==0 || jian==0)
{
delay_ms(5);
if(jia==0 || jian==0)
{
TR0=0; // 停止计数
fmq();
while(!first)
{
first=1;
enable(0x0f); //开光标,开反白
}
if(jia==0) inc();
else if(jian==0) dec();
}
else
{
_nop_();
}
}
}
/* 加一 */
void i 11、nc(void)
{
_nop_();
if(green==1 && yellow==0 && red==0)
{
if(g_l<9 && g_h<5) g_l++;
else
{
if(g_h<5)
{
g_h++;
g_l=0;
}
else
{
g_h=5;
g_l=0;
}
}
sec_h=g_h;sec_l=g_l;
}
else if(yellow==1 && red==0 && green==0)
{
if(y_l<9 && 12、 y_h<5) y_l++;
else
{
if(y_h<5)
{
y_h++;
y_l=0;
}
else
{
y_h=5;
y_l=0;
}
}
sec_h=y_h;sec_l=y_l;
}
else if(red==1 && green==0 && yellow==0 )
{
if(r_l<9 && r_h<5) r_l++;
else
{
if(r_h<5)
{
r_h++;
r_l=0;
13、 }
else
{
r_h=5;
r_l=0;
}
}
sec_h=r_h;sec_l=r_l;
}
L1602_char(2,9,sec_l+0x30);
L1602_char(2,8,sec_h+0x30);
}
/* 减一 */
void dec(void)
{
_nop_();
if(green==1 && yellow==0 && red==0)
{
if(g_l>0 && g_h>0)
{
g_l--;
}
el 14、se
{
if(g_h>0)
{
g_h--;
g_l=9;
}
else if(g_l>0)
{
g_l--;
}
else
{
g_h=0;
g_l=0;
}
}
sec_h=g_h;sec_l=g_l;
}
else if(yellow==1 && green==0 && red==0)
{
if(y_l>0 && y_h>0)
{
y_l--;
}
15、
else
{
if(y_h>0)
{
y_h--;
y_l=9;
}
else if(y_l>0)
{
y_l--;
}
else
{
y_h=0;
y_l=0;
}
}
sec_h=y_h;sec_l=y_l;
}
else if(red==1 && yellow==0 && green==0)
{
if(r_l>0 && r_h>0)
{
r_l--;
16、 }
else
{
if(r_h>0)
{
r_h--;
r_l=9;
}
else if(r_l>0)
{
r_l--;
}
else
{
r_h=0;
r_l=0;
}
}
sec_h=r_h;sec_l=r_l;
}
L1602_char(2,9,sec_l+0x30);
L1602_char(2,8,sec_h+0x30);
}
/* 蜂鸣器鸣响一声 */
void fm 17、q()
{
unint i=150;
while(i--)
{
bell=~bell;
delay_ms(1);
}
}
/* time0 service function */
void time0() interrupt 1
{
static count=0;
TR0=0;
if(count==20) //1s
{
count=0;
if(sec_l==0)
{
if(sec_h==0) //30s结束
{
if(flag==1)
{
flag++;
18、 green=1;
red=0;
yellow=0;
sec_h=g_h;
sec_l=g_l;
}
else if(flag==2) //状态二,亮黄灯
{
flag++;
green=0;
red=0;
yellow=1;
sec_h=y_h;
sec_l=y_l;
}
else if(flag==3)//状态三,红灯亮
{
flag=1; //回到状态一,绿灯亮
green=0;
19、
red=1;
yellow=0;
sec_h=r_h;
sec_l=r_l;
}
}
else //个位为0而已
{
sec_h--;
sec_l=9;
}
}
else
{
sec_l--;
}
L1602_char(2,8,sec_h+0x30);
L1602_char(2,9,sec_l+0x30);
}
else
{
count++;
}
TH0=0x4c;
20、TL0=0x00;
TR0=1;
}
#endif
(4)lcd_1602.h
#ifndef _lcd_1602_h
#define _lcd_1602_h
#include "define.h"
#include "delay.h"
uchar Convert(uchar In_Date)
{
uchar i, Out_Date = 0, temp = 0;
for(i=0; i<8; i++)
{
temp = (In_Date >> i) & 0x01;
Out_Da 21、te |= (temp << (7 - i));
}
return Out_Date;
}
/*写命令*/
void enable(uchar del)
{
P0 = Convert(del);
RS = 0;
RW = 0;
E = 0;
delay();
E = 1;
delay();
}
/*写数据*/
void write(uchar del)
{
P0 = Convert(del);
RS = 1;
RW = 0;
E = 0;
delay();
E = 1;
delay();
22、}
/*写字符*/
void L1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
enable(a);
write(sign);
}
/*写字符串*/
void L1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
enable(a);
while(1)
{
if(*p == '\0') break;
write(*p);
p++;
}
}
#endif
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 踏春寻趣 乐享时光——春季旅游踏春出游活动
- 清明假期至安全不缺席风起正清明安全需守护
- 全国党员教育培训工作规划
- XX中小学公共卫生培训树立文明卫生意识养成良好卫生习惯
- 小学生常见传染病预防知识培训传染病的预防措施
- 3月18日全国爱肝日中西医结合逆转肝硬化
- 肝病健康宣教守护您的肝脏健康如何预防肝炎
- 垃圾分类小课堂教育绿色小卫士分类大行动
- 中小学班主任经验交流从胜任到优秀身为世范为人师表 立责于心履责于行
- 教师数字化转型理解与感悟教师数字化转型的策略与建议
- 团建小游戏团建破冰小游戏团队协作破冰游戏多人互动
- 教师使用deepseek使用攻略让备课效能提升
- 办公室会议纪要培训会议内容会议整理公文攥写
- 党员要注重培塑忠诚奋斗奉献的人格力量
- 橙色卡通风儿童春季趣味运动会