# Timer2 Discrepancy on PIC16F628A

I have a PIC16F628A running at 12MHz and I wanted to create an approximate 20ms timer. At 12MHz, and an instruction cycle of 4 clock cycles, this works out to be 3,000,000 instructions per second, or 60,000 instructions per 20ms. Since Timer2 advances with the instruction cycle (FOSC/4), I felt it was perfect for counting out 60,000 instructions.

The specs indicate I can set a prescaler to 1:16 as well as a postscaler to 1:16, giving a total divisor of 16x16 or 1:256. Dividing 60,000 by 256 leaves 234.375. Rounding down to 234, if I count 234 * 256 instructions, that's 59,904, getting me very close to my desired 20ms interval. So, I set PR2 = 234 (233, actually, since it wraps after counting 0-233, giving 234 total count), fully expecting to have my 20ms interval.

However, after running this, I get an 80ms interval -- 4x longer than expected. Looking at the prescaler, there are two bits that can select 00 = 1:1, 01 = 1:4, or 1x = 1:16. I set the bits to 11, for the 1:16 setting. But I'm curious if maybe the specs don't mention that maybe 10 = 1:16, and 11 = 1:64? Could that be possible? I haven't tried setting the bits to 10, and now I'm out of town so can't try it, but I'm scratching my head trying to determine where the discrepancy is. I originally thought maybe I wasn't taking into account the fact that Timer2 is driven by FOSC/4, but if it was driven directly by the clock, it would be 4x shorter than expected.

I get around the problem by setting the prescaler to 1:4, but this doesn't explain the discrepancy. Is there something wrong with my thought process on this, or is this a genuine issue? If the prescale and postscale are truly 1:16, and I set PR2 to 233 (0xE9), that should be 16x16x(233+1) = 59904 instructions. 59904 instructions with a 12MHz clock = 1/(12Mhz/4) * 59904 = 19.968ms. Right???