赞
踩
采用定时器1,工作方式2,软件启动。实现下述功能:有8个LED小灯,首先8个灯同时亮1s,灭0.5s,重复3次;然后从第一个灯开始依次点亮,实现流水灯功能,每个灯亮2s;往复循环上述功能。
#include<reg51.h> //采用定时器1工作方式2实现1s定时 void delay1s( ) { unsigned int i; TMOD=0x20; TH1=256-250; //250us,因51单片机频率12Mhz,一个机器周期1us,计数250次即为250微秒 TL1=256-250; for(i=0;i<4000;i++) //循环4000次即为250微秒X4000=1s { TR1=1; while(!TF1); TF1=0; } } //采用定时器1工作方式2实现0.5s定时,原理同上 void delay500ms( ) { unsigned int i; TMOD=0x20; TH1=256-250; TL1=256-250; for(i=0;i<2000;i++) { TR1=1; while(!TF1); TF1=0; } } void main() { unsigned char j,k,w; while(1) { for(j=0;j<3;j++)//8个灯同时闪烁三次,闪烁时亮1s,灭0.5s { P1=0X00; delay1s( ); P1=0Xff; delay500ms( ); } //实现8个LED循环点亮,点亮时间为2s w=0x01; for(k=0;k<8;k++) { P1=~w; delay1s( ); delay1s( ); w<<=1; } } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。