Saturday, December 25, 2021

PIC16F877A PortB Interrupt on Change Programming with XC8

 In previous post, I showed the external interrupt programming of PIC16F877A. Here I use another interrupt source of PortB. It's Port B Interrupt on Change. This interrupt creates by any logic change between RB4 and RB7 of PortB.

To use this interrupt source, we must

1- Set any pins between RB4 and RB7 as a digital input.

2- Set its transition

3- Turn on PortB weak pull up resistors (optional)

4- Write the ISR

PIC16F877A PortB Interrupt on Change Programming with XC8
Simulation sample in Proteus 8

In this example, I use all PortB interrupt on change source from RB4 to RB7. These interrupt sources connect to input buttons. They will toggle RB1 and RB2. RB0 blinks an output LED regularly.

  1. /*
  2.  * PIC16F877a Port B Interrupt On Change
  3.  * Example in xc8
  4.  */
  5. #include <xc.h>
  6.  
  7. // PIC16F877A Configuration Bit Settings
  8. // CONFIG
  9. #pragma config FOSC = HS
  10. #pragma config WDTE = OFF
  11. #pragma config PWRTE = OFF
  12. #pragma config BOREN = ON
  13. #pragma config LVP = OFF
  14. #pragma config WRT = OFF
  15. #pragma config CP = OFF
  16.  
  17. #define _XTAL_FREQ 20e6
  18.  
  19. void main(void){
  20. /*Clear IO port*/
  21. PORTB=0x00;
  22. /*RB0...1 input*/
  23. TRISB=0xF0;
  24. /*Turn on Pull Up Resistor*/
  25. nRBPU=0;
  26. /*Turn On External Interrupt*/
  27. RBIE=1;
  28. /*Turn On Global Interrupt*/
  29. GIE=1;
  30. /*Clear Interrupt Flag*/
  31. RBIF=0;
  32. while(1){
  33. RB0^=1;
  34. __delay_ms(500);
  35. }
  36. }
  37.  
  38. /*Interrupt Service Routine*/
  39. void interrupt portBChange(void){
  40. /*Check RB Port Change Interrupt Present*/
  41. if(RBIE&&RBIF){
  42. if(RB4^1) RB1=1;
  43. if(RB5^1) RB1=0;
  44. if(RB6^1) RB2=1;
  45. if(RB7^1) RB2=0;
  46. RBIF=0;
  47. }
  48. }

Its clock source is 20MHz from a crystal oscillator.

PIC16F877A PortB Interrupt on Change Programming with XC8
Schematic Diagram

Click here to download source file.

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)