Page 254 - Mechatronics with Experiments
P. 254
240 MECHATRONICS
function, after saving the contents of certain registers and return address in the stack. We
can type in the code for the interrupt service routine in high_ISR or call another function
from there and just have the “call” or “goto” statement in the high_ISR function. In
addition, we need to setup certain registers for the interrupt to function properly, that is,
enable interrupt, define the active state of interrupt. Interrupt service routine (ISR) must
have no input arguments and no return data.
#include <p18f452.h> /∗ Header file for PIC 18F452 register declarations ∗/
#include <portb.h> /∗ For RB0/INT0 interrupt ∗/
/∗ Interrupt Service Routine (ISR) logic ∗/
void toggle_buzzer(void) ;
#pragma code HIGH_INTERRUPT_VECTOR = 0x8
/∗ Specify where the program address to be stored ∗/
void at_high_interrupt_vector(void)
{
_asm
goto High_ISR
_endasm
}
#pragma code /∗ Restore the default program memory allocation. ∗/
#pragma interrupt High_ISR
void High_ISR(void)
{
CCP1CON = ˜CCP1CON & 0x0F
/∗ Toggle state of buzzer: OFF if ON, ON if OFF ∗/
INTCONbits.INT0IF = 0
/∗ Clear flag to avoid another inter-
rupt due to same event∗/
}
/∗ Setup of the ISR ∗/
void EnableInterrupt(void)
{
RCONbits.IPEN = 1 ; /∗ Enable interrupt priority levels ∗/
INTCONbits.GIEH = 1;/∗ Enable high priority interrupts ∗/
}
void InitializeBuzzer(void)
{
T2CON = 0x05 ; /∗ postscale 1:1, Timer2 ON, prescaler 4 ∗/
TRISCbits.TRISC2 = 0;/∗ configure CCP1 module for buzzer operation ∗/
PR2 = 0x80 ; /∗ initialize PWM period ∗/
CPPR1l = 0x80 ; /∗ ......... PWM duty cycle ∗/
}
void main (void)
{
EnableInterrupts() ;
InitializeBuzzer() ;
OpenRB0INT(PORTB_CHANGE_INT_ON & PORTB_PULLUPS_ON & FALLING_EDGE_INT) ;
/∗ Enable RB0/INT0 interrupt, configure RB0 pin for
interrupt, trigger interrupt on falling edge ∗/
CCP1CON = 0x0F ; /∗ Turn ON buzzer ∗/
while (1) ; /∗ Wait indefinitely. ∗/
/∗ when the interrupt occurs,
the corresponding ISR will be executed ∗/
}