
So, the 32 values must be called such that they make up the half cycle. Recall that the sine table used is only for half a cycle.

Our frequency is 50Hz – time period is 20ms. Now let’s see why TBL_POINTER_SHIFT is needed to be incremented every 5 interrupts and how we’ve done that. This 16-bit value is then split into two 8-bit values to be assigned to OCR1AH and OCR1AL, thus setting the appropriate duty cycle. This acts as – or rather is – the table pointer and the required value is retrieved off the sine table. Why 5 times? We’ll see that a little later.Īfter TBL_POINTER_NEW is shifted 11 times, DUTY_CYCLE is assigned the resultant 5-bit value. Now, the trend is that TBL_POINTER_SHIFTholds the same value for 5 consecutive interrupts – go on, you can do the math. When the microcontroller starts up, TBL_POINTER_SHIFTholds 0 and on subsequent interrupts, TBL_POINTER_SHIFTholds 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3 …. Right shifting 11 times is the same as dividing by 2 11 (2 11 = 2048) – just more efficient. TBL_POINTER_SHIFT right-shifts TBL_POINTER_NEW eleven times and stores that value. At the 160 thinterrupt, TBL_POINTER_NEWoverflows, just as it does the 320 th interrupt, the 480 thinterrupt and so on. At each interrupt TBL_POINTER_NEW is increased by 410.

So, at the next interrupt, TBL_POINTER_NEWoverflows and holds 64. After 159 interrupts, TBL_POINTER_NEW holds 65190. So, after the first interrupt it holds 410, after the second interrupt 820, after the third 1230 and so on.

TBL_POINTER_NEW is a 16-bit variable and at the beginning of program execution has a value of 0. In the interrupt, the variable TBL_POINTER_NEWis updated – every 62.5us it is increased by 410. SET_FREQ determines the output frequency of the sine wave, which in our case here is 50Hz. OCR2 =(unsigned char) x + ( ( 256 - x ) * 0.In the main() function, I had assigned the value of 410 to SET_FREQ.

TCNT2=x //reload this value at each overflow
