Monday, November 2, 2020

PIC16F887 Interrupt-On-Change in MikroC

Introduction

Port B of PIC16F887 is individually able to trigger interrupt signal to the controller. It's called INTERRUPT-ON-CHANGE. It also cover the external interrupt on RB0. However this interrupt source comes with most of new version of PIC microcontroller.

There are some registers relate to this interrupt source, IOCB, and INTCON.

Interrupt Control Register - INTCON

This register contains control and flag bit of interrupt-on-change.

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

Set the Global Interrupt Enable bit (GIE) to turn on interrupt for PIC16F887 microcontroller. Bit 3 is called PORTB Change Interrupt Enable bit (RBIE). This bit must be set to turn on the PortB-Interrupt-On-Change. RBIF is PORTB Change Interrupt Flag bit. In the ISR the program must test this bit for its interrupt request.

Interrupt-On-Change PORTB Register - IOCB

This 8-bit wide special function register configures the present of PortB-Interrupt-On-Change.

PIC16F887 Interrupt-On-Change in MikroC
Interrupt-On-Change PortB Register - IOCB
It corresponds to all 8 pins of PortB. It can be individually turn on and off up to the user.

IOCB of PIC16F887 Programming in MikroC

Introduction

There are some more register to make this programming work well. Port B weak pull up resistors must be turned on to save component counts. This job will be done with the OPTION_REGand the WBPU register.

Its internal oscillator with a frequency of 8MHz could fit this simple task. Program will need to configure the OSCCON register to select its 8MHz internal oscillator.



Unlike external interrupt, this interrupt source doesn't have edge selection control bit. Its interrupt flag RBIF is set at the instance of port reading mismatch the previous port value.

Hardware

I made its schematic in Proteus due to its ease of design.

PIC16F887 Interrupt-On-Change in MikroC
Schematic Diagram

SW1 connects to RB7 creates interrupt when it's pressed (shorted to ground). D1 toggles each time the interrupt occurs.

Programming in MikroC

The controller is configured to clock from its internal 8MHz oscillator. Port B input doesn't need external pull up resistor since it's internally connected to its weak pull up resistor by software.

Interrupt-On-Change occurs when the input logic to RB7 change from high to low.

  1. void interrupt() {
  2. if(INTCON.RBIF)
  3. if(PORTB.RB7==0) // check if RB7 is shorted to GND
  4. PORTB.RB0^=1;
  5. INTCON.RBIF=0;
  6. }
  7.  
  8. void main() {
  9. PORTB=0x00; // CLEAR PORTB
  10. TRISB=0x80; // RB7 IS INPUT
  11. ANSELH=0x00; // DISABLE ALL ANALOG FUNCTION
  12. OPTION_REG.F7=0; // ENABLE WPUB
  13. WPUB=0x80; // TURN ON WPUB FOR RB7
  14. OSCCON|=0x70; // SELECT INTRCIO 8MHz
  15. INTCON.GIE=1; // TURN ON GLOBAL INTERRUPT
  16. INTCON.RBIE=1; // TURN ON IOCB
  17. IOCB=0x80; // RB7 IS USE TO CREATE INTERRUPT
  18. INTCON.RBIF=0; // CLEAR INTERRUPT FLAG
  19. while(1); // STAY HERE
  20. }

Both MikroC and Proteus simulation were recorded in this video.


 

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)