# PIC16F15345 Microcontroller, RA5 Digital Input Pin Issues

I've been having a very strange problem with my PIC16F15345 microcontrollers. I'm trying to read a digital input state from a specific pin on the microcontroller (Pin2, or RA5). My setup code is below:

#include <xc.h>
#include <pic.h>
#include <pic16F15345.h>

#define _XTAL_FREQ 32000000

#pragma config WDTE = OFF

void main(void){
OSCFRQbits.HFFRQ = 0b110; //set HFINTOSC to 32MHz
OSCCON1bits.NOSC = 0b110; //set clock to HFINTOSC
TRISA = 0b11111111; //set all portA bits to input
ANSELA = 0b00000000; //set all portA bits to digital input
while(1)
{
//loop forever;
}
return;
}


I'm using the latest XC8 compiler, MPLABx IDE, and a PICKIT3 programmer. I'm using an arduino's output pins to toggle the state of the input pin on the PIC, so I don't have to worry about debouncing and can trigger state changes via software.

If I want to measure RA4, or pin3, I can use the debugger to check the value of the PORTA register, and it shows a change of state corresponding to what I'd expect. PORTB and PORTC are also working normally.

However, for some reason, RA5 will always read 0, no matter what the input voltage at the pin is (I've verified 4.8V at the pin with a multimeter). At first I thought maybe I damaged my input via ESD, so I changed to another IC of the same model (I ordered 3 from Digikey), and the problem persisted. I changed to the 3rd and final chip, but the problem was still there.

I've been trawling the microchip forums for answers, but no avail so far. Any advice is appreciated!

• You are not indeed reading nothing anywhere. I would expect something within the while(1) loop. – aluis.rcastro Oct 9 '17 at 6:00
• To clarify, stuff would normally be going on in the main loop of the program, but I cut it out for clarity's sake here. I'm using the debugger in the IDE to view the registers, not a variable in the program itself (I've tried that also, but it doesn't change the results) – Platytude Oct 9 '17 at 6:14

http://www.mouser.com/ds/2/268/40001865B-1082596.pdf Check TABLE 1-2: PIC16(L)F15325 PINOUT DESCRIPTION (CONTINUED) Only Select GPIO function Disable all other functions.

• Could you clarify slightly? The setup code would indicate that I have the pin set up to be a GPIO only, no? None of the other modules that this pin is interfaced to have been initialized. Even so, RA4 has similar extra functionality, but it's working just fine. – Platytude Oct 9 '17 at 6:21

Solved it myself after some more digging. Turns out the RA5 pin will default to the external oscillator input by default, and to allow GPIO function you need to add the following line to the beginning of your code:

#pragma config FEXTOSC = OFF


This resolved the problem for me.

on my 12F1822 RA5 also defaults to the external oscillator input. if FOSC.intsoc is set to 1 and OSCCONbits.SCS = 00;, it does not solve the problem. You must also put in a delay after OSCCONbits.SCS = 00;. This is because when the Oscillator pins is instructed to be switched from the default setting, the process takes some time to take effect.

• Welcome to EE.SE, Robert. Just a tip: capitalise and punctuate your sentences properly. Your answer lacks credibility because it is carelessly written. Your compiler won't let you away with it. Why should we? – Transistor Mar 8 '18 at 13:38