Pic18 one second delay using timer 0


#1

Hello, I am trying to blink an led every second using timer 0 of pic18f4550. I am using 16 Mhz crystal and mplab xC8 compiler for programming. The program compiles successfully but the led keeps turned off. I double checked the led, their is nothing wrong with led. Any Idea?

Thanks in advance!

#include <xc.h>
#include <p18f4550.h>
#include <stdint.h>  
#define _XTAL_FREQ 16000000            //20 for the Delays

#pragma config FOSC        = HS
#pragma config WDT         = OFF
#pragma config LVP         = OFF
#pragma config BOR         = OFF
#pragma config MCLRE       = ON
#pragma config PWRT        = ON
#pragma config PBADEN      = OFF


 uint8_t tmr0l;
 uint8_t tmr0h;

int main(void)
{
  
    INTCON = 0x20;
    TMR0H = 0x7a;
    TMR0L = 0x12;
   
    tmr0l = TMR0L;
    tmr0h = TMR0H;
    
    T0CON = 0x8e;

    while(1)
    {
        while(TMR0IF != 1); {
            TMR0IF = 0;
            TMR0L = 0x12;
            TMR0H = 0x7a;
            PORTBbits.RB0 ^= 1;
        }
     
    }
    
    return;
}

#2

Hi! I dont think you can create a one second delay with current T0CON value. You have not enabled the prescaler in T0CON. In order enable prescaler you need to set T0CON = 0x86 in your
program. Please make the change and report back.

Regards!


#3

I changed the T0CON value to 0x86, but still the same. The led is not blinking. Do you think my pic18f4550 is damaged?


#4

Add TRISB = 0x00 to the program. You need set the port or corresponding pin as output. Try below code and report back. Regards

#include <xc.h>
#include <p18f4550.h>
#include <stdint.h>  
#define _XTAL_FREQ 16000000            //20 for the Delays

#pragma config FOSC        = HS
#pragma config WDT         = OFF
#pragma config LVP         = OFF
#pragma config BOR         = OFF
#pragma config MCLRE       = ON
#pragma config PWRT        = ON
#pragma config PBADEN      = OFF


 uint8_t tmr0l;
 uint8_t tmr0h;

int main(void)
{
    TRISB = 0x00;    
    INTCON = 0x20;
    TMR0H = 0x7a;
    TMR0L = 0x12;
   
    tmr0l = TMR0L;
    tmr0h = TMR0H;
    
    T0CON = 0x86;             

    while(1)
    {
        while(TMR0IF != 1); {
            TMR0IF = 0;
            TMR0L = 0x12;
            TMR0H = 0x7a;
            PORTBbits.RB0 ^= 1;
        }
     
    }
    
    return;
}

#5

Thanks!! Your code worked.