资源描述:
《单片机例子闪烁的led灯》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、1.闪烁的LED灯#include#defineucharunsignedchar#defineuintunsignedintsbitLED=P1^0;voidDelayMS(uintx){uchari;while(x--){for(i=120;i>0;i--);}}voidmain(){while(1){LED=~LED;DelayMS(150);}}2.从左到右的流水灯#include#include#defineucharunsignedchar#defineuintunsignedintvoidDelayMS(uin
2、tx){uchari;while(x--){for(i=120;i>0;i--);}}voidmain(){P0=0xFE;while(1){P0=_crol_(P0,1);DelayMS(150);}}3.左右来回的流水灯#include#include#defineucharunsignedchar#defineuintunsignedintvoidDelayMS(uintx){uchart;while(x--){for(t=120;t>0;t--);}}voidmain(){uchari;P2=0x01;while(1){for(i=7
3、;i>0;i--){P2=_crol_(P2,1);DelayMS(150);}for(i=7;i>0;i--){P2=_cror_(P2,2);DelayMS(150);}}}4.花样流水灯程序#include#defineucharunsignedchar#defineuintunsignedintucharcodePattern_P0[]={0xfc,0xf9,0xf3,0xe7,0xcf,0x9f};ucharcodePattern_P2[]={0xf5,0xf6,0xfe,0x54,0x56,0x76,0xd7,0x49,0xa9,0xe4,0xc6};
4、voidDelayMS(uintx){uchart;while(x--){for(t=120;t>0;t--);}}voidmain(){uchari;while(1){for(i=136;i>0;i--){P0=Pattern_P0[i];P2=Pattern_P2[i];DelayMS(150);}}}5.LED交通模拟灯#include#defineucharunsignedchar#defineuintunsignedintsbitRED_A=P0^0;sbitYELLOW_A=P0^1;sbitGREEN_A=P0^2;sbitRED_B=P0^3;sb
5、itYELLOW_B=P0^4;sbitGREEN_B=P0^5;ucharFlash_Count=0;Operation_Type=1;voidDelayMS(uintx){uchart;while(x--){for(t=120;t>0;t--);}}voidTraffic_lignt(){switch(Operation_Type){case1:RED_A=1;YELLOW_A=1;GREEN_A=0;RED_B=0;YELLOW_B=1;GREEN_B=1;DelayMS(2000);Operation_Type=2;break;case2:DelayMS(200);YELL
6、OW_A=~YELLOW_A;if(++Flash_Count!=10)return;Flash_Count=0;Operation_Type=3;break;case3:RED_A=0;YELLOW_A=1;GREEN_A=1;RED_B=1;YELLOW_B=1;GREEN_B=0;DelayMS(2000);Operation_Type=4;break;case4:DelayMS(200);YELLOW_B=~YELLOW_B;if(++Flash_Count!=10)return;Flash_Count=0;Operation_Type=1;break;}}voidmain
7、(){while(1){Traffic_lignt();}}6.单只数码管显示0到9#include#include#defineucharunsignedchar#defineuintunsignedintucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x82,0xf8,0x80,0x90,0xff};voidDelayMS(uintx){uchart;whil