PIC18F4550 - LEDs won't light (Beginner)

Discussion in 'Embedded Systems and Microcontrollers' started by Ozette, Dec 19, 2011.

  1. Ozette

    Thread Starter New Member

    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:
    Code ( (Unknown Language)):
    2. #include <stdio.h>
    3. #include <p18f4550.h>
    5. #pragma config WDT = OFF
    7. void main(void)
    8. {
    9. TRISB = 0    //set PORTB bits to output
    11. /* Reset the LEDs */
    12. PORTB = 0;
    14. /* Light the LEDS */
    15. PORTBbits.RB0 = 1;
    16. PORTBbits.RB1 = 1;
    17. PORTBbits.RB2 = 1;
    18. PORTBbits.RB3 = 1;
    19. PORTBbits.RB4 = 1;
    20. PORTBbits.RB5 = 1;
    21. PORTBbits.RB6 = 1;
    22. PORTBbits.RB7 = 1;
    24. while(1);    //do nothing
    25. }
    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.
  2. nerdegutta


    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?
  3. Ozette

    Thread Starter New Member

    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.

  4. hexreader

    Active Member

    Apr 16, 2011

    Code ( (Unknown Language)):
    1. PORTBbits.RB0 = 1;
    Code ( (Unknown Language)):
    1. 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: Dec 19, 2011
  5. Ozette

    Thread Starter New Member

    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.
  6. spinnaker

    AAC Fanatic!

    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?
  7. Ozette

    Thread Starter New Member

    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.
  8. t06afre

    AAC Fanatic!

    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
  9. t06afre

    AAC Fanatic!

    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
  10. Ozette

    Thread Starter New Member

    Dec 19, 2011
    Ok, thank you for your reply. I'll look into it.
  11. thatoneguy

    AAC Fanatic!

    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:

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

    AAC Fanatic!

    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
  13. Ozette

    Thread Starter New Member

    Dec 19, 2011
    Thanks for all the information. I think the pins aren't set for output.
    I'll look into it.
  14. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    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.
  15. Ozette

    Thread Starter New Member

    Dec 19, 2011
    Oh I see, that's really helpful. Thanks alot!
  16. Ozette

    Thread Starter New Member

    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.
  17. nigelwright7557

    Distinguished Member

    May 10, 2008
    I have had no problems just setting/resetting portbbits.
    I thought setting portb also set the latches anyway ?