1
\$\begingroup\$

I have just begun in the world of microcontrollers, and the first microcontroller. I chose to work with was the PIC from Microchip. The PIC I am using is the PIC16F877A. I am using MPLAB IDE and HI-TECH C. I am trying to make an extremely simple program that turns on an LED. This is the code I am using:

#include<htc.h>
#define _XTAL_FREQ 8000000
__CONFIG(UNPROTECT & PWRTDIS & WDTDIS & HS & LVPDIS); 

int main()
{
    TRISB0 = 0;
    RB0 = 1;
    while(1);
}

When I hook up the PIC to my circuit the LED does not turn on. Here is my circuit diagram and a picture of my circuit:

enter image description here

enter image description here

A few other notes about my circuit:

  • I am using a 9 volt battery hooked up to a 7805 regulator for the power supply.
  • I have measured the voltage coming from pin RB0 with a multimeter and it measures 0.0 V.
  • If there is no problem with my circuit I could have programmed the chip wrong.
  • My capacitors hooked up with my crystal are 22 pf, not 22 µf as in the schematic.
  • I have put 100 µf capacitors between pins 11 and 12 and between 31 and 32.
\$\endgroup\$
  • 2
    \$\begingroup\$ Do you have a current limiting resistor for the led? A led with no resistor is like a short, either remove the led and use the multimeter to check the pin state or add a resistor in series with the led (try 270-330 Ohm) \$\endgroup\$ – alexan_e Jan 30 '14 at 22:49
  • 1
    \$\begingroup\$ i am using a red with a built in current limiting resistor \$\endgroup\$ – popgalop Jan 30 '14 at 22:51
  • 2
    \$\begingroup\$ So the breadboard image you have provided is not your actual circuit? It shows a green led and no capacitors for the crystal (or are hidden underneath) \$\endgroup\$ – alexan_e Jan 30 '14 at 22:52
  • 1
    \$\begingroup\$ So let me get this straight: 1) You are able to write and read successfully. 2) Those load capacitors next to the crystal are surely not 22uF... \$\endgroup\$ – Dzarda Jan 30 '14 at 22:53
  • \$\begingroup\$ 1. Yes i can read and write correctly 2. I meant 22pF \$\endgroup\$ – popgalop Jan 30 '14 at 22:55
1
\$\begingroup\$

As it's impossible to put code in comments, I'll put my suggestion for code that sets the Config bits correctly here. The issue is I don't have this compiler so I cannot be certain of the names (LVPDIS as opposed to, say, LVP_OFF in assembly) but if I'm wrong, someone could comment(?)

#include <htc.h>

#define _XTAL_FREQ 8000000

__CONFIG(UNPROTECT & PWRTDIS & WDTDIS & HS & LVPDIS);

int main()
{
    TRISB0 = 0;
    RB0 = 1;
    while(1);
}
\$\endgroup\$
  • \$\begingroup\$ i updated my code to this and the same problem still occurs \$\endgroup\$ – popgalop Jan 31 '14 at 0:42
  • \$\begingroup\$ Sorry to hear this - my first PIC project was with a PIC16f88 which has an internal oscillator. As a last resort you could try the RC oscillator as shown on pg 146 of the datasheet. Change HS in the __CONFIG to RCCLK (I think) and use your 22pF capacitor with a 100k resistor. The oscillator will be in the 100s of kHz but something (anything!) might start! \$\endgroup\$ – carveone Jan 31 '14 at 11:02
1
\$\begingroup\$

Have you tried checking the clko pin to see if you are getting a clock signal? Hook a scope up to it and make sure the chip is even running. If you don't have a scope or usb logic analyzer, you might be able to see an LED glow slightly from that pin.

Have you measured the Vdd to make certain you are supply the correct power level (between 4.0 and 5.5)?

As RB0 is also an external interrupt pin, try moving the LED to pin34 and changing your code to use RB1 instead:

TRISB1 = 0;
RB1 = 1;
\$\endgroup\$
  • \$\begingroup\$ I dont have a scope, my multimeter says 5.01V as power level \$\endgroup\$ – popgalop Jan 31 '14 at 1:14
  • \$\begingroup\$ try another pin (updated above) \$\endgroup\$ – NateFisher Jan 31 '14 at 1:18
  • 1
    \$\begingroup\$ What does your multimeter say if you measure pin 14 (to ground)? How did you program the chip, and did it verify? \$\endgroup\$ – Spehro Pefhany Jan 31 '14 at 1:42
  • \$\begingroup\$ It actually worked on pin RB1 for some reason, that seems odd \$\endgroup\$ – popgalop Jan 31 '14 at 11:16
  • \$\begingroup\$ You could have a few things going on. You may need to disable all interrupts to use RB0 (GIE, PIE, and the external pin interrupt). Also, not sure if that pin is also an ADC pin, but if that's the case, you should clear the ANSEL register to make it a digital I/O. \$\endgroup\$ – NateFisher Jan 31 '14 at 16:44
1
\$\begingroup\$
  1. There is no bypass cap in sight. Put 100 nF to 1 µF ceramic directly between each power pin and ground.
  2. Showing only the pin numbers doesn't show us the pin function. It's impossible to see what you have connected to what pin without looking up the pinout in the datasheet. This information should be in your schematic.
  3. 22 µF is about 6 orders of magnitude too much capacitance for the crystal and its drive circuit. This will definitely keep the oscillator from running, and therefore the PIC from running.
  4. There is no resistor in series with the LED, so it will draw too much current. That can load down the supply, damage the LED, and/or damage the PIC pin output driver.
  5. Note that any pin that can be a analog input wakes up that way by default. I don't remember off the top of my head whether the pin you are trying to use is one of them, but it's a good idea to go to the A/D and/or comparator modules and make sure all pins are configured as digital.
\$\endgroup\$
  • \$\begingroup\$ +1 for commenting about needing the pin function in addition to showing the pin numbers. \$\endgroup\$ – tcrosley Aug 29 '14 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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