0
votes

i tried lately to make a program that uses timer0 to overflow from 255 to 0 and goto the isr code which will turn an led on if button on T0CKI pin was pressed for once as I was taught from microchip's timer0 datasheet tutorial but it is not working at all whatever I tried to change from code, written in MPLAB X IDE compiled with XC8, here is the code:

#include "config.h"

void interrupt ISR();
void INIT();

void main(){
    INIT();
    while(1){
        if(PORTAbits.RA4 == 1){
            TMR0--;
        }
    }

}

void interrupt ISR(){

        if(INTCONbits.TMR0IF == 1 && INTCONbits.TMR0IE == 1){
            TMR0 = 255;
            INTCONbits.TMR0IF = 0;
            PORTDbits.RD0 = 1;
        }else{
            PORTDbits.RD0 = 0;
        }

}

void INIT(){
    INTCON = 0b11100000;
    OPTION_REG = 0b11100111;
    TRISDbits.TRISD0 = 0;
    PORTDbits.RD0 = 0;
    TRISAbits.TRISA4 = 1;
    TMR0 = 255;
}

this is main.c where I wrote all the code config.h is just where I set the configuration bits using Windows->Configuration bits in MPLAB

thanks in advance for all repliers much appreciated

1
You check for TMR0IE. Where do you set it ? - Marco van de Voort
it is bit 5 in intcon register set to 1 -on- - user3674628

1 Answers

0
votes

The timer hardware does not check the result of arithmetic operations. It will only set the interrupt flag if it overflows the register without the intervention of the core (ie. when the timer is running and overflows from 255 back to zero). When you do:

TMR0--;

It is essentially implemented in the core hardware as,

Read TMR0 memory location
Add 1
Write TMR0 memory location