Precision Frequency Generator


Frequency generator usually has certain accuracy. Analog frequency generator typically has a relatively low accuracy. Errors that arise sometimes still above 1%, while digital frequency generator typically has higher accuracy with the error below 0.05%.
Many applications of frequency generator are created using the AVR microcontroller to build AVR-based frequency generator. But most have low accuracy. AVR can be operated as frequency generator by using a timer. CTC (Clear Timer on Compare) mode is usually used for this purpose. Theoretically, the output frequency fOCn of a timer will fulfill the following equation.
Where N is the prescaler value and OCRn is the match compare register. These equations will yield values that are not high accuracy. For example, we use the timer 0 to generate the frequency of 10Hz. There are several options for the value of N and OCRn.
 
      N    OCRn (dec)    OCRn (hex)

      8    69119            10DFF
    64      8639              21BF
  256      2159                86F
1024        539                21B

By looking at OCRn value in the table above, the generation of these frequencies is only possible with prescaler equal to 64, or 256 or 1024 and must use 16-bit timer, which timer 1. This is a conventional way.
With a prescaler 64, or 256 or 1024, we can use 8-bit timer, which is
timer 0 or 2. This may occur in large applications where the timer 1 is used to trigger another subroutine. With 8-bit timer we just need to fill the low byte of OCRn to OCR0 or OCR2 register. Furthermore, we run the timer as many as M times, where M is the high byte of OCRn value.
For example, to generate a clock with a frequency of 10Hz, the prescaler value can be filled 64, the value OCR0 = BFh and timer 0 is repeated as many times 21h.
But after a try, sometimes inaccurate output frequency 10Hz could be occured. For that, we need to do calibration. The trick is to shift the value OCRn. By increasing the OCRn value, the frequency will be lower; and by decreasing the OCRn value, the frequency will be higher. Ideally, the increase or decrease the output frequency value is as small as possible. But the time for single-digit increase of TCNTn (t) cannot be small as possible. It depends to prescaler which is used as the following table.

      N    t (us)
      8       0,72
    64       5,79
  256     23,15
1024     92,59

To fix it, shift the OCRn
value on the last count. Another example, for the generation of 5Hz frequency, we can operate the timer 0 with CTC mode and prescaler=64. In theory OCR0 value = 255. After the timer 0 count as much as 67 times, the value OCR0 changed to 128. With the simulation in AVR Studio, we get the output frequency to 5.0003 Hz. The value of these can be calibrated by decreasing the value of OCR0 be 127 so that the value of output frequency is 5.0000 Hz. Output frequency will have a maximum error of 0.01%. 

Download .asm and hex code


Does the error still can be reduced? Of course we still can!
We still use the prescaler = 64. With prescaler = 8, the error rate can be reduced.
But for now it might not need to do ....



What this routine can be applied for?


We have applied it for:
- To fix sampling periode for True RMS Calculation.
- To fix the periode of interval of sending data via serial port.

3 comments:

  1. Please check your formula. I find it wrong when you have to 2.N (1 + OCRn)

    ReplyDelete
  2. The formula above is correct.
    but indeed there are few errors in the text
    I've updated it...
    Thanks for your correction....:)

    ReplyDelete
  3. super tutorial which u have given ...Now i got some idea about the frequency generation using timer

    ReplyDelete