收藏 分销(赏)

交通灯C程序.doc

上传人:仙人****88 文档编号:8890157 上传时间:2025-03-06 格式:DOC 页数:7 大小:52.50KB
下载 相关 举报
交通灯C程序.doc_第1页
第1页 / 共7页
交通灯C程序.doc_第2页
第2页 / 共7页
点击查看更多>>
资源描述
# include<reg51.h> # define uchar unsigned char # define disp_code P0 //把数据送到P0口 # define disp_sel P2 # define OFF 1 //灯灭 # define ON 0 //灯亮 sbit EW_green=P1^0; //东西绿 sbit EW_yellow=P1^1; //东西黄 sbit EW_red=P1^2; //东西红 sbit SN_green=P1^3; //南北绿 sbit SN_yellow=P1^4; //南北黄 sbit SN_red=P1^5; //南北红 bit sign; //定义一个量来判断是东西还是南北通行 uchar time_c1,time_c2; int count_down=30; uchar time [2]; uchar disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数字代码 uchar select[]={0xfe,0xfd}; //数码管段选码 void delay(uchar n) //延时程序 { uchar i,j,k; for(i=0;i<n;i++) for(j=0;j<2;j++) for(k=0;k<254;k++); } void T_countdown() interrupt 1 using 1 //定时器1中断50ms { TH0=(65536-50000)/256; TL0=(65536-50000)%256; time_c1++; if(time_c1==20) //20次计数 { time_c1=0; count_down--; if(count_down<0) count_down=30; if(count_down>=7) { if(sign) //判断sign是1南北通行 { EW_red=OFF; EW_green=ON; SN_yellow=OFF; SN_red=ON; } else // 否者东西通行 { EW_red=ON; EW_yellow=OFF; SN_green=ON; SN_red=OFF; } } else if(count_down<7&&count_down>3) //在7S到3S时绿灯闪烁 { if(sign) { EW_green=!EW_green; SN_red=ON; } else { SN_green=!SN_green; EW_red=ON; } } else if (count_down<=3&&count_down>=0)//在3S到0S时黄灯亮 { if(sign) { EW_green=OFF; EW_yellow=ON; SN_red=ON; } else { SN_green=OFF; SN_yellow=ON; EW_red=ON; } } if(count_down==0) sign=! sign; } } /*--- 倒计时间显示---*/ void display () interrupt 3 using 3 //定时器1,10mS中断 { TH1=(65536-10000)/256; TL1=(65536-10000)%256; time[0]=count_down/10; time[1]=count_down%10; time_c2++; if(time_c2==2) time_c2=0; disp_code=disp[time[time_c2]]; disp_sel=select[time_c2]; } /*---main program---*/ void main( ) { delay(10); delay(10); delay(10); time_c1=0; time_c2=0; sign=0; EA=1; //开总中断 ET0=1; //开中断0 ET1=1; //开中断1 TMOD=0x11; //设置定时器1工作及工作方式1 TH0=(65536-50000)/256; //定时器0定时50mS TL0=(65536-50000)%256; TH1=(65536-10000)/256; //定时器1定时10mS TL1=(65536-10000)%256; TR0=1; //开T0中断 TR1=1; //开T1中断 while (1); //循环等待中断 }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服