New to PIC, I am enthusiastically trying to apply the small 8-pin chip to some simple model model railroad controlling chores. I need a few inputs and a few outputs. I have read my way into using mplab 3.05, xc8 and a pickit3. Flashing the chip works fine. I use +5.0 V VDD supply, input pins have an external 10k pull-up to VDD fitted and I can pull them to either GND or VDD with a dedicated wires on my breadboard test setup.
If I configure GP0 as output and the other five pins as input. Two LEDs on GP0 light either green if '1' or red if '0'. The program is a minimal loop, showing static '1' = green on GP0, if all input pins are open (= '1') and oscillating GP0 '0/1' = both red+green, if any one input pin is '0'. Now, here is my problem - I can only read the input pins GP3 and GP5. GP1, GP2 and GP4 always just read '0', regardless of the actual voltage level (GND/+5V) at those input pins. In other words, I have to comment-out the code lines for GP1,2,4 to get the program 'working' and showing green only if no pin is pulled to GND.
To check the HW setup and PIC chip, I am flashing an existing program HEX file into the chip using above HW setup unchanged. This code works perfect and it uses GP2 and GP3 as inputs and the rest as output. This HEX was built with PicSimulator IDE basic ( http://www.oshonsoft.com/ ).
By searching for errors, I have pared down the code to a minimum number of instructions, using defaults otherwise. I am aware, that it no longer reflects good programming practice. Please note that I have already worked through separate I/O-register variables to avoid any RMW effects, to no effect. I have run out of juice - is there anybody to point me to my error(s)? Being a newbie, it could be fairly basic, I know...
Thanks a lot - Carlo
- Code: Select all
#include <xc.h>
#pragma config FOSC = INTRCIO
#pragma config MCLRE = OFF
int main(void)
{
TRISIObits.TRISIO0 = 0; // GP0 is output
while(1) {
GPIObits.GP0 = 1;
if (GPIObits.GP1 == 0) { GPIObits.GP0 = 0; } //reads always '0'
if (GPIObits.GP2 == 0) { GPIObits.GP0 = 0; } //reads always '0'
if (GPIObits.GP3 == 0) { GPIObits.GP0 = 0; }
if (GPIObits.GP4 == 0) { GPIObits.GP0 = 0; } //reads always '0'
if (GPIObits.GP5 == 0) { GPIObits.GP0 = 0; }
}
return 0;
}