# Counting PIC18F46K20 internal clock edges using MPLAB C18?

I am trying to create a countdown timer using the PIC18F46K20 and display the time on the OLED. I have set up TIMER0 to create a delay of 1 second ( T0CON = 0b00000001 ) so the variabe intCLKsecs should increase every second.

    Timer0_Init();          //Initialise timer
T0CONbits.TMR0ON = 1;  // start timer

while(INTCONbits.TMR0IF = 1) {


/* Check for clock edges and and increment countSecs by one on every falling edge. The timer is set up to output pulses that are around 1 sec apart. */ INTCONbits.TMR0IF = 0; // Clear the flag

    korf_intCLKsecs++; //Should increase by one every second on every falling edge
korf_InitSecs = 1800; //Initialised time for a single korfball half
net_timeLeft = net_InitSecs-net_intCLKsecs; //Time remaining in the half
displayTime(net_timeLeft);


}

I pass the the [timeLeft] to a function that formats it and displays it like a clock i.e 8:56 Here is the time dicplay function.

int displayTime(int currentTime)
{
int secs;
int mins;
char Buffer[60];
secs =  currentTime % 60;
mins = (currentTime / 60) % 60;

sprintf(Buffer, "\n\t\t %d : %d\0",mins,secs);
oled_puts_1x(Buffer);
oled_refresh();
oled_clear();
}


I do apologize if i have made any mistakes in the way i am posting, I am very new to both forums and PICKit programming. Can someone out there please help me figure out how to get a correct value into my intCLKsecs variable by counting the CLK pulses on every falling edge? What do i write in MPLAB IDE to make sure that [intCLKsecs] increases on every falling edge?

• What is your actual clock frequency? – Matt Young Apr 18 '14 at 15:26
• I don't know. How do i find out? – DixonLamont Apr 18 '14 at 15:53
• Timer0 runs off the internal instruction clock. At Fosc = 1MHz, the instruction clock is 250kHz. – DixonLamont Apr 18 '14 at 16:02
• "while(INTCONbits.TMR0IF = 1)" is a classic C-language programming trap. You must code "==" when comparing and "=" when assigning a value. My preference is to omit the comparison when comparing to true (i.e. not 0): – aja Apr 19 '14 at 7:16
• The timer will overflow in 65536 counts. At the instruction clock rate of 250kHz, the timer overflow will occur every 65536 x (4 / 250,000) = 1.048 seconds. This is a time in the range i with the prescaler assigned to Timer0. It runs directly off the instruction clock. I want to increase a variable [count] by 1 everytime the timer overflows. Can you suggest how i can write that in MPLAB C18 please? I assume interrupts will be involved. – DixonLamont Apr 19 '14 at 12:08