SX Microcontroller port pins

"When I set a port pin as an input, then ground & unground it with a switch, it sometimes works & other times doesn't work."
You have a floating input; You want to use a pull-up or a pull-down resistor, if you are driving this pin with an open drain or a switch to Vcc or Ground, directly. 10k is a good value here. See Extra, unused pins

SX Microcontroler Port Setup

SX Microcontroller Input / Output Port Expansion