I have used RB state change interruption to turn on two LED connected to portA (pin4 and 5). The probem is when interruption occurs LED on pin5 turn on, but not LED on pin4. When I change pin4 to pin3 it works normally, but I can't use this pin, because my circuit is already printed.

I've read the datasheet, and I understand that PORTA pin4 is multiplexed with Timer0, is that causing the problem? How do I disable that?

XC8 compiler code:

#define _XTAL_FREQ 20000000

#include <xc.h>

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial 
                            Programming Enable bit (RB3 is digital I/O, HV 
                            on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data 
                             EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write 
                            protection off; all program memory may be 
                            written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code 
                           protection off)

int main()
   TRISA = 0x00;    //set portA as output
   TRISB = 0xFF;    //set portB as input    
   TRISD = 0xFF;    //set portD as input    
   TRISC = 0xFF;    //set portC as input
   ADCON0 = 0x00;   //disable AD converter
   ADCON1 = 0b10000110; //disable AD converter
   OPTION_REG = 0b00000000; //disable AD converter
   INTCON = 0b11001000;     //enable port b pullups
   PORTA = 0x00;        //turn off LEDs

  return 0;

void interrupt isr(void)
   if (INTCONbits.RBIF) {
   if (PORTB) {
   INTCONbits.RBIF = 0; //clear interrupt flag
   PORTAbits.RA4 = 1;   // turn on bottom LED
   PORTAbits.RA5 = 1;   // turn on top LED

enter image description here


1 Answer 1


RA4 on that microcontroller is an open drain output. This means that when you set it as low, the output will be low; but when you set it high the output will be tristated (high impedance).

You will need to change your LED configuration to turn it on with an open drain.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.