欢迎来到天天文库
浏览记录
ID:24621258
大小:65.69 KB
页数:3页
时间:2018-11-15
《《片机红绿灯程序》word版》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、/*-----------------------------------------------名称:红绿灯------------------------------------------------*/#include//包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义bitred,green,yellow,turnred;//定义红绿黄灯标志位codeunsignedchartab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管0-9unsignedcha
2、rDis_Shiwei;//定义十位unsignedcharDis_Gewei;//定义个位/******************************************************************//*延时函数*//******************************************************************/voiddelay(unsignedintcnt){while(--cnt);}/**********************************************************
3、********//*主函数*//******************************************************************/main(){TMOD
4、=0x01;//定时器设置10msin12McrystalTH0=0xd8;TL0=0xf0;IE=0x82;//打开中断TR0=1;P1=0xfc;//红灯亮red=1;while(1){P0=Dis_Shiwei;//显示十位P2=0;delay(300);//短暂延时P0=Dis_Gewei;//显示个位P2=1;delay(300);}}/******************
5、************************************************//*定时器中断函数*//******************************************************************/voidtim(void)interrupt1using1{staticunsignedcharsecond=60,count;//初值99TH0=0xd8;//重新赋值TL0=0xf0;count++;if(count==100){count=0;second--;//秒减1if(second==0){//这里添加定时
6、到0的代码,可以是灯电路,继电器吸合等,或者执行一个程序if(red){red=0;yellow=1;second=5;P1=0xF3;//黄灯亮5秒}elseif(yellow&&!turnred){yellow=0;green=1;second=50;P1=0xCF;//绿灯亮50秒}elseif(green){yellow=1;green=0;second=5;P1=0xF3;//黄灯亮5秒turnred=1;}elseif(yellow&&turnred){red=1;yellow=0;P1=0xFC;//红灯亮60秒second=60;turnred=0;}}D
7、is_Shiwei=tab[second/10];//十位显示值处理Dis_Gewei=tab[second%10];//个位显示处理}}
此文档下载收益归作者所有