2
\$\begingroup\$

I wrote a program to turn on LEDs on PORTB. The programmer erased, programmed, verified, so far so good. But then nothing happened. No lights. My programmer is a MPLAB Compatible Mini USB PIC Programmer.

What are some things that novices miss?

Below is my code:

#include <p18f4550.h> 
#pragma config WDT = OFF 
void delay (void) 
{ 
  unsigned int i; 
  for (i = 0; i < 65535 ; i++) ; 
} 

void main (void) 
{ 
  TRISB = 0; 
  while (1) 
  { 
    PORTB = 0; 
    delay (); 
    PORTB = 0xFF; 
    delay (); 
  } 
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Please provide us with which PIC chip and programmer you are using. \$\endgroup\$
    – W5VO
    Jan 9, 2011 at 5:52
  • 2
    \$\begingroup\$ Can you post your code? \$\endgroup\$
    – BG100
    Jan 9, 2011 at 8:38
  • \$\begingroup\$ Some PortB pins are shared with the ADC. They need to be configured for digital I/O. \$\endgroup\$ Jan 10, 2011 at 14:12

4 Answers 4

5
\$\begingroup\$
  • Did you forget to set the LED pins to output?
    • Each pin you want to drive a LED must be set as an output in your code. This involves setting the TRIS bits for that pin to '0'.
  • Did you connect the LED backwards?
    • LEDs will turn on only when the anode-cathode voltage is positive.
  • Do you have a clock source?
    • Make sure you're using a clock source that's hooked up, or the internal RC oscillator. Without a clock, your program won't run.
  • Do you have the LED connected to the correct pin?
  • Is power applied to the circuit?
  • Do you have bypass capacitors for the PIC?
  • Is there a resistor pull-up for MCLR?
    • Without this pull-up, your chip will stay in reset and never execute your program.

Check your wiring again. Wiring and connection errors are very common in beginner's circuits.

\$\endgroup\$
6
  • \$\begingroup\$ If you still have the programmer connected when you try to run the program you will have to "release from reset" from mplab. If you don't do this it doesn't matter if you have a resistor pull-up for MCLR it will still be pulled down. \$\endgroup\$
    – Kellenjb
    Jan 9, 2011 at 18:41
  • \$\begingroup\$ To W5VO: Here's the code: #include <p18f4550.h> #pragma config WDT = OFF void delay (void) { unsigned int i; for (i = 0; i < 65535 ; i++) ; } void main (void) { TRISB = 0; while (1) { PORTB = 0; delay (); PORTB = 0xFF; delay (); } } \$\endgroup\$
    – rarque
    Jan 10, 2011 at 1:48
  • \$\begingroup\$ Programmer is a MPLAB Compatible Mini USB PIC Programmer. MCLR is always low when ICSP cable connected. Gotta yank it out to run? \$\endgroup\$
    – rarque
    Jan 10, 2011 at 1:51
  • \$\begingroup\$ no, you should be able to run it with the ICSP cable still plugged in, IF you use Debug mode in MPLAB. Otherwise, if you build the project as Release, you will have to pull the cable out. \$\endgroup\$
    – Isaac
    Jan 10, 2011 at 13:28
  • \$\begingroup\$ @Isaac That contradicts my experience with MPLAB. If you build it as a release, there is a button in MPLAB that will allow you to release the programmer reset. No cable removal necessary. \$\endgroup\$
    – W5VO
    Jan 10, 2011 at 16:04
2
\$\begingroup\$

Most common is using pins that are shared with the ADC. You need to enable them for digital I/O as they are analogue inputs by default.

Quote from PIC18F2455/2550/4455/4550 datasheet:

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.

That is probably the cause of the problem. Some of the PortB pins of the PIC18F4550 are shared with the ADC.

\$\endgroup\$
1
\$\begingroup\$

"What are some things that novices miss?"

  • First check your hardware. Find a blink-a-led.hex file that is known to work and verify that your hardware works. (I have some on my website, Google is your friend)
  • You have disabled the watchdog, that is a good start, but there are some more fuses settings that are need, especially the oscillator (XP, HS, internal, etc).
  • Configure the relevant pins as digital pins (check the A/D and comparator modules)
  • Set the pin direction (TRIS, you did so)
  • Avoid the Read-Modify-Write (RMW) curse (write the whole PORT or use shadow or use LATx)
\$\endgroup\$
0
\$\begingroup\$

All of these answers have valid potential issues, especially Leon's point that analog pins can be tricky and W5VR's point about MCLR not being pulled high. Also - depending on your chip, you may need to write to the LATB register for output and read from the PORTB register for input. Docs on the difference between LATB and PORTB are iffy and in the pst I've had to use some trial and error to make it work.

LATB is the port B output latch. This will contain the most recent values that you wrote into the latch. Under some conditions (say, your output pin is heavily loaded) the actual value on that pin may drop quite low. This can cause an erroneous reading of PORTB, i.e. it will return 0 when it should return 1. Thus, it's safer to read LATB because this will still contain the 'correct' value.

@kellenjb: You should be able to run the program without removing the ICSP cable, IF you use Debug mode in MPLAB. Otherwise, if you build the project as Release, you will have to pull the cable out. Debug mode may not be supported for OP's programmer; it works on my Pickit2.

\$\endgroup\$
1
  • \$\begingroup\$ The delays should avoid RMW problems. LATches should be used for output and PORTs should be used for input. \$\endgroup\$ Jan 10, 2011 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.