Last active
August 29, 2015 13:56
-
-
Save kingcoyote/8940084 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include the XC and PIC18 libraries | |
#include <xc.h> | |
#include <pic18.h> | |
#pragma config FOSC=HS1 | |
// timer that will increment 4 times per second | |
unsigned long int timer; | |
// main loop of the program | |
void main() { | |
// enable Timer 0, part of the T0CON register (datasheet page 211) | |
TMR0ON = 1; | |
// enable prescaler for Timer 0 | |
T0CONbits.PSA = 0; | |
// prescale TMR0 to 1:128 | |
T0CONbits.T0PS = 0b110; | |
// alternatively, you can set the 3 previous commands in one line, like this: | |
// T0CON = 0b10000110; | |
// enable Timer 0's interrupt | |
TMR0IE = 1; | |
// enable general interrupts | |
GIE = 1; | |
// and you can set those last 2 in one line, like this: | |
// INTCON = 0b10100000; | |
// main loop | |
while (1) { } | |
} | |
// interrupt service routine to handle all interrupts | |
void interrupt main_isr(void) { | |
// if Timer 0's interrupt flag has been raised | |
if (TMR0IF) { | |
// increment the seconds counter | |
timer++; | |
// adjust the TMR0 value to 65536 - 15625 = 50271 | |
TMR0 = 50271; | |
// lower the interrupt flag | |
TMR0IF = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment