基于单片机STC89C52RC的交通灯.doc_第1页
基于单片机STC89C52RC的交通灯.doc_第2页
基于单片机STC89C52RC的交通灯.doc_第3页
基于单片机STC89C52RC的交通灯.doc_第4页
基于单片机STC89C52RC的交通灯.doc_第5页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于单片机stc89c52rc的交通灯摘要:本交通灯系统采用stc89c52rc作为核心控制器,控制红绿黄三盏灯,来达到模拟日常交通灯系统,其中倒计时时间通过1602显示出来。关键词:交通灯 1602液晶一、 设计方案整个模拟交通灯系统用单片机stc89c52rc作为核心控制器,有序的控制着红黄绿三盏灯的运行,实际交通灯系统所需的倒计时时间采用1602显示出来,并且其倒计时时间可通过独立式键盘进行调整。stc89c52红黄绿led1602液晶独立式键盘 系统框图二、 主程序流程图开始系统初始化 等待中断 n 按键是否按下? y调用按键处理函数 返回三、 附录-源程序(c)一、the main function of this system /*用stc52实现简易交通灯*/#include#include#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();/扫描键盘delay_ms(100);二、include some .h files(1)defines.h#ifndef _define_h#define _define_h#define uchar unsigned char#define unint unsigned int/*液晶数据区*/sfrlcd_bus=0x80;/液晶数据口sbit e=p27;sbitrw=p26;sbitrs=p25;/*定义三盏led*/sbitred=p20;sbitgreen=p21;sbityellow=p22;/*定义三个开关*/sbitjia=p10; /加一键sbitjian=p11;/减一键sbitok=p12;/确认键ucharflag=1;/1,2,3:绿灯,黄灯,红灯bitfirst=0;/*蜂鸣器*/sbitbell=p14;ucharsec_h;/秒针十位ucharsec_l;/秒针个位uchar g_h=1,g_l=0;/绿灯计时数据uchary_h=1,y_l=0;/黄灯计时数据ucharr_h=1,r_l=0;/红灯计时数据#endif(2)some_inits.h#ifndef_some_inits_h#define_some_inits_h#include lcd_1602.h#include define.h/state some functionsvoid l1602_init();void time0_init();/*系统初始化*/void sys_init()flag=1;/绿灯green=1;red=0;yellow=0;l1602_init();/1602液晶初始化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初始化*/void time0_init()tmod=0x01;/time0,time1工作在方式一ie=0x8a;/开ea,允许et0,允许et1th0=0x4c;/50ms,晶振是11.0592mtl0=0x00;/th1=0xfc;/1ms/tl1=0x66;tr0=1;/time0开始计数#endif(3)control.h#ifndef _control_h_#define _control_h_#include #include lcd_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_();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 inc(void)_nop_();if(green=1 & yellow=0 & red=0)if(g_l9 & g_h5) g_l+;elseif(g_h5)g_h+;g_l=0;elseg_h=5;g_l=0; sec_h=g_h;sec_l=g_l;else if(yellow=1 & red=0 & green=0)if(y_l9 & y_h5) y_l+;elseif(y_h5)y_h+;y_l=0;elsey_h=5;y_l=0; sec_h=y_h;sec_l=y_l; else if(red=1 & green=0 & yellow=0 )if(r_l9 & r_h5) r_l+;elseif(r_h0 & g_h0) g_l-;elseif(g_h0)g_h-;g_l=9;else if(g_l0)g_l-;elseg_h=0;g_l=0;sec_h=g_h;sec_l=g_l;else if(yellow=1 & green=0 & red=0) if(y_l0 & y_h0) y_l-;elseif(y_h0)y_h-;y_l=9;else if(y_l0)y_l-;elsey_h=0;y_l=0;sec_h=y_h;sec_l=y_l; else if(red=1 & yellow=0 & green=0)if(r_l0 & r_h0) r_l-;else if(r_h0)r_h-;r_l=9;else if(r_l0) r_l-;elser_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 fmq()unint i=150;while(i-)bell=bell;delay_ms(1);/*time0 service function*/void time0() interrupt 1static count=0;tr0=0;if(count=20)/1scount=0;if(sec_l=0)if(sec_h=0)/30s结束if(flag=1)flag+;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;red=1;yellow=0;sec_h=r_h;sec_l=r_l; else/个位为0而已 sec_h-;sec_l=9;elsesec_l-;l1602_char(2,8,sec_h+0x30);l1602_char(2,9,sec_l+0x30);else count+; th0=0x4c;tl0=0x00;tr0=1;#endif(4)lcd_1602.h#ifndef_lcd_1602_h#define_lcd_1602_h#include define.h#include delay.huchar convert(uchar in_date) uchar i, out_date = 0, temp = 0; for(i=0; i i) & 0x01; out_date |= (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();/*写字符*/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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论