T07
T07

Reputation: 43

How to count up in binary on a board of 8 LEDs - PIC

I have this program and I'm wanting to modify it so the LEDs count up in binary at a rate of 1Hz (1s).

#include <xc.h>           

void main(void) { 
    TRISD  = 0x00;          
    PORTD  = 0x00; 
    INTCON = 0xA0;          
    OPTION_REGbits.T0CS = 0;     
    OPTION_REGbits.PSA  = 0;     
    OPTION_REGbits.PS = 7;          
    while(1);           
} 

void interrupt myISR(void) { 
    if(TMR0IF) { 
        PORTD++; 
        INTCONbits.TMR0IF = 0;  
    }
}

I understand I will have to modify the myISR function so that PORTD increments after a certain amount of overflows and that a suitable preload value will be needed but in terms of counting in binary, what are the possible ways to go about this?

Upvotes: 1

Views: 718

Answers (1)

Mike
Mike

Reputation: 4288

If your Timer 0 Interrupt comes e.g every 100ms this example could help:

void interrupt myISR(void) {
    static uint8_t counter = 0;

    if(TMR0IF) 
    { 
        INTCONbits.TMR0IF = 0;  
        TMR0 = RELOAD_VALUE_100MS
        counter++;
        if (counter >= 10)
        {
            counter = 0;
            PORTD++; 
        }
    }
}

Upvotes: 1

Related Questions