Close

GPIO Defaults CANNOT read pin input

A project log for operation: Learn The MIPS (PIC32MX1xx/2xx/370)

Having been exclusive to a certain uC-line for over a decade, it's time to learn something new (and port commonCode!)... Enter MIPS

eric-hertzEric Hertz 07/14/2015 at 06:550 Comments

NOTE:

(Update: Now, if I'da just read Tahmid's intro from the start, as well as all the way through its first section, then I'da saved a LOT of time!)

Summary: Put this early-on in your code!

//Pins which are shared with Analog peripherals default to ANALOG by
//default... This causes all READS from PORTx to be 0
//Set ANSELx bits to 0 for PORTx reads to be valid!
//Below takes care of PORTA inputs, use ANSELB, etc. for PORTB...
ANSELA = 0x0000;

Frankly, the documentation is downright confusing at times. Somewhere I'm sure I read that the defaults result in the GPIO pins defaulting to inputs.

But, no... That's not enough... Because if you're *not* planning on using the ADC, you *still* need to look into it... Because later down-the-line it also says something like GPIO pins that are shared with analog peripherals default to ANALOG mode. Which, apparently, explicitly *disables* PORT [input] reads on those pins... So, even if you're *not* planning to use any analog stuff, and just want to read a pushbutton on a pin, you *still* need to acknowledge (and learn the intricate details regarding) the fact that the pin is shared with an analog peripheral... even though those analog peripherals default to OFF.

It took me two days to figure this one out.

The takeaway, here, I guess, is to make sure that all your inputs are set to digital mode, until you decide to use them otherwise. Just do it: ANSELx = 0;



Discussions