Professional Documents
Culture Documents
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.