Monday, October 12, 2020

PIC16F887 External Interrupt Example With 7-Segments Display In MikroC

An Overview Of External Interrupt

An interrupt of the MCU is an event that triggers a notification to the CPU. The CPU itself could responds to that events, or could ignore. Responding to the interrupt takes a very short time to handle and come back to the program main loop. Interrupt is very fast. It make the embedded controller program more responsive and more effective.

In the mid-range PIC microcontroller, an interrupt could create by an external events or by an internal peripheral operation. The external event may cause by any logic change to any digital input pin. A communication to the external device also create an interrupt, but we don't want to talk more about this here.

PIC16F887 External Interrupt Example With 7-Segments Display In MikroC
Programming Sample Of External Interrupt

For PIC16F887 the interrupt creates by the digital input output pin has two categories - an external interrupt creates by RB0-INT input pin, and the interrupt on change creates by any pin on PORTB input.

In this post we gonna talk about the external interrupt creates by INT pin. There are two choices of logic changes that could trigger an interrupt on this pin - on the falling edge and on the rising edge. The OPTION_REG register configures each choices.



The OPTION_REG register

Bit 6 (INTEDG) selects between the rising and the falling edge of interrupt creates by the pin INT. Programming the external interrupt we need to configure the following stuffs:

  1. Set RB0-INT pin as a digital input
  2. Optionally turn on its individual weak pull up resistor.
  3. Turn on the external interrupt enable (INTE) bit and the global interrupt enable (GIE) bit in the interrupt control register (INTCON).
  4. Select between that two interrupt edge
  5. Clear the external interrupt flag (INTF) of the INTCON register.
  6. In MikroC, write the Interrupt Service Routine (ISR) to handle the job. Within this routine the programmer must firstly test the INTF to get the external interrupt event, then writing a block of codes to for this event. Within the final line of this block, the INTF bit must be clear by software.


The Interrupt Control Register (INTCON) has many configurations and flag as lists below.

PIC16F887 External Interrupt Example With 7-Segments Display In MikroC
Interrupt Control Register (INTCON)

External Interrupt Programming In MikroC

This example may become difficult to some novice microcontroller programmer. To get a simple introductory programming example about using the external interrupt in MikroC, see this post.

Here, the MCU display the counting data on a multiplexed 7-segments display. The counting has a 99 maximum decimal values. After reaching the maximum value it rolls back to 0.

The counting increment creates by the falling edge of the INT pin. As mentioned earlier, using the interrupt the main program keeps executing its main task. The interrupt event occurs in a very short time and the main program doesn't need to poll for this event.

MikroC Program

  1. #define SW PORTB.RB0
  2. #define RATE 10
  3.  
  4. char CNT=0;
  5.  
  6. void interrupt() {
  7. if(INTCON.INTF)
  8. CNT++;
  9. INTCON.INTF=0;
  10. }
  11.  
  12. void SSD(char TEN,char ONE) {
  13. char LED[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  14. // ROUTINE FOR SEVEN SEGMENTS
  15. PORTA=LED[TEN];
  16. PORTB=0x80;
  17. delay_ms(RATE);
  18. PORTB=0x00;
  19.  
  20. PORTA=LED[ONE];
  21. PORTB=0x40;
  22. delay_ms(RATE);
  23. PORTB=0x00;
  24.  
  25. }
  26. void main() {
  27. char CNT1=0;
  28. PORTA=0x00; // CLEAR PORTBA
  29. PORTB=0x00; // CLEAR PORTB
  30. TRISA=0x00; // SET PORTA AS OUTPUT
  31. TRISB=0x01; // SET RB0 AS INPUT
  32. ANSELH=0x00; // DISABLE ANALOG FUNCTION ON PORTB
  33. OSCCON|=0x70; // SELECT 8MHz INTERNAL OSCILLATOR
  34. OPTION_REG.NOT_RBPU=0; // Enable Global WPUB
  35. OPTION_REG.INTEDG=0; // Interrupt from High TO Low
  36. WPUB=0x01; // Enable WPUB for RB0
  37. INTCON.GIE=1; // Enable Global Interrupt
  38. INTCON.INTE=1; // Enable External Interrupt
  39. INTCON.INTF=0; // Clear External Interrupt Flag
  40.  
  41. // MAIN ACTIVITY
  42. while(1) {
  43. if(CNT==10) { CNT=0; CNT1++; }
  44. if (CNT1==10) CNT1=0;
  45. SSD(CNT1,CNT);
  46. }
  47. }



No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)