PIC18F4550 - LEDs won't light (Beginner)

Thread Starter


Joined Dec 19, 2011
Dear community,

I'm pretty new to microcontroller programming. So, please correct me if I say anything weird. And I'm new to the forums - hello, let's get along well allaboutcircuits community.

I'm still figuring out the basics of microcontroller programming, but I've read several guides here and there and wanted to experiment with some of them.

I'm using a PIC18f4550. (link datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf)

It can be recognized by the computer as a Human Interface Device.
USB connected to pins 24(D+), 23(D-), 32(Vdd) and 12(Vss).

I have 8 LEDs connected to pins 33(RB0) up to 40(RB7). If I'm not mistaken these are PORTB pins?

I'm using MPLAB with the mplabc18 compiler. I've written a small program, it's supposed to light the LEDs but they won't light and I don't know why.

In my project I've included the pic18f4550.h header file and a rm18f4550.lkr linker script.
I'm still not sure about the minimum files requirements which I should include to the project are. Are the header file of the pic and the linker script all?

Anyway, other than these two files, ofcourse my main:
Rich (BB code):
#include <stdio.h>
#include <p18f4550.h>

#pragma config WDT = OFF

void main(void)
TRISB = 0    //set PORTB bits to output

/* Reset the LEDs */
PORTB = 0;

/* Light the LEDS */
PORTBbits.RB0 = 1;
PORTBbits.RB1 = 1;
PORTBbits.RB2 = 1;
PORTBbits.RB3 = 1;
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 1;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 1;

while(1);    //do nothing
This code builds fine and after I put the hex on my microcontroller and disconnect it from my computer,
I attach the circuit to a 5V adapter. Only the 7th LED lights, not even the 8th, as if it's only indicating there's current on the board.
I don't know what I'm doing wrong and some help would be greatly appreciated.

If anything is unclear or if I've provided unsufficient information please let me know.

Thanks for reading.


Joined Dec 15, 2009
After just a quick glance at the code:

Don't you think you need more configuration bits than only turning the WatchDogTimer off?

What about oscillation?

Do you have a schematic?

Thread Starter


Joined Dec 19, 2011
Hi nerdegutta, thanks for your reply.
I'm not too sure about configuration bits yet, but I can provide a schematic. I'm afraid it's not too clear though. It's not my schematic, I'm a student.



Joined Apr 16, 2011

Rich (BB code):
PORTBbits.RB0 = 1;
Rich (BB code):
LATBbits.LATB0 = 1;
And same for all other bits.

For PIC18 and above always write to LAT, read from PORT.

Look up RMW or read-modify-write for the explanation.

No need to include linker file for current version of MPLAB.
You must always include #include <p18f4550.h> or maybe #include <p18cxxx.h> to cater for a possible change of PIC18 in your project.

Hope this helps
Last edited:

Thread Starter


Joined Dec 19, 2011
Thanks for your reply, this should work I think. But it did not work for me.
Still only the 7th LED that's on.

Thank you for the answer about the linker file.


Joined Oct 29, 2009
You should really set latch bits and not ports.

LATBbits.LAT12 = 1; and so on

You can set them all like this:

LATB = 0xff;

Do you have a logic probe, scope, DMM? What does the value of each pin measure? Hi or Lo?

Thread Starter


Joined Dec 19, 2011
Hi spinnaker, thanks for your reply. Unfortunately it doesn't work.
I will check my hardware and try some other things now. I will let you know what I have done if it works. If anyone still has a word of advice I'd like to hear that.


Joined May 11, 2009
Make it habbit of setting all configuration bits. You may use MBLAB for this as short cut only. The configuration bits shall always be set by code for documentation purpose. se section 25.0 Special Features of the CPU


Joined May 11, 2009
It looks like then not set by config words. FOSC3:FOSC0: Oscillator Selection bits will be set to 0101. That mean your PIC is expecting external oscillator source. See section 25. Your micro is dead in the water so to speak


Joined Feb 19, 2009
If one light is working, the clock may be working.

First, look at the PIC18F4550 Datasheet and determine what other on chip peripherals use those pins.

Never forget this: Ports with analog functions will default to ENABLED (Analog) on power up! This applies to all PICs. This is a stumbling block that everybody runs into, even people that have worked with PICs for a long time will forget when using a different PIC than they are used to.

Fix that, and put _XTPLL (or similar for Crystal, I think you have a Crystal Osc in that tiny schematic) in the #config line and you should be good to go.

Here is Microchip's suggestion for setting up PORTB:

Rich (BB code):
Note: On a Power-on Reset, RB4:RB0 are
configured as analog inputs by default and
read as ‘0’; RB7:RB5 are configured as
digital inputs.
By programming the Configuration bit,
PBADEN (CONFIG3H<1>), RB4:RB0 will
alternatively be configured as digital inputs
on POR.
Rich (BB code):
CLRF PORTB ; Initialize PORTB by
; clearing output
; data latches
CLRF LATB ; Alternate method
; to clear output
; data latches
MOVLW 0Eh ; Set RB<4:0> as
MOVWF ADCON1 ; digital I/O pins
; (required if config bit
; PBADEN is set)
MOVLW 0CFh ; Value used to
; initialize data
; direction
MOVWF TRISB ; Set RB<3:0> as inputs
; RB<5:4> as outputs
; RB<7:6> as inputs


Joined May 11, 2009
Your PIC also do have an Internal oscillator block. So you do not need any crystal. If you do not have a premade demo board on PCB. But use bread board. I strongly recommend using the internal oscillator. At least in the start. A crystal oscillator may not work so good on some bread board. But the Internal oscillator. Is very fail safe


Joined Feb 19, 2009
Thanks for all the information. I think the pins aren't set for output.
I'll look into it.
Setting the pins for output isn't enough for pins with analog functions. You need to disable the analog device first, such as the comparator, or the internal peripheral will not let the state be set. Most often, the minimum is CMCON or CMCON0 and CMCON1. The other common "blocks" are the USB D+ and D- ports.

Thread Starter


Joined Dec 19, 2011
I got part of my program working by using some other example code which included a user.h and contains a user.c with some basic tasks.
I still don't know what I did wrong unfortunately, but at least I learned about rmw and the LATbits.

Thanks for the help, happy new year.