# Basic code to switch LED for PIC18f4550 C18 not working

Ok, I'm sure it's something stupid but this is probably the 20th variant of this code and I can't get it figured out. I simply want a (debounced) switch to trigger an LED on the PIC18f4550. Should I be using a different port for the switch? Here's my code:

void main(void)
{

TRISBbits.TRISB0 = 1;    //switch input
TRISBbits.TRISB3 = 0;    // LED output
PORTBbits.RB3 = 0;       // make LED low by default
TRISEbits.TRISE2 = 1;    // alternate switch input

while (1) {                       // doesn't work for some reason
if (PORTEbits.RE2 == 1) {
LATBbits.LATB3 == 1;
}
else LATBbits.LATB3 == 0;
}
}


Tried to post the config code but it screwed up the formatting here.

EDIT: sorry for the ugly formatting, but here are the config settings. Had to change some things to keep the formatting from getting weird.

include stdio.h
include stdlib.h
include p18f4550.h
include delays.h

// chip config     ////////////////////////////////////////////////////////////////////////////////////

pragma config PLLDIV = 5
pragma config CPUDIV = OSC1_PLL2
pragma config USBDIV = 2

// internal clock, pin #14 (RA6) as I/O pin, pin #13 unused,
pragma config FOSC = INTOSCIO_EC    // if OSCCON is left as default clock speed will be 1Mhz

// now the other less confusing options . . .
pragma config FCMEN = OFF
pragma config IESO = OFF
pragma config PWRT = OFF
pragma config BOR = OFF
pragma config BORV = 3
pragma config VREGEN = OFF
pragma config WDT = OFF
pragma config WDTPS = 32768
pragma config CCP2MX = ON
pragma config PBADEN = OFF
pragma config LPT1OSC = OFF
pragma config MCLRE = ON
pragma config STVREN = ON
pragma config LVP = OFF
pragma config ICPRT = OFF
pragma config XINST = OFF
pragma config DEBUG = OFF

-
What compiler are you using? – Dave Tweed Oct 15 '12 at 22:12
sorry, mplabx with c18 compiler. – Mark Oct 15 '12 at 22:23
Thanks for the edit M.Alin! – Mark Oct 15 '12 at 22:57

## 2 Answers

You didn't show the external circuit, but if you just have a switch connected between the input pin and ground (or power), then that's not enough. That will drive the pin one way when the switch is closed, but leave it floating when open. There is no guarantee what state it will be in when left floating. To fix this, tie a pullup or pulldown to the pin that pulls causes it to go to the opposite polarity when the switch is open.

For example, tie the switch between the pin and ground, and then the pullup between the pin and Vdd. use 1 - 10 kΩ for the pullup.

Some pins of that PIC can be configured for internal pullups. RB4-RB7 can but possibly others on that PIC (your job to check the datasheet). If you enable the internal pullup, then the only external part you need is the switch between the pin and ground.

-
 Well, I think you are on the right track here. I had the switch (to ground) debounced but not pulled up and I was seeing the behavior of floating voltages on pins. really weird stuff. after hours of troubleshooting, somebody (who shall remain nameless) got careless and rather than run the 9v though the voltage divider, sunk it straight to the chip. If it wasn't acting weird before, it surely was now. – Mark Oct 17 '12 at 0:23 eventually, the device ID errors arrived and the chip couldn't be programmed anymore. Replaced the 4550 with a spare and voila. All seems to be working as planned. Thanks all for the help. We idiots really appreciate it. – Mark Oct 17 '12 at 0:25 Apparently, the datasheet really means it when they say max input Vdd = 7.5V! (embarrassed) – Mark Oct 17 '12 at 0:41

The pins you are using are also analogue inputs, so you need to make sure these are off. Also turning the comparators off is a good idea, in case you want to use RA0 to RA5.

The code is:

CMCON = 0x07;
ADCON1 = 0x0F;

Read the section on the IO Ports and the ADC, it tells you all you need to know.

-
 but shouldn't PBADEN = off take care of RB0? thanks. – Mark Oct 15 '12 at 22:22 Looks like it should do, yes. Try it and see though. Also you may want to add CCP2CON = 0x00; and UCON = 0x00 – Oli Glaser Oct 15 '12 at 22:32 @Mark - have you verified the chip is configured correctly? (i.e. have you had any code at all running on it yet?) – Oli Glaser Oct 15 '12 at 22:33 yes sir. I did a nice LED sequencer using ports C&D without much fuss and these same configurations. I'm simply trying to add a switch to trigger that sequence but have commented it out while I troublshoot this. I'll try to post my config info. – Mark Oct 15 '12 at 22:41 @Mark - it does say for RE2 that on power up reset RE0 to RB2 are configured as analog inputs (datasheet p.125). Also, just try setting the LED high by default (or toggling it with a simple loop) just to confirm everything is as it should be power/connections wise. – Oli Glaser Oct 15 '12 at 23:10