FPGA Learning Series: 15. Breath Light (pwm) Design

design background:

Breathing lights are widely used in mobile phones and have become one of the selling points for the new brands of mobile phones. If the phone there are unprocessed notifications, such as missed calls, text messages, and so did not check, breathing light will be completed gradually changed from light to dark under control, I feel like people breathe, play a notification The role of reminders.

 

Design principle :  

The theory of breathing lamp design is mainly PWM knowledge. PWM ( Pluse Width Modulation ) Pulse Width Modulation is a method of digitally encoding an analog signal level. Through the use of a high resolution counter, the duty cycle of the square wave is modulated to encode the level of a particular analog signal. It is widely used in many fields such as measurement, communication, power control and conversion, and LED lighting. As the name suggests, it is a signal with an adjustable duty cycle. What is the duty cycle?

The duty cycle ( Duty Cycle or Duty Ratio ) can be interpreted as the ratio of the duration of a positive pulse sequence to the total pulse period in a pulse sequence (square wave). It can also be understood as the ratio of the effective time of release of energy by the circuit to the total release time.

How does PWM achieve dimming? To adjust the brightness variation of the LED , it is actually to regulate the current flowing through the LED . When the current increases, the brightness of the LED increases and vice versa. However, since the current is an analog signal, PWM is used at this time . As shown in the figure below:

image.png

Instead of a sine wave, a series of pulses of unequal amplitude is used. The width of the pulse varies according to the amplitude of the sine wave a . The amplitude is high and the pulse width is the opposite.     

The PWM modulation frequency required for most loads is higher than 10Hz . To achieve the effect of a breathing lamp , the modulation frequency must be increased . Usually the modulation frequency is between 1Khz and 200Khz . In the LED control, the PWM acts on the power supply. The pulse width modulation pulse frequency is usually greater than 100Hz , and the human eye will not feel flicker. Here we take the PWM modulation frequency of 1KHz and the PWM period is 1ms .

When the pulse frequency is constant, the greater the duty cycle of the output pulse, the higher the effective level of the output, and the brightness of the LED will be different depending on the duty cycle . If the duty cycle is 0 , then the LED does not light, when it is 100% , the LED is the most. We let the duty cycle change from 0 to 100% , and then change from 100% to 0 , then we can realize the breathing lamp. effect.

The design of a respiratory cycle of the lamp for 2s, the duty cycle divided by "suction" and the duty Save "call" modes, each of 1s, a PWM cycle of 2 ms, so that each pattern contains 1000 The PWM cycle divides each PWM cycle into 1000 copies, which is 2 us per time period .

 

Design architecture diagram :

Clock 50M

image.png

Design code :

Design module

0    Module huxi_led_state ( clk , led , rst_n );

1    Input clk ;

2    Input rst_n ;

3   

4    Output   Reg led ;

5   

6    Parameter T =   100_000 ;

7   

8    Localparam s0 =   1'b0 ;

9    Localparam s1 =   1'b1 ;

10  

11   Reg   [ 25 : 0 ] lw ;

12   Reg   [ 25 : 0 ] hw ;

13  

14   Reg   [ 16 : 0 ] count ;

15  

16   // Generate 2MS pulses

17   Always   @( posedge clk or   negedge rst_n)

18   If (! rst_n )

19   Begin

20   Count <=   1'b0 ;

twenty one   End

twenty two   Else

twenty three   Begin

twenty four   If ( count == T -   1 )

25   Begin

26   Count <=   1'b0 ;

27   End

28   Else

29   Begin

30   Count <= count +   1'b1 ;

31   End

32   End

33  

34   wire flag;

35   Assign flag =( count == T -   1 )   ?   1'b1 : 1'b0 ;

36  

37   Reg state ;

38  

39   // Generate PWM waves by adding or subtracting high and low levels in one cycle

40   Always   @( posedge clk or   negedge rst_n)

41   If (! rst_n )

42   Begin

43   Lw <= T -   100 ;

44   Hw <=   100 ;

45   State <=   1'b0 ;

46   End

47   Else

48   Begin

49   Case   ( state )

50   S0 : begin

51   If ( flag &&   ( lw >   100 ))     // Judge the low time

52   Begin

53   Lw <= lw -   100 ;

54   Hw <= hw +   100 ;

55   State <= s0 ;

56   End

57   Else   If ( flag &&   ( lw ==   100 ))

58   Begin

59   Hw <= hw -   100 ;

60   Lw <= lw +   100 ;

61   State <= s1 ;

62   End

63   Else

64   Begin

65   Hw <= hw ;

66   Lw <= lw ;

67   State <= s0 ;

68   End

69   End

70   S1 : begin

71   If ( flag &&   ( hw >   100 ))     // Judge the high time

72   Begin

73   Hw <= hw -   100 ;

74   Lw <= lw +   100 ;

75   State <= s1 ;

76   End

77   Else   If ( flag &&   ( hw == Multimedia Wiring Harness

The multimedia wiring harness apply to Audio,Video,Radio, LVDs,Flat RCA,USB.


Yacenter has experienced QC to check the products in each process, from developing samples to bulk, to make sure the best quality of goods. Timely communication with customers is so important during our cooperation.

If you can't find the exact product you need in the pictures,please don't go away.Just contact me freely or send your sample and drawing to us.We will reply you as soon as possible.



Electric Fan Wiring Kit,Socket Fan Wiring Harness,Custom Pc Wiring Harness,Multimedia Wiring Harness

Dongguan YAC Electric Co,. LTD. , https://www.yacentercns.com