1
\$\begingroup\$

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?

\$\endgroup\$
5
  • \$\begingroup\$ What is your actual clock frequency? \$\endgroup\$ – Matt Young Apr 18 '14 at 15:26
  • \$\begingroup\$ I don't know. How do i find out? \$\endgroup\$ – DixonLamont Apr 18 '14 at 15:53
  • \$\begingroup\$ Timer0 runs off the internal instruction clock. At Fosc = 1MHz, the instruction clock is 250kHz. \$\endgroup\$ – DixonLamont Apr 18 '14 at 16:02
  • \$\begingroup\$ "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): \$\endgroup\$ – aja Apr 19 '14 at 7:16
  • \$\begingroup\$ 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. \$\endgroup\$ – DixonLamont Apr 19 '14 at 12:08
1
\$\begingroup\$

Timer 0 set up for a "delay" of 1 second sounds implausible unless you are using a very slow clock.

They way I usually create slow clock ticks (like 1 second) is to use timer 2 with its built-in period register to make a 1 ms clock tick. That is still "slow" relative to how fast the PIC can run. If the PIC is running at 10 MHz instruction rate, then the 1 ms interrupt will occur once every 10,000 insruction, which means it takes a tiny fraction of the processor's overal time. Then you count multiple 1 ms periods in firmware to make various other clock ticks.

For example, you might count 100 of those to make a 100 ms clock tick, then 10 of those to make a 1 second clock tick. Often several different internal clock tick rates are useful. If you truly only need 1 second, then set up timer 2 to the largest sub-multiple of 1 second worth of instruction cycles it can manage, then count in firmware from there.

\$\endgroup\$
3
  • \$\begingroup\$ I think i asked the wrong question. All i need to know is how to count internal clock edges for which ever timer module that gives me a number that will increase every 1 second or close to 1 second. Can you help? \$\endgroup\$ – DixonLamont Apr 18 '14 at 17:09
  • \$\begingroup\$ @Dixon: I can't understand what you are asking. "ever timer module that gives me a number that will increase every 1 second" is gibberish. \$\endgroup\$ – Olin Lathrop Apr 18 '14 at 17:28
  • \$\begingroup\$ Say if i wanted to count the pulses from the internal clock of the PICKit. How would i do it?. OR Imagine i have a square wave generator and i want to count how many cycles have passed \$\endgroup\$ – DixonLamont Apr 18 '14 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.