资源描述:
《单片机交通灯c程序 只有红绿灯 和数码显示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、参考《51单片机C语言创新教程》温子祺等著。源码转自:《51单片机C语言创新教程》。/*实验名称:交通灯实验*描述:交通灯实验要求红灯亮15秒,绿灯亮10秒,黄灯亮5秒,当红灯切换为绿灯或者绿灯切换为红灯,要实现灯闪烁。红灯、绿灯、黄灯的点亮持续时间可以通过串口来修改,并在下一个循环中更新数值。*作者:温子祺*修改日期:2010/5/4*说明:代码注释与讲解详见《51单片机C语言创新教程》温子祺等著,北京航空航天大学出版社*/#include"stc.h"typedefunsignedcharUINT8;typedefunsignedintUINT16
2、;typedefunsignedlongUINT32;typedefcharINT8;typedefintINT16;typedeflongINT32;#defineTIMER0_INITIAL_VALUE5000#defineHIGH1#defineLOW0#defineON1#defineOFF0#defineSEG_PORTP0#defineLS164_DATA(x){if((x))P0_4=1;elseP0_4=0;}#defineLS164_CLK(x){if((x))P0_5=1;elseP0_5=0;}#defineNORTH_R_LIG
3、HT(x){if((x))P2_0=0;elseP2_0=1;}#defineNORTH_Y_LIGHT(x){if((x))P2_1=0;elseP2_1=1;}#defineNORTH_G_LIGHT(x){if((x))P2_2=0;elseP2_2=1;}#defineSOUTH_R_LIGHT(x){if((x))P2_3=0;elseP2_3=1;}#defineSOUTH_Y_LIGHT(x){if((x))P2_4=0;elseP2_4=1;}#defineSOUTH_G_LIGHT(x){if((x))P2_5=0;elseP2_5=
4、1;}#defineTRAFFIC_STATUS_10#defineTRAFFIC_STATUS_21#defineTRAFFIC_STATUS_32#defineUART_MARKER0xEEUINT8Timer0IRQEvent=0;UINT8Time1SecEvent=0;UINT8Time500MsEvent=0;UINT8TimeCount=0;UINT8SegCurPosition=0;UINT8LightOrgCount[4]={15,5,15,5};UINT8LightCurCount[4]={15,5,15,5};UINT8Traff
5、icLightStatus=0;codeUINT8SegCode[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};UINT8SegBuf[4]={0};codeUINT8SegPosition[4]={0x07,0x0b,0x0d,0x0e};typedefstruct_LIGHT_VAL{UINT8Head;UINT8val[4];}LIGHT_VAL;typedefunion_LIGHT_VAL_EX{LIGHT_VALlv;UINT8p[5];}LIGHT_VAL_EX;voidLS
6、164Send(UINT8byte){UINT8j;for(j=0;j<=7;j++){if(byte&(1<<(7-j))){LS164_DATA(HIGH);}else{LS164_DATA(LOW);}LS164_CLK(LOW);LS164_CLK(HIGH);}}voidRefreshDisplayBuf(UINT8s1)//刷新显示缓存{SegBuf[0]=s1%10;SegBuf[1]=s1/10;SegBuf[2]=s1%10;SegBuf[3]=s1/10;}voidSegDisplay(void){UINT8t;t=SegCode[
7、SegBuf[SegCurPosition]];SEG_PORT
8、=0x0f;LS164Send(t);SEG_PORT=(SEG_PORT
9、0x0f)&SegPosition[SegCurPosition];if(++SegCurPosition>=4){SegCurPosition=0;}}voidTimerInit(void){TH1=0;TL1=0;TH0=(65536-TIMER0_INITIAL_VALUE)/256;TL0=(65536-TIMER0_INITIAL_VALUE)%256;//定时1MSTMOD=0x51;/*010100
10、01T1计数,T0定时*/}voidTimer0Start(void){TR0=1;//启动计