This application note provides the source for a 100 Hz timer tick with a 11.0592 MHz oscillator.
I understand all the code, but can't understand how to define the timer count costant.
The code is:
#define TIMER0_COUNT 0xDC11 /* 10000h - ((11,059,200 Hz / (12 * FREQ)) - 17) */
Where does those "12" and "17" comes out from?
I'm using a Winbond 8051 core micro and there is no clue on the data sheet of that constant, it just says that in mode 1 the timer is a 16 bit counter/timer increased by negative edge of the clock.
Could someone point me on the right direction? Thanks in advance.
The "12" is the usual difference between the actual external crystal frequency and the 8051 instruction cycle. The original 8051 took 12 clocks to execute one instruction cycle. The cycle counts for instructions from 1..3 are counting these instruction cycles, not clocks. Check the data sheet for your variant to see how many clocks it takes to execute one instruction cycle; 12, 6, 4, 2, and 1 clocks per cycle cores are all common.
Similarly, you need to check the data sheet for your part to see exactly what clock is fed to the timers. For compatibility, most parts have a divide-by-12 on the external frequency. You often have a mux in front of the timer to choose from several clocks. The equation in that #define will change depending on how you configure your timer input.
The "17" I'd guess is a fudge factor to improve accuracy by taking into the account the number of instructions it takes to set the timer and start it running. For a 100 Hz tick, an auto-reload timer is preferable. Or, you could use a free-running timer and calculate the next tick time from the previous tick time. If you have to use a one-shot, and you want _exactly_ 100 Hz, then you need to correct for the time it takes to process the interrupt and execute code reset the timer. If you set the timer to exactly 100 Hz worth of timer ticks, then your interval will be too long by this execution time. The exact fudge factor will depend on your code and the interrupt latency, which is one reason the other sorts of timer are better for a recurring regular tick.