基于单片机的交通控制系统源程序.doc

上传人:飞****2 文档编号:52198890 上传时间:2022-10-21 格式:DOC 页数:26 大小:288.50KB
返回 下载 相关 举报
基于单片机的交通控制系统源程序.doc_第1页
第1页 / 共26页
基于单片机的交通控制系统源程序.doc_第2页
第2页 / 共26页
点击查看更多>>
资源描述

《基于单片机的交通控制系统源程序.doc》由会员分享,可在线阅读,更多相关《基于单片机的交通控制系统源程序.doc(26页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、/*文件名:基于单片机的交通控制系统作 者:风雪子郁时 间: 2012/11/22邮 箱:fxziyu地 址:四川信息职业技术学院(雪峰校区)*/*液晶显示函数源程序*/#include #define LCD_databus P0 /LCD8 位数据口sbit RS=P22; /RS 为0 命令;1 数据sbit RW=P21; /RW 为1-写;0-读sbit EN=P20; /使能端sbit CS1=P24; /片选1 低电平有效,控制左半屏sbit CS2=P23; /片选1 低电平有效,控制右半屏控制右半屏unsigned char num;void Delay(unsigned i

2、nt t)unsigned int i,j;for(i=0;it;i+)for(j=0;j110;j+);void Read_busy()/读“忙”函数-数据线的最高位DB71 则busyP0=0x00;RS=0;RW=1;EN=1;while(P0 & 0x80);EN=0;void write_LCD_command(unsigned char value)Read_busy();/每次读写都要忙判断RS=0; /选择命令RW=0; /读操作LCD_databus=value;EN=1; /EN 由1-0 锁存有效数据_nop_();_nop_();EN=0;void write_LCD_

3、data(unsigned char value)/写数据函数Read_busy();RS=1; /选择数据RW=0;LCD_databus=value;EN=1; /EN 由1-0 锁存有效数据_nop_();_nop_();EN=0;void Set_page(unsigned char page)/设置“页”LCD12864 共8 页,一页是8 行点阵点page=0xb8|page; /页的首地址为0xB8write_LCD_command(page);void Set_line(unsigned char startline) /设置显示的起始行startline=0xC0|start

4、line; /起始行地址为0xC0write_LCD_command(startline); /设置从哪行开始:共0-63;一般从0 行开始显示void Set_column(unsigned char column) /设置显示的列column=column &0x3f; /列的最大值为64column= 0x40|column; /列的首地址为0x40write_LCD_command(column);/列位置void SetOnOff(unsigned char onoff) /显示开关函数:0x3E 是关显示,0x3F 是开显示onoff=0x3e|onoff; /0011 111x,

5、onoff只能为0 或者1write_LCD_command(onoff);void SelectScreen(unsigned char screen) /选屏switch(screen)case 0: CS1=0;CS2=0;break; /全屏case 1: CS1=0;CS2=1;break;/左半屏case 2: CS1=1;CS2=0;break;/右半屏default:break;void ClearScreen(unsigned char screen) /清屏unsigned char i,j;SelectScreen(screen);for(i=0;i8;i+)/控制页数0

6、-7,共8 页Set_page(i);Set_column(0);for(j=0;j64;j+) /控制列数0-63,共64 列write_LCD_data(0x00); /写入0,地址指针自加1void InitLCD() /LCD 的初始化Read_busy();SelectScreen(0);SetOnOff(0); /关显示SelectScreen(0);SetOnOff(1); /开显示SelectScreen(0);ClearScreen(0);/清屏Set_line(0); /开始行:0void Display(unsigned char ss,unsigned char pag

7、e,unsigned char column,unsigned char *p) /显示汉字unsigned char i;SelectScreen(ss);Set_page(page); /写上半页Set_column(column); /控制列for(i=0;i16;i+) /控制16 列的数据输出write_LCD_data(pi); /汉字的上半部分Set_page(page+1); /写下半页Set_column(column); /控制列for(i=0;i16;i+) /控制16 列的数据输出write_LCD_data(pi+16);/汉字的下半部分void DisplayNC(

8、unsigned char ss,unsigned char page,unsigned char column,unsigned char *p) /显示数字或字符unsigned char i;SelectScreen(ss);Set_page(page); /写上半页Set_column(column); /控制列for(i=0;i8;i+) /控制16 列的数据输出write_LCD_data(pi); /汉字的上半部分Set_page(page+1); /写下半页Set_column(column); /控制列for(i=0;i8;i+) /控制16 列的数据输出write_LCD_

9、data(pi+8);/汉字的下半部分void display_BMP(unsigned char a64)/显示图形的左边部分unsigned char i,j;for(j=0;j8;j+)SelectScreen(1);Set_page(j);Set_column(0);for(i=0;i64;i+) /显示左屏write_LCD_data(a2*ji);/每隔一行取一次数组中的数据SelectScreen(2);Set_page(j);Set_column(0);for(i=0;i64;i+) /显示右屏write_LCD_data(a2*j+1i);/每隔一行取一次数组中的数据/*主机

10、源程序*/#include unsigned char code duan=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/段选码unsigned char model=0;/三机通信变量unsigned char temp=0,tab=0;/通行状态模式unsigned char time74;void init()/初始化函数TMOD=0x21;TH0=(65536-50000)/256;TL0=(65536-50000)%256;ET0=1; TH1 = 0xfd;TL1 = 0xfd;TR1 = 1;SCON = 0x50;PCON

11、 = 0x00;ES = 1; EA = 1;void sysint()/时间初值/东西停,南北通time00=time02=30;/东西倒计时长time01=time03=25;/南北倒计时长/东西停,南北等time10=time12=5;/东西倒计时长time11=time13=5;/南北倒计时长/东西通,南北停,time20=time22=25;/东西倒计时长time21=time23=30;/南北倒计时长/东西等,南北停,time30=time32=5;/东西倒计时长time31=time33=5;/南北倒计时长void sysint2()/特殊情况时间初值unsigned char

12、j,k;for(j=4;j=6;j+)for(k=0;k4;k+)timejk=10;void delay() /延时unsigned char j=100;while(j-);void seri(unsigned char i)/数据发送SBUF=i;delay();while(!TI);TI=0;void seg()/正常交通数码管显示unsigned char i=0x01,n;for(n=0;n4;n+)P2=i;P0=duantimetempn/10;delay();i=1;P2=i;P0=duantimetempn%10;delay();i=1;void seg2()/特殊情况数码

13、管显示unsigned char i=0x01,n;for(n=0;n4;n+)P2=i;P0=duantimetabn/10;delay();i=1;P2=i;P0=duantimetabn%10;delay();i=1;void zcjt() /正常交通switch(temp)case 0: seri(0xa0);P1=0x21;break;/东西停,南北通case 1: P1=0x11;break; /东西停,南北等case 2:seri(0xa1);P1=0x0c;break; /东西通,南北停case 3:P1=0x0a;break;/东西等,南北停default : break;v

14、oid main() unsigned char tempp,tabb;init();while(!model) delay();sysint();sysint2();TR0=1;while(1)tabb=tab;tempp=temp;switch(tab)case 0: zcjt();break;/正常交通case 1: P1=0x09;break;/禁止通行case 2: P1=0x21;break;/东西停,南北通case 3: P1=0x0c;break;/东西通,南北停default : break;while(tempp=temp & !tab) seg(); while(tabb

15、=tab & tab) seg2();void t0() interrupt 1 /1s函数中断unsigned char a,n;EA=0;TH0=(65536-50000)/256;TL0=(65536-50000)%256;a+;/是否满1sif(a=24)a=0;for(n=0;n4;n+)if(!tab)if(timetempn)timetempn-;elsetemp+;if(temp=4) temp=0;sysint();break;elseif(timetabn)timetabn-;elsetab=0;sysint2();break;EA=1;void serial() inte

16、rrupt 4/数据接收中断EA = 0;delay();while(!RI);RI = 0;model = SBUF;switch(model)case 0xb1 : tab=0;break;case 0xb2 : tab=4;sysint2();break;case 0xb3 : tab=5;sysint2();break;case 0xb4 : tab=6;sysint2();break;default : break;EA = 1;/*从机1源程序*/#include #include /lcd驱动函数#include /中文字库sbit bj=P25;/led报警sbit soud=

17、P26;/蜂鸣报警unsigned char model=0;/三机通信变量unsigned char temp=0,num=0,tab=1,key=0;void init()TMOD=0x21;TH1 = 0xfd;TL1 = 0xfd;TR1 = 1;SCON = 0x50;PCON = 0x00;ES = 1;EA = 1;void delay() /延时unsigned char j=100;while(j-);void seri(unsigned char i)/数据发送SBUF=i;delay();while(!TI);TI=0;void Screen_Sys()Display(1

18、,1,1,xi);Display(1,1,18,tong);Display(1,1,34,qi);Display(1,1,50,dong);Display(2,1,1,zhong);Display(1,4,18,qing);Display(1,4,34,shao);Display(1,4,50,hou);Delay(1000);DisplayNC(2,1,48,num3);Delay(1000);Display(2,4,4,xingh);DisplayNC(2,1,48,num2);Delay(1000);Display(2,4,21,xingh);DisplayNC(2,1,48,num1)

19、;Delay(1000);Display(2,4,37,xingh);DisplayNC(2,1,48,num0);Delay(1000);void anjian()/按键处理if(P1 != 0xff)delay();if(P1 != 0xff)key=P1;key=key;switch(key)case 0x01:temp=0xb1;break;case 0x02:temp=0xb2;break;case 0x04:temp=0xb3;break;case 0x08:temp=0xb4;break;case 0x10:num=0xb5;break;case 0x20:num=0xb6;br

20、eak;case 0x40:num=0xb7;break;case 0x80:num=0xb8;break;if(key0x08)tab=0;seri(num); void zhishi()/交通模式示意switch(temp)case 0xa0 : case 0xa1 : case 0xb1 : display_BMP(jtzc);break;case 0xb2 : display_BMP(jztx);break;case 0xb3 :display_BMP(jj1);break;case 0xb4 : display_BMP(jj2);break;default : break;void

21、biaoyu() /标语,广告及提示模式unsigned char i,keyy;switch (num)case 0xc0: case 0xc1: case 0xc2: case 0xc3: display_BMP(jb_chd); break; /闯红灯case 0xc4: case 0xc5: case 0xc6: case 0xc7: display_BMP(jb_qt); break; /桥塌case 0xc8: case 0xc9: case 0xca: case 0xcb: display_BMP(jb_hz); break; /火灾case 0xcc: case 0xcd: c

22、ase 0xce: case 0xcf: display_BMP(jb_ch); break; /车祸 case 0xb5: case 0xb6: case 0xb7:case 0xb8: display_BMP(xcbiaoyu); break; /宣传标语default: break;tab=1;if(num=0xc0 & num=0xcf)soud=1;bj=0;key=keyy;for(i=0;i=0xc0 & model= 0xcf) tab=0;num=model; EA = 1;/*从机2源程序*/#include #include /lcd驱动函数#include /中文字库u

23、nsigned char code key_char44=0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,;unsigned char model=0;/三机通信变量unsigned char temp=0,key=0;tab=1;void init()/初始化TMOD = 0x21;TH1 = 0xfd;TL1 = 0xfd;TR1 = 1;SCON = 0x50;PCON = 0x00;ES = 1;EA = 1;P1=0x0f;void delay() /延时unsigned

24、char j=100;while(j-);void seri(unsigned char i)/数据发送SBUF=i;delay();while(!TI);TI=0;void jzjp()/键盘扫描 unsigned char hang,lie;if(P1!=0x0f) delay();if(P1!=0x0f)switch(P1&0x0f)case 0x0e:lie=0;break;case 0x0d:lie=1;break;case 0x0b:lie=2;break;case 0x07:lie=3;break;default: break;P1=0xf0;switch(P1&0xf0)cas

25、e 0xe0:hang=0;break;case 0xd0:hang=1;break;case 0xb0:hang=2;break;case 0x70:hang=3;break;default: break; P1=0x0f;key=key_charhanglie;tab=0;/道路情况void ew_g_ns_s()/东西通,南北停display_BMP(tra_pol_3);Delay(200);jzjp();display_BMP(tra_pol_4);Delay(200);jzjp();void ew_s_ns_g()/东西停,南北通display_BMP(tra_pol_1);Del

26、ay(200);jzjp();display_BMP(tra_pol_2);Delay(200);jzjp();void stop()/禁止通行display_BMP(stop_1);Delay(200);jzjp();display_BMP(stop_2);Delay(200);jzjp();void zhishi()/交通示意模式switch (temp)case 0xa0: ew_g_ns_s(); break; case 0xa1: ew_s_ns_g(); break;case 0xb2: stop(); break;case 0xb3: ew_g_ns_s();break;case

27、 0xb4: ew_s_ns_g();break;default: break;void biaoyu() /标语,广告及提示模式switch (key)case 0xc0: case 0xc1: case 0xc2: case 0xc3: display_BMP(cj2_chd); break; /闯红灯case 0xc4: case 0xc5: case 0xc6: case 0xc7: display_BMP(cj2_qt); break; /桥塌case 0xc8: case 0xc9: case 0xca: case 0xcb: display_BMP(cj2_hz); break;

28、 /火灾case 0xcc: case 0xcd: case 0xce: case 0xcf: display_BMP(cj2_ch); break; /车祸case 0xb5: display_BMP(cj2_by1); break; /标语1 case 0xb6: display_BMP(cj2_by2); break; /标语2 case 0xb7: display_BMP(cj2_by3); break; /标语3case 0xb8: display_BMP(cj2_by4); break; /标语4default: break;if(key =0xc0 | key =0xcf) se

29、ri(key);Delay(2000);tab=1;void main()init();InitLCD(); display_BMP(name);while(1)if(tab) zhishi();elsebiaoyu(); void serial() interrupt 4EA = 0;delay();while(!RI);RI = 0;model = SBUF;switch (model)case 0xa0: case 0xa1: case 0xb2: case 0xb3: case 0xb4: temp=model; break; case 0xb5:case 0xb6:case 0xb7: case 0xb8: tab=0;key=model; break;default: break;EA = 1;/” ziku.h”为液晶字模代码文件,在此略

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁