Brief introduction of PWM
pwm--Pulse width modulated signal (pulse width modulation), which is realized by microprocessor digital output, is a very effective technology for analog circuit control, which is widely used in many fields such as measurement, communication, power control and change.
Led effect Breathing Light principle
In the way of PWM, the change of LED brightness is realized by means of duty ratio at fixed frequency. The duty ratio for the 0,led lamp is not lit, the duty ratio is 100%, the LED light is the brightest. Therefore, the duty ratio from the 0到100%, and then from 100% to 0 constantly changing, you can realize the LED light to achieve special effects breathing.
Design ideas, Block diagram
Lighten: High when Cnt2 < Cnt3, vice versa low.
Dimming: Low when Cnt2 < Cnt3, vice versa High.
The time delay is 1us delay circuit, as the driving circuit behind the circuit, then 1ms delay and 1s delay, will produce two counters, just 1000 times times the relationship.
(1) In the first second, with the Cnt3 from 0 to 999 gradually increased, each 1ms duty ratio by Cnt2 to adjust. When Cnt2 < Cnt3 is high, the reverse is low. Therefore, as the cnt3 gradually increases, the time of the high level increases gradually until 100%.
(2) in the second second, with the Cnt3 from 0 to 999 gradually increased, each 1ms duty ratio by Cnt2 to adjust. Low-level when Cnt2 < Cnt3, and vice versa high. Thus, as the cnt3 gradually increases, the time of the high level decreases gradually until 0.
The design diagram as shown above, after the completion of the construction of the picture, attached to some code.
1 //Delay 1us2 always@(PosedgeMclkor Negedgerst_n)3 begin4 if(!rst_n)5Cnt1 <=6'B0;6 Else if(Cnt1 < STOP_50-1'B1)7Cnt1 <= Cnt1 +1'B1;8 Else 9Cnt1 <=6'B0;Ten End One AssignDelay_1us = (Cnt1 = = stop_50-1'B1)? 1'B1:1'B0; A - //Delay 1ms - always@(PosedgeMclkor Negedgerst_n) the begin - if(!rst_n) -Cnt2 <=Ten'B0; - Else if(Delay_1us) + begin - if(Cnt2 < stop_1000-1'B1) +Cnt2 <= Cnt2 +1'B1; A Else atCnt2 <=Ten'B0; - End - End - AssignDelay_1ms = ((Delay_1us = =1'B1) && (Cnt2 = = Stop_1000-1'B1))?1'b1:1'B0; - - //delay 1s in always@(PosedgeMclkor Negedgerst_n) - begin to if(!rst_n) +Cnt3 <=Ten'B0; - Else if(delay_1ms) the begin * if(Cnt3 < stop_1000-1'B1) $Cnt3 <= Cnt3 +1'B1;Panax Notoginseng Else -Cnt3 <=Ten'B0; the End + A End the AssignDelay_1s = ((delay_1ms = =1'B1) && (Cnt3 = = Stop_1000-1'B1))?1'b1:1'B0; + - // State Change $ always@(PosedgeMclkor Negedgerst_n) $ begin - if(!rst_n) -Display_state <=1'B0; the Else if(delay_1s)//switch the LED light display status every second -Display_state <= ~display_state;Wuyi Else theDisplay_state <=display_state; - End Wu - //generation of PWM signals About always@(PosedgeMclkor Negedgerst_n) $ begin - if(!rst_n) -PWM <=1'B0; - Else A Case(display_state) + 1'B0:PWM <= (Cnt2 < Cnt3)? 1'B1:1'B0; the 1'B1:PWM <= (Cnt2 < Cnt3)? 1'B0:1'B1; - default: PWM <=PWM; $ Endcase the End the the //bit stitching makes the output eight-bit led breathing lamp the AssignLed_out = {8{PWM}}; -
The resulting PWM signal turns out to be one, so if you need to control the lighting of multiple LEDs at the end, use a bit concatenation operation, as shown in the last line of code.
PWM (pulse width modulation)--led Special effect breathing lamp design