Recent Posts


                                          My PIC12F683 Development Board
Office Map Circuit diagram for each experiment may not be available separately because they are conducted with PIC12F683 development board that I made. So, the readers should first see the schematic of my development board.

Saturday, June 26, 2010

Experiment No. 4: External Interrupt

PIC12F683 has got one external interrupt pin (GP2, pin 5) which is edge-triggered:
  • rising edge if INTEDG bit in OPTION register is SET,or 
  • falling edge if INTEDG bit in OPTION register is CLEAR
In this experiment we are going to simulate an external interrupt event by a tact switch which when pressed gives a falling edge (5->0V) to external interrupt pin. When a valid edge appears on GP2/INT pin, the INTF bit (INTCON<1>) is set. This interrupt can be disabled by clearing INTE control bit (INTCON<4>). While exiting from the Interrupt Service Routine, INTF flag must be cleared to re-enable the interrupt again. ANSEL and CMCON0 must be initialized to configure GPIO as digital I/Os.

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.


  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 );

do {

 for (i=0; i<= 23; i++) {
  Soft_UART_Write(10); // Line Feed
  Soft_UART_Write(13); // Carriage Return

 if (new_count > old_count)
     for (i=0; i<= 13; i++) {
     for (i=0; i<= 1; i++) {
  Soft_UART_Write(10); // Line Feed
  Soft_UART_Write(13); // Carriage Return

 } 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.


vivek said...

hi sir how do i reset the interrupt count to zero ??
i am writing a program to find out how many interrupt i get with in a second

Post a Comment

Online Embedded Systems Lab

This online laboratory teaches you the fundamentals of microcontroller-based embedded system development through a series of laboratory exercises. Most of the time, students and hobbyists could not afford expensive development kits and software to learn these things. These tutorials are prepared such that you will be building microcontroller projects at a minimum cost. Check this out