/******************************************************************** * FileName: timers using blocking code.c * Processor: PIC18F4520 * Compiler: MPLAB C18 v.3.06 * * This file shows the basic syntax for using Timer 0 * * Author Date Comment *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ // David Fisher /** Header Files *****************************************/ #include #include /** Define Constants Here ******************************************/ /** Local Function Prototypes **************************************/ // Configuration Bits #pragma config OSC = INTIO67 #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF /** Global Variables *********************************************/ /***************************************************************** * Function: void main(void) ******************************************************************/ #pragma code void main (void) { // Run the clock at 500 kHz OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 1; OSCCONbits.IRCF0 = 1; // Setup the time with a 1:32 prescaler OpenTimer0( TIMER_INT_OFF & T0_16BIT & T0_SOURCE_INT & T0_PS_1_32 ); // Therefore the timer0 freq is 500 kHz / 4 / 32 = 3906 Hz = 256 uSeconds ADCON1 = 0x0F; TRISB = 0x00; PORTB = 0x00; while (1) { WriteTimer0(0x0000); PORTB = 0x01; // Let's say I want a 0.1 second delay while(ReadTimer0() < 1) { // Could do stuff while waiting for timer } WriteTimer0(0x0000); PORTB = 0x02; // Let's say I want a 0.2 second delay while(ReadTimer0() < 1); WriteTimer0(0x0000); PORTB = 0x04; // Let's say I want a 1 second delay while(ReadTimer0() < 1); WriteTimer0(0x0000); PORTB = 0x08; // Let's say I want a 20 second delay while(ReadTimer0() < 1); WriteTimer0(0x0000); while(ReadTimer0() < 1); } }