If you have a freq of 10 Hz you can divide that by using a softwarecounter(postscaler). You'll end up with 10 divided by your postscalervalue. The postscaler can only be integers. Soooo.........
10Hz / 1 = 10Hz
10Hz / 2 = 5Hz
10Hz / 3 = 3.333Hz
10Hz / 4 = 2.5Hz
10Hz / 5 = 2Hz
10Hz / 6 = 1.667Hz
and so on .......
A better way would be to use a fixed postscaler. Then change the timer reloadvalue and prescaler to get twice the frequency you want. This way you can produce very accurate frequencies in the region you want. If you use a fixed prescaler of 1:8 and a fixed postscaler of 1:2 and only change the reloadvalue you can produce ......
65536 - 6250 = 59286 ---> 10Hz
65536 - 6251 = 59285 ---> 9.9984Hz
65536 - 6252 = 59284 ---> 9.9968Hz
......
65536 - 6944 = 58592 ---> 9.0006Hz
65536 - 6945 = 58591 ---> 8.9993Hz
......
65536 - 7812 = 57724 ---> 8.0005Hz
65536 - 7813 = 57723 ---> 7.9995Hz
......
65536 - 12500 = 53036---> 5Hz
65536 - 12501 = 53035---> 4.9996Hz
......
65536 - 20833 = 44703 ---> 3.0000Hz
65536 - 20834 = 44702 ---> 2.9999Hz
......
65536 - 31250 = 34286 ---> 2Hz
65536 - 31251 = 34285 ---> 1.9999Hz
......
65536 - 62499 = 3037 ---> 1.000016Hz
65536 - 62500 = 3036 ---> 1Hz
It's fairly easy to let PBP calculate the reloadvalue for you. You could specify your frequency in mHz(milliHertz). 10 Hz would be 10000, 1 Hz would be 1000. Run it through this code to produce your timer1reloadvalue. All variables are WORD sized.
Dummy = 62500
Dummy = Dummy * 1000
TMR1Reload = DIV32 DesiredFreq
TMR1Reload = 0 - TMR1Reload
The 62500 comes from OSC/4/Prescaler/Postscaler. 4000000/4/8/2=62500. 1000 is there to make it possible to specify mHz.
There are ways to expand all of this to include variable prescalersettings to get better accuracy at higher frequencies, but that's a story for another day......
/Ingvar
Bookmarks