TIMER1 2LSBs always read as high. PIC16F15324 xc8

Thread Starter

mobyj

Joined Oct 26, 2019
1
I am trying to use timer1 to measure the duration of an internal process. I just want the number of TIMER1 clockings/counts this proceess takes. Therefore I am not using interrupts or any internal gating. I zero out TIMER1, then enable it, then run my process to be measured 128 times in a while loop, then read TIMER1. The processor is in the PIC16 family I am writing xc8 C code within the MPLABX IDE. My intentent is to be running FOSC at full 32MHz from the HFIRC clock and TIMER1 off of FOSC (which I believe to be 32MHz.

When I run this process, no matter how many times I set the loop to run, when I read TIMER1 afterwards, the bottom 2 bits are always both high. Ma ybe this reflects 4 clock cycles for an instruction... but I don't think that could explain it.

This is (what I believe to be) my relevant code.


OSCCON1 = 0x60; // HFINTOSC, NDIV 1
OSCFRQ = 0x06; // 32MHz system clock

unsigned int timelapse = 0x0000; // 16bit in xc8
unsigned int k = 0x0000;
unsigned int dummyvar = 0x0000;

T1CLK = 0x02; // TIMER1 Clock is FOSC (which I think is 32MHz)
T1CON = 0x02; // Disable TIMER1 + setup for 1 x 16 bit read/write + 1:1 prescaler
TIMER1 = 0x0000; // Zero out TIMER1 as a precaution.
T1CON = 0x03; // Enable TIMER1 while keeping other setting same.

while (k<128)
{
dummyvar = dummyvar + 132;
k++;
}
timelapse = TIMER1;
 
Top