資源描述:
《pwm控制led亮度2321321》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、1242132132125351421321213213213213213PWM控制LED亮度PS:只是拿了先成的一個板子,帶下載口的,拉了8個I/O口出來,接了8個LED燈線路弄的不好,請大家見諒編寫環(huán)境:GCC20080512使用了一個計數(shù)器中斷圖片和視頻不是很清楚,希望大家諒解(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)//計數(shù)器溢出中斷{TCNT0=16;timecount++;timecount2++;if(timecount>=320)//PWM周期{PORTD
3、=0xff;//關(guān)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)//計數(shù)器溢出中斷{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;vola
10、tileunsignedcharadddir_FLAG=1;unsignedinti=0;SIGNAL(SIG_OVERFLOW0)//計數(shù)器溢出中斷{TCNT0=16;timecount++;timecount2++;if(timecount2>=2000)//每隔60毫秒更新下數(shù)組,即改變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)//達(dá)到對應(yīng)調(diào)節(jié)到的占空比時{PORTD=0xff;//關(guān)}}intmain(void){DDRD=0xff;PORTD
12、=0xff;TCCR0=0x01;TIMSK=0x01;cli();sei();//使能全局中while(1){}}/*8個燈從第一個開始依次漸亮,直到最后一個。再從最后一個起漸暗,直到第一個。如此循
13、環(huán)往復(fù)。*/#include#includevolatilesignedinttimecount=0;volatilesignedlongtimecount2=0;unsignedinti=0;unsignedintj=0;unsignedintm=0;volatileunsignedcharadddir_FLAG=1;volatileintLED_code[]={0,-40,-80,-120,-160,-200,-240,-280};//每