And the output pin can be changed to whatever pin you want.Īs explained in assembly example, the same code can be implemented in C. You can also make use of Timer1 if you want. R7 = 0 will give you o/p 0V approx and R7 = 255 will give you 5V approx. In above example I am using 160, you can choose any value from 0 to 255. The width of PWM can be changed by changing the value of R7 register.
so to stop PWM you can simply disable the timer. MOV TH0, A so the value loaded into TH0 + R7 = 255Īs everything is handled in ISR. MOV TH0, R7 Load high byte of timer with R7ĬLR TF0 Clear the Timer 0 interrupt flagĬLR PWM_FLAG Make PWM_FLAG=0 to indicate start of low sectionĬLR C Clear C (the carry bit) so it does SETB PWM_FLAG Make PWM_FLAG=1 to indicate start of high section JB PWM_FLAG, HIGH_DONE If PWM_FLAG flag is set then we just finished If flag is set timer is loaded with timer value for low time and if flag is cleared timer is loaded with high time value. When timer overflows, PWM_FLAG is checked. IC circuit diagram of the bit splitter is shown in the figure 5.7 27. PWM_FLAG EQU 0 Flag to indicate high/low signalĪs we are using Timer0 for generating PWM, timer interrupt service routine uses PWM_FLAG bit to selects the high and low section of PWM signal. Usually, the duty cycle of I-data and Q-data are two times or double with compare to the original data signal. If for high level we load a value X in TH0 then for low level TH0 will be loaded with 255-X so that total remains as 255.Īssembly Code Example Timer setup for PWM PWMPIN EQU P1.0 PWM output pin Values for high and low level will be loaded in such a way that total delay remains same. As we have discussed in the introduction of PWM that by changing the Ton time, we can vary the width of square wave keeping same time period of the square wave. The basic idea behind PWM implementation on 8051 is using timers and switching port pin high/low at defined intervals. if T on is T total then V out is V in or say maximum. Output voltage is represented by the following equation:Īs you can see from the equation the output voltage can be directly varied by varying the T on value. Voltage regulation is done by averaging the PWM signal. This method is used in various areas of application like: PWM signal when used at a different duty cycles gives a varying voltage at the output. This is also important in driving output devices such as lamps, heaters and many others. 4.4.5 and hence the power supplied to control the speed of the motor. Using the period calculated above, duty cycle is calculated as: Changing the duty cycle changes the average DC voltage or DC current level of the output, as shown in Fig. Period is the sum of both on and off times and is calculated as shown in the equation below:ĭuty cycle is calculated as on-time to the period of time. Duty cycle: It is represented as percentage of time signal remains on during the period of the PWM signalĪs shown in the the figure, T on denotes the on-time and T off denotes the off time of signal.Period: It is represented as the sum of on-time and off-time of PWM signal.Off-Time: Duration of time signal is low.On-Time: Duration of time signal is high.There are various terms associated with PWM: