- rising edge if INTEDG bit in OPTION register is SET,or
- falling edge if INTEDG bit in OPTION register is CLEAR
On the arrival of an interrupt, the new value of counter will increase by 1 and is displayed as no. of interrupts serviced.
Experimental Setup:
External interrupt will be simulated by a tact switch on the board. So connect SW1 to GP2 with a jumper wire. For display purpose we will again use a hyperterminal window on a PC.
Software:
/*
PIC12F683 Experiment Board
Experimen No. 4 : External Interrupt
Date: 06/26/2010
*/
char Message1[] = "Interrupt Demonstration!";
char Message2[] = "Interrupt No.= ";
char *temp = "00", error;
unsigned int i, old_count=0, new_count=0;
// Interrupt Service Routine
void interrupt(void){
if (INTCON.INTF == 1) // Check if INTF flag is set
{
new_count++; // If yes, increase counter
temp[0] = new_count/10 + 48; // Convert count digits into characters
temp[1] = new_count%10 + 48;
INTCON.INTF = 0; // Clear interrupt flag before exiting ISR
}
}
// Main program
void main() {
CMCON0 = 7;
TRISIO = 15; // GPIO 0, 1, 2, 3 Inputs; Rest are O/Ps
ANSEL = 0;
OPTION_REG = 0x00; // Clear INTEDG, External Interrupt on falling edge
// Interrupt Setup
INTCON.INTF = 0; // Clear interrupt flag prior to enable
INTCON.INTE = 1; // enable on change interrupts
INTCON.GIE = 1; // enable Global interrupts
// Define GPIO.3 as UART Rx, and 5 as Tx
error = Soft_UART_Init(&GPIO,3, 5, 9600, 0 );
Delay_ms(100);
do {
for (i=0; i<= 23; i++) {
Soft_UART_Write(Message1[i]);
Delay_ms(50);
}
Soft_UART_Write(10); // Line Feed
Soft_UART_Write(13); // Carriage Return
delay_ms(500);
if (new_count > old_count)
{
for (i=0; i<= 13; i++) {
Soft_UART_Write(Message2[i]);
Delay_ms(50);
}
for (i=0; i<= 1; i++) {
Soft_UART_Write(temp[i]);
Delay_ms(50);
}
Soft_UART_Write(10); // Line Feed
Soft_UART_Write(13); // Carriage Return
old_count=new_count;
}
} while(1);
}
Experimental Output:
On hyperterminal window, you will continuously see a message "Interrupt Demonstration!". When you press the tact switch, that will give an interrupt (falling edge), and the counter value goes up by 1. The updated counter value will be displayed.
No comments:
Post a Comment