I am trying to get Timer2 to flash an LED, this is not the final operation but I had it flashing an LED without the interrupt so I am continuing with this for now. But I cannot seem to get it working. I think I have set up the peripheral interrupts, and the RC2 register so when that equals the TMR2 register the interrupt flag should be set and the interupt should be initiated. When in the interupt I add a delay after toggleing the LED so it will be possible to see the flash of the LED, again not usual behaviour but if I don't add a delay it will flsh to fast and we won't be able to see it.
Here is my code,
// PIC18F6520 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config OSCS = OFF // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is disabled (main oscillator is source))
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF // Brown-out Reset Enable bit (Brown-out Reset disabled)
#pragma config BORV = 25 // Brown-out Reset Voltage bits (VBOR set to 2.5V)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 128 // Watchdog Timer Postscale Select bits (1:128)
// CONFIG3L
// CONFIG3H
#pragma config CCP2MUX = OFF // CCP2 Mux bit (CCP2 input/output is multiplexed with RE7)
// CONFIG4L
#pragma config STVR = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Low-Voltage ICSP Enable bit (Low-voltage ICSP disabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot Block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 8000000
void main(void) {
TRISC = 0x00; // Initialise PORTC as all 0's, LED on PORTC RC2.
// Setup Timer2 1:1 post and pre scaler.
T2CONbits.T2CKPS = 0b00;
T2CONbits.T2OUTPS = 0b0000;
T2CONbits.TMR2ON = 1; // Turn on Timer2.
PR2 = 15; // Set PR2 register to jump to interrupt when equal to TMR2 register
PIE1bits.TMR2IE = 1; // Initialise Timer2 interrupt enable.
INTCONbits.PEIE = 1; // Setup peripheral interrupts.
// Run main loop for ever.
while(1){
}
}
// Interrupt routine.
void __interrupt() isr(void)
{
// Check to see if TMR2 interrupt flag set.
if(PIR1bits.TMR2IF){
PIR1bits.TMR2IF = 0; // Reset TMR2 interrupt flag.
PORTCbits.RC2 ^= 1; // Toggle RC0.
__delay_ms(500); // Delay Toggle to make it possible to see the LED flash.
}
}
Can anyone give advise as to what I may be doing wrong please?