Using 0C2 interrupt generate a pulse width modulated signal
Solution
There are various terms associated with PWM:
Period
As shown in the the figure, Ton denotes the on-time and Toff denotes the off time of signal. Period is the sum of both on and off times and is calculated as shown in the equation below:
Duty Cycle
Duty cycle is calculated as on-time to the period of time. Using the period calculated above, duty cycle is calculated as:
Voltage regulation is done by averaging the PWM signal. Output voltage is represented by the following equation:
As you can see from the equation the output voltage can be directly varied by varying the Ton value.
If Ton is 0, Vout is also 0. if Ton is Ttotal then Vout is Vin or say maximum.
here, Vin =5V
Vout = 3.4 V
therefore, D=(3.4/5) = 0.68
D=0.68*255= 173D=AE hex
Ton= D*Ttotal= 6.8 ms
AVR: Assembly Code Example
;8-bit Non-Inverted PWM code example
.equ pulse_width = $0AE
;Pulse width can be changed from 0 to FF(255)
PWM_START:
ldi temp, pulse_width ;Load pulse width
out OCR1AL, temp ;OCR1A = Pulse width
clr temp
out OCR1AH, temp
ldi temp, $81 ;8-bit PWM Mode
out TCCR1A, temp ;Non Inverted
in temp, DDRD ;Make PortD.5 as o/p
ori temp, (1<<5)
out DDRD, temp
ldi temp, $1 ;Start PWM
out TCCR1B, temp
ret ;Return to main
;PWM will run in background automatically

