欢迎来到天天文库
浏览记录
ID:14178623
大小:56.50 KB
页数:9页
时间:2018-07-26
《pwm控制led亮度2321321》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1242132132125351421321213213213213213PWM控制LED亮度PS:只是拿了先成的一个板子,带下载口的,拉了8个I/O口出来,接了8个LED灯线路弄的不好,请大家见谅编写环境:GCC20080512使用了一个计数器中断图片和视频不是很清楚,希望大家谅解(1)/*第1,3,5,7个灯半亮,其余的全亮。*/#include#includevolatilesignedinttimecount=0;volatilesi
2、gnedlongtimecount2=0;unsignedintj=0;constintLED_code[]={160,0,160,0,160,0,160,0};SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount>=320)//PWM周期{PORTD
3、=0xff;//关timecount=0;}for(j=0;j<=7;j++){if(LED_code[j]<=timecount)//读取占空
4、比值{PORTD&=~(1<5、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*第1个亮1/8,第2个亮2/8,第3个亮3/8,第4个亮4/8,第5个亮5/8,第6个亮6/8,第7个亮7/8,第8个全亮。*/#include#includevolatilesignedinttimecount=0;6、volatilesignedlongtimecount2=0;unsignedintj=0;constintLED_code[]={280,240,200,160,120,80,40,0};SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount>=320)//PWM周期{PORTD7、=0xff;timecount=0;}for(j=0;j<=7;j++)//依次比较读取{if(LED_code[j8、]<=timecount){PORTD&=~(1<9、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*所有的灯逐渐从暗到全亮,再由全亮到暗。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;vola10、tileunsignedcharadddir_FLAG=1;unsignedinti=0;SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount2>=2000)//每隔60毫秒更新下数组,即改变PWM的占空比{if(i<=0){adddir_FLAG=1;}if(i>=48){adddir_FLAG=0;}if(adddir_FLAG==1){i++;}if(adddir_FLAG==0){i--11、;}timecount2=0;}if(timecount>=48)//PWM的周期{PORTD=0x00;//周期初始开timecount=0;}if(timecount>=i)//达到对应调节到的占空比时{PORTD=0xff;//关}}intmain(void){DDRD=0xff;PORTD12、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*8个灯从第一个开始依次渐亮,直到最后一个。再从最后一个起渐暗,直到第一个。如此循13、环往复。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;unsignedinti=0;unsignedintj=0;unsignedintm=0;volatileunsignedcharadddir_FLAG=1;volatileintLED_code[]={0,-40,-80,-120,-160,-200,-240,-280};//每
5、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*第1个亮1/8,第2个亮2/8,第3个亮3/8,第4个亮4/8,第5个亮5/8,第6个亮6/8,第7个亮7/8,第8个全亮。*/#include#includevolatilesignedinttimecount=0;
6、volatilesignedlongtimecount2=0;unsignedintj=0;constintLED_code[]={280,240,200,160,120,80,40,0};SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount>=320)//PWM周期{PORTD
7、=0xff;timecount=0;}for(j=0;j<=7;j++)//依次比较读取{if(LED_code[j
8、]<=timecount){PORTD&=~(1<9、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*所有的灯逐渐从暗到全亮,再由全亮到暗。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;vola10、tileunsignedcharadddir_FLAG=1;unsignedinti=0;SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount2>=2000)//每隔60毫秒更新下数组,即改变PWM的占空比{if(i<=0){adddir_FLAG=1;}if(i>=48){adddir_FLAG=0;}if(adddir_FLAG==1){i++;}if(adddir_FLAG==0){i--11、;}timecount2=0;}if(timecount>=48)//PWM的周期{PORTD=0x00;//周期初始开timecount=0;}if(timecount>=i)//达到对应调节到的占空比时{PORTD=0xff;//关}}intmain(void){DDRD=0xff;PORTD12、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*8个灯从第一个开始依次渐亮,直到最后一个。再从最后一个起渐暗,直到第一个。如此循13、环往复。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;unsignedinti=0;unsignedintj=0;unsignedintm=0;volatileunsignedcharadddir_FLAG=1;volatileintLED_code[]={0,-40,-80,-120,-160,-200,-240,-280};//每
9、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*所有的灯逐渐从暗到全亮,再由全亮到暗。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;vola
10、tileunsignedcharadddir_FLAG=1;unsignedinti=0;SIGNAL(SIG_OVERFLOW0)//计数器溢出中断{TCNT0=16;timecount++;timecount2++;if(timecount2>=2000)//每隔60毫秒更新下数组,即改变PWM的占空比{if(i<=0){adddir_FLAG=1;}if(i>=48){adddir_FLAG=0;}if(adddir_FLAG==1){i++;}if(adddir_FLAG==0){i--
11、;}timecount2=0;}if(timecount>=48)//PWM的周期{PORTD=0x00;//周期初始开timecount=0;}if(timecount>=i)//达到对应调节到的占空比时{PORTD=0xff;//关}}intmain(void){DDRD=0xff;PORTD
12、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*8个灯从第一个开始依次渐亮,直到最后一个。再从最后一个起渐暗,直到第一个。如此循
13、环往复。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;unsignedinti=0;unsignedintj=0;unsignedintm=0;volatileunsignedcharadddir_FLAG=1;volatileintLED_code[]={0,-40,-80,-120,-160,-200,-240,-280};//每
此文档下载收益归作者所有