#include "p18f4550.h"
#include "Config_Bits.h"
void Timer2(void);
#pragma code High_Vector_Section=0x08 // We shall use high-interrupt. Interrupt vector is at address 0x08. <--- (changed)
void Timer2_Interrupt (void) // On every occurence of interrupt from Timer2...
{
_asm
goto Timer2 // ... jump to high-interrupt ISR Timer2!
_endasm
}
#pragma code
volatile unsigned int Limit=0, Counter=0; // Global variables. Their values can be changed by main() or by ISR.
#pragma interrupt Timer2 // Tell the compiler that ISR for high-interrupt is Timer2. <--- (changed)
void Timer2(void) // The body of high-interrupt ISR starts here.
{
PIR1bits.TMR2IF=0; // Immediately clear the interrupt flag.
Counter++; // Increase counter. Remember, this means that one mS is passed!
LATDbits.LATD0=!LATDbits.LATD0; // Change the state of port D, pin 0 and LED connected to it.
if(Counter>=1000) // Look if counter reaches 1000mS - it is one second (1s)! And if so...
{
LATDbits.LATD4=!LATDbits.LATD4; // ... change state of port D, pin 4 and LED connected to it...
Counter=0; // ... and clear counter to start counting again.
Limit++; // Increase the limit.
}
}
void main(void)
{
TRISD=0x00; // Port D to be output port.
LATD=0x00; // All LED`s are OFF.
IPR1bits.TMR2IP=1; // Interrupt priority bit set to 1 - high priority. <--- (changed)
PIE1bits.TMR2IE=1; // Interrupt on TMR2IF enabled.
//T2CONbits.T2CKPS0=1;
//T2CONbits.T2CKPS1=1;
//T2CONbits.TMR2ON=1;
T2CON=0b00000110; // Timer2 config register: prescaler (bits 0-1): divide by 16
// timer2 ON/OFF (bit 2): TMR2ON=1 - ON
// postscaler (bits 3-7): 1:1
PR2=0x7C; // Period register set to 124 (dec.).
INTCONbits.PEIE=1; // Enable peripheral interrupt sources.
INTCONbits.GIE=1; // Enable global interrupts.
while(Limit<=4); // Wait approx. 5 seconds... <--- (changed)
T2CONbits.TMR2ON=0; // ... than stop theTimer2... <--- (changed)
INTCONbits.PEIE=0; // ... disable peripheral interrupt sources... <--- (changed)
INTCONbits.GIE=0; // ... and disble global interrupts. <--- (changed)
LATDbits.LATD7=1; // Change state of pin 7 at port D if we arriwe here. <--- (changed)
while(1);
}