Hi I am facing some problem with Reading the input from a PIC18f4550 , for a simple arrangement where when a switch on (switch = 1), few led should glow., and on releasing the switch the led should go off (switch = 0),. On providing some volts across a pin (RA0 or RA1)- the leds should glow but I am getting no success in doing so. I am using MPLAB IDE AND C18 compiler. tried with MPLABX and XC8 compiler too.

I wanted to take input from RA0 and RA1 , with a two push switch. And glow leds on RD7-RD4. I made a simple if then else statement, inside a while(1) loop to do so.

    #include <p18f4550.h>
#define switch1  PORTAbits.RA0   // two switches
#define switch2  PORTAbits.RA1
#define led1    LATDbits.LATD7   // defining led’s for making life easy
#define led2    LATDbits.LATD6
#define led3    LATDbits.LATD5
#define led4    LATDbits.LATD4

void main (void)
{
// IO settings
/* tired both ways*/

TRISbits.RA0=1   //making input for switch 1
TRISbits.TRISA1=1  //making input for switch 2

TRISDbits.TRISD7 = 0;   // RD7 to RD4 set to output for led.
TRISDbits.TRISD6 = 0;
TRISDbits.TRISD5= 0;
TRISDbits.TRISD4 = 0;

ADCON1bits.PCFG0 = 1;        // making all pins digital.
ADCON1bits.PCFG2 = 1;     // referred Data Sheet Page:262

while(1)
{      if(switch1 == 1)
{    led1 = 1;
led2 = 1;
led3 = 1;
led4 = 1;          }

else if (switch1 == 0)  // on reading no voltage
{     led1 =  0;
led2 =  0;
led3 =  0;
led4 =  0;  }
else { }
}
}


It did not work out well.. the leds on RD7-RD4 keeps glowing all the time no matter what I do.

I tried to change the

        PORTAbits.RA0   to  LATAbits.LATA0  but still no help.


I did little bit of research and someone recommended me to turn the ADC value off and after looking up datasheet setting. I turned the adc off

        ADCON0bits.ADON = 0;  //turn off the ADC


Checked with the circuit on breadboard. Everything but still situation same. Someone further recommended that , it’s a better idea to turn off the comparator. Hence I turned off the comparator again.

     CMCON = 0x07;     // comparator off.



I built entire thing again from scratch but still no success. I push the switch on or off. The leds on RD7-RD4 only of port D keeps glowing all the time :’( .. Here I am please help me.

PS: [Just to add up, I also thought may be my pic18f4550 has gone bad. Hence I tested a code to blink led on pic18f4550 http://goo.gl/PosRgg from a website it worked fine… hence I am sure my pic184550 is has not gone bad.] Please help and recommend where I gone wrong, i have been trying since 3 days now :'(

• How exactly have you connected your switch to the processor? Does the line with "if (switch1 = = 0)" really have a space between the two equal signs? Dec 12 '13 at 14:24
• it would give an error if there would be any space in between "switch == 0" on mplab ide. The Switch is connected with VSS of the pic18f4550 5v. when the pin in ON - 5v goes into RA0 of the microcontroller. Dec 12 '13 at 14:32
• Sorry, but I don't understand how you have connected the switch. It's connected with VSS of the 5V? What does that mean? Is there a pullup or pulldown resistor? Dec 12 '13 at 14:44