Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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:
Check register ADCON1 on page 260 of the datasheet. You need to initialise that also.
 
GOT IT WORKING! Thankyou so much the problem was I was writing to adcon0 not adcon1! I am happy :p
 
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.
 
oh, I thought they were always Digital, And I guess it was a typo :/
 
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top