Page 1 of 1

12F675 - Please help me solve the I/O riddle

PostPosted: Fri Aug 07, 2015 8:33 pm
by bluetrain
Dear all

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;
}

Re: 12F675 - Please help me solve the I/O riddle

PostPosted: Fri Aug 07, 2015 11:36 pm
by ric
There's one feature common to most PICs that you have missed.
Any pin that has analog input capability, wakes up in analog mode.
This is for a good reason, pins which may have non-digital voltages on them shouldn't power up in digital mode, so it is up to the programmer to switch them to digital mode as required.
("Analog mode" actually means the digital input buffer is disabled, so you cannot read the pin. You will always get zero if you try.)
In your case, on a PIC12F675, the analog input pins are GP0, GP1, GP2 and GP4.
You can tell this, because on the pin diagram in the datasheet, one of the functions of those pins is labelled "ANx".
Also in table 1-1, the PINOUT DESCRIPTION, this is spelt out.
You write to the ANSEL and CMCON registers to control the analog/digital mode.
Have a look at the example code to initialise the port in Example 3-1. It is in assembler, but easily translateable to C as:
Code: Select all
GPIO = 0;
CMCON = 0x07;
ANSEL = 0;

(I have left out "TRISIO = 0b00001100;" as that is application specific.)

Solved: 12F675 - Please help me solve the I/O riddle

PostPosted: Sat Aug 08, 2015 10:20 am
by bluetrain
That solves it nicely. Alas, I've missed that fact big time!
I will re-read the data sheet 'with new eyes' - So many trees... I was missing to see the forest :)

Thank you so much for 'enlightening' me! :idea:

Re: 12F675 - Please help me solve the I/O riddle

PostPosted: Tue Aug 18, 2015 10:59 pm
by Olin Lathrop
bluetrain wrote:I have read my way into using mplab 3.05, xc8 and a pickit3.


Yikes! Did you find that version of MPLAB on cards or punched tape?

Really, especially if you're just starting and have no existing code and build scripts, you should start with the latest version of MPLAB at the time. The latest "old" version is 8, then they did a total re-write and called the new program MPLAB-X. Definitely get the latest MPLAB-X from the Microchip site. It's a free download.

Re: 12F675 - Please help me solve the I/O riddle

PostPosted: Tue Aug 18, 2015 11:19 pm
by ric
Olin Lathrop wrote:
bluetrain wrote:I have read my way into using mplab 3.05, xc8 and a pickit3.


Yikes! Did you find that version of MPLAB on cards or punched tape?

I'm assuming that's tongue in cheek. Carlo plainly means MPLABX 3.05, and as a new user, isn't aware of the difference between MPLAB and MPLABX. :)