By changing the duty cycle of the PWM signal we can control the brightness of the LED. The brightness is varied from 0-10
Here's the circuit diagram and the C code
[code:c]/*Program of 8051 single-chip pulse width modulation (PWM) brightness control of LED lights, as we all know 8051 does not have an pwm interface on it. This process is simulated by software PWM. The adjustment of high and low duty cycle of the square wave frequency can be realized to control LED brightness*/
#include "AT89X51.H" //simulation of PWM output control lamp brightness level of 10
unsigned int scale; //duty cycle control variable
void main(void) // main program
{ unsigned int n; //delay cycle variables
TMOD=0x02; //timer 0, mode 2 (0000,0010), 8-bit timer mode
TH0=0x06; //write the timer 0 preset initial value, so that an overflow of 250 microseconds (12MHz)
TL0=0x06; //written into the preset value
TR0=1; //start timer
ET0=1; //allow timer 0 interrupt
EA=1; //Enable global interrupt
while(1) //infinite loop, the main work is done here
{ for(n=0;n<50000;n++); //every time, automatically increases the brightness of a grade
scale++; //duty cycle control variable scale plus 1
if(scale==10) scale=0; //if the scale = 10, so that scale is 0
} }
timer0() interrupt 1 //Timer 0 interrupt service routine
{ static unsigned int tt ; //tt used to save the current time in a second location in the proportion of
tt++; //microsecond increase of 1 per 250
if(tt==10) //2.5 ms cycle clock
{ tt=0; //so that tt = 0, start a new cycle of the PWM
P3_0=0; //make LED lights ON
}
if(scale==tt) //in accordance with the present duty cycle output for high switching
P3_0=1; //make LED lights OFF
}
/*Process tt = 0 from the beginning to the low scale from the scale to the tt = 10 for high,
No comments:
Post a Comment