You are on page 1of 4

ES Assignment

Manvendra Singh Kushwah, 18EE30013


Lekhraj Parihar, 18EE30012

Objective: ​Sine wave generation of variable frequency


and for a variable time duration

Algorithm :
● Store frequency and time data in program RAM with .DB directive
● Hardcode OCR values for timer 2 for a single sine wave
● Formula for OCR = (%Duty cycle x 256) -1
● By this, OCR2 value comes = 178,255,255,178,76,0,0,76
● Set Prescaler value for fast PWM mode for both timers(for now take prescaler of 1)
by configuring TCCR0 and TCCR2
● TCCR0 = 0x69(01101001)(prescaler 1)
● TCCR2 = 0x2A(00101010)(prescaler 8)
In Main loop,
➢ Read frequency(F) & time length(T)
➢ Find the number of cycles N as N=T.F Multiplication subroutine
➢ Calculate T1 = 1/8F . Effectively, calculate TCNT2 value for timer 2
➢ To end current phase
➢ Run Inner loop for N times
➢ Run nested inner loop for 8 times
➢ For 1st iteration run OCR0[0] value for T1 time
➢ For 2nd iteration run OCR0[1] value for T1 time
➢ For Kth iteration run OCR0[k-1] value for T1 time
➢ In nested inner loop add condition to continue with next iteration(for next
value of k) if OCR2 reached
Note​ : This can be better achieved with interrupt, but it wasn’t used for this solution.
Explanation:
The sine wave is divided into 8 equal segments and the OCR0 values for each segment is
preset into an array that is used in the loop to set OCR0 register values during iterations.

The approximation of the sinusoidal wave in steps


into 8 segments

Respective OCR0 values: 178 255 255 178 76


0 0 76
This is an approximation of the sinusoidal wave and the OCR values for the corresponding
segments are 178, 255, 255, 178, 76, 0, 0, 76 respectively.
Each sine wave will have a time period = 1/F, where F is the frequency stored in the RAM.
Now we can divide this time period of 1/F into 8
intervals corresponding to the 8 segments in the wave.
If the RAM asks to repeat the wave of frequency F for a
time T then the number of waves in this time T is,
N = time periodtotal time T
of each wave (1/F ) = T × F .
Now we will iterate N number of times in which each
iteration will generate a sinusoidal wave. And nested
within each of those iterations will be 8 additional
iterations to generate PWM pulses of different duty
cycles in each segment.
Now within each of these segments, there would be a
number of PWM pulses and the duration of each
segment will be decided by the required frequency of
the wave. These durations can be calculated as,
time period of each wave (1/F ) 1
T1 = 8
= 8×F

To implement the duration of each segment, we


implement TIMER2 in CTC mode and set the TCNT2
value according to T​1 to ensure the segment only runs
for the desired time interval, and then the overflow flag
of TIMER2 (TOV) is used to determine the end of a
period T1
CIRCUIT DIAGRAM

You might also like