PIC18F2550 w/ XC8 Compiler, Cannot read from pins propperly.

Status
Not open for further replies.

secretagent

New Member
Hi guys!

I'm using this PIC and for some reason I can output to RA0,RA1 etc. I cannot read from them though like this:
Code:
RA0=~RA0;
I have to do:
Code:
LATB0=~LATB0;
Which is obviously the last written bit...

Even with TRISA=0xFF, The input doesn't change, Even with debugging on I can't get an input, I can tell because the LED doesn't turn on when I hit the switch, and the variable doesn't change in the PIC.

Any help would be awesome! Thanks!

Code:
Code:
#include <xc.h>
#pragma config  PLLDIV = 12, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HS, FCMEN = OFF, IESO = OFF
#pragma config  PWRT = ON,  BOR = OFF, VREGEN = ON, WDT = OFF, PBADEN = OFF,  LPT1OSC = OFF,  MCLRE = OFF
#pragma config  STVREN = OFF,  LVP = OFF, DEBUG = ON


void main(void)
	{
    char x=0;//for debugging
		ADCON0=0x0F;
		CMCON=7;
		PORTA=0;
		PORTB=0;
		TRISA=0xFF;
		TRISB=0;
		while(1)
                {
                    x=RA0;
                    if(RA0==1)
                    {
                        LATB0=~LATB0;
                        while(RA0==1);
                    }

                }
	}

Code should be and wanted:
Code:
#include <xc.h>
#pragma config  PLLDIV = 12, CPUDIV = OSC1_PLL2, USBDIV = 2, FOSC = HS, FCMEN = OFF, IESO = OFF
#pragma config  PWRT = ON,  BOR = OFF, VREGEN = ON, WDT = OFF, PBADEN = OFF,  LPT1OSC = OFF,  MCLRE = OFF
#pragma config  STVREN = OFF,  LVP = OFF, DEBUG = ON


void main(void)
	{
		ADCON0=0x0F;
		CMCON=7;
		PORTA=0;
		PORTB=0;
		TRISA=0xFF;
		TRISB=0;
		while(1)
                {
                    if(RA0==1)
                    {
                        RA0=~RA0;
                        while(RA0==1);
                    }

                }
	}

Thanks!
 
Last edited:
It is completely unrelated to the compiler. ADCON1 (bits PCFG3:0) control whether AN0 to AN12 are analogue or digital. There are two possible reset values depending on a configuration word, this is all explained in the datasheet.

You will notice from page 13 that RA0 shares the pin with AN0. When the chip starts this pin will always be analogue. You need to set ADCON1 accordingly to set the pins to digital. Read page 260, set the bits correctly and see if that works.
 
Glad you fixed it. I haven't got round to trying XC8 yet.. but am looking forward to the opportunity.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…