/******************************************************************** * FileName: timers with interrupts.c * Processor: PIC18F4520 * Compiler: MPLAB C18 v.3.06 * * This file displays the timer 0 on the PICDEM 2 LCD screen. * * As a bonus it increments the PORTB LEDs using an interrupt when * the timer overflows. * * Author Date Comment * Dave Fisher 10/3/2007 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /** Processor Header Files ****************************************/ #include #include "LCD module.h" #include #include /** Define Constants Here ******************************************/ /** Local Function Prototypes **************************************/ void low_isr(void); void high_isr(void); #pragma config OSC = EC // External 4MHz crystal #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF /** Declare Interrupt Vector Sections ****************************/ #pragma code high_vector=0x08 void interrupt_at_high_vector(void) { _asm goto high_isr _endasm } #pragma code low_vector=0x18 void interrupt_at_low_vector(void) { _asm goto low_isr _endasm } /** Global Variables *********************************************/ char line1[17]; char line2[17]; unsigned int result0, result1; /***************************************************************** * Function: void main(void) ******************************************************************/ #pragma code void main(void) { // Setup Output LEDs ADCON1 = 0x0F; TRISB = 0xF0; PORTB = 0x00; // Initialize the LCD XLCDInit(); XLCDClear(); // Configure timer0 OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_128 ); // Clock Frequency = 4 MHz // Instruction cycle Frequency = 1 MHz // Timer 0 Frequency = 1 MHz / 128 = 7812.5 Hz // Timer 0 Period = 128 uSeconds <-- Always equals prescaler when clock is 4 MHz // Overflow every 128 uSecond * 2^16 ticks = 8.4 seconds // This code is only necessary for the interrupt // INTCONbits.PEIE = 1; // Enable Peripheral interrupts (to use Timer 1 as interrupt) INTCONbits.GIE = 1; // Enable Interrupts while (1) { // Read Timer0 and display on line 1 result0 = ReadTimer0(); // read timer sprintf(line1,"Timer 0 -> %#5.5u",result0); XLCDL1home(); XLCDPutRamString(line1); // TODO: Read Timer1 and display on line 2 (very similar to above) } } /***************************************************************** * Function: void high_isr(void) * Input: Occurs on a timer overflow * Output: Modify the PORTB LEDs * Overview: The interrupt counts overflows using LEDs ******************************************************************/ #pragma interrupt high_isr // declare function as high priority isr void high_isr(void) { if(INTCONbits.TMR0IF) // Check wether it was the timer 0 interrupt { INTCONbits.TMR0IF = 0; // Clear interrupt flag for TIMER Zero PORTB++; } } /****************************************************************** * Function: void low_isr(void) * Input: * Output: * Overview: ********************************************************************/ #pragma interruptlow low_isr // declare function as low priority isr void low_isr(void) { }