Problem with PIC16F876A - LED Blink

Status
Not open for further replies.

amith.srivatsa

Junior Member level 1
Hello everyone,

I am still a beginner in Microcontroller programming. I want to make LED blink using a simple C code - using PIC16F876A, compiled in CCS.

The code is:
Code:
##include <16F876A.H>
#fuses HS,NOWDT, NOPROTECT, NOBROWNOUT, PUT,NOCPD,NOPUT
#use delay(clock=1000000)

//====================================
void main(){

while(1)
{
output_high(PIN_C5);
delay_ms(1000);
output_low(PIN_C5);
delay_ms(1000);
}
}

My problem is that: The LED is blinking perfectly when I power up using PIC KIT 2. But, when I power up using my circuit, it doesn't blink. I am using 8MHz crystal (HS mode). Also, I have connected MCLR - Pin No. 1 to VDD through a voltage divider.

Is there anything else I have forgotten to do ? Any help/suggestions would be greatly appreciated.

betwixt

Super Moderator
Staff member
MCLR should not go to a voltage divider, connect it only to VCC through a resistor.

You may also have LVP mode enabled (Low Voltage Programming). It is enabled by default on these devices but you can turn it off in the configuration word. You probably have the PGM pin pulled to ground by the PICKIT2 but with it disconnected it will float high. Connect it to ground through a resistor of say 4700Ω so it stays low but can still be safely driven high if you need to reprogram the chip.

Brian.

pasicr

I think you have hardware problem...
regards

Somphorn

Member level 1
Your code indicate 1MHz clock. But when you use 8MHz in your circuit, wouldn't that make the blinking so rapid that it's indistinguishable?

betwixt

Super Moderator
Staff member
Even at 8MHz it would only flash four times a second so that isn't the problem. I'm pretty sure it's the PGM pin being high. When LVP mode (the default) is enabled the pin becomes the programming enable input and if it floats high, the program execution is suspended and it enters programming mode. The PICKIT normally pulls it high to program the chip then pulls it low afterward. My guess is that removing the PICKIT is let it float and hence stop running.

Brian.

amith.srivatsa

Junior Member level 1
Hello,

Just now I have modified the following: (But still LED doesn't blink)
Code:
#include <16F876A.H>
#fuses HS,NOWDT, NOPROTECT, NOBROWNOUT, PUT,NOCPD,NOPUT,NOLVP
#use delay(clock=8000000)

//====================================
void main(){

while(1)
{
output_high(PIN_C5);
delay_ms(1000);
output_low(PIN_C5);
delay_ms(1000);
}
}

Here is the schematic:

As suggested, I also connected MCLR to VDD using only a resistor, Have changed the configuration word so that LVP is disabled. Also, I have connected the PGM pin to ground through a resistor. Still doesn't work.

Should I also do something with the PGD, PGC pins ??? Am I still missing something ?

I am using a 12V battery, & so I have a voltage regulator giving me the required 5V. I don't see any problems in this but still thought of mentioning.

Any help would be great for me...

Just realized that the schematic in my previous msg has a resistor missing between PGM & Ground. I have this connected on my board though...

Last edited by a moderator:

Somphorn

Member level 1
If /MCLR for this chip is equivalent to RST for AT89S51 then I suggest putting a capacitor (100uF) between /MCLR and ground. Maintain R 1K as it is. The idea is to give time for the oscillator to stabilize while /MCLR is pulled low.

I got the same problem once, using AT89S51. I'm not sure whether the capacitor will help in your situation but it's worth a try.

amith.srivatsa

Junior Member level 1
Hello,

I am guessing I have narrowed down the problem to just something to do with power supply. I have the circuit on my board & when I just connect it to VDD & VSS in the PIC KIT 2, everything works, BUT; why is it not working with my battery ???

Somphorn

Member level 1
Are you sure the voltage regulator pins connected correctly? Measured it?

amith.srivatsa

Junior Member level 1
Yes, I have 4.6 volts across the output of the voltage regulator. Also, I tried to power up using a 5V battery, still no luck. But when connected to VDD & VSS of PIC KIT 2, still works...

Somphorn

Member level 1
What is the voltage across VDD/VSS of PIC KIT 2? What is the minimum voltage for VDD of the microcontroller?

amith.srivatsa

Junior Member level 1
Across VDD & VSS, I have given 4.5 V. It works for lower voltages too.

The minimum voltage would be 2.5V - LED blinking at this voltage when powered across VDD & VSS of PIC KIT 2.

But for programming i.e. to download the *.hex file onto the chip, we need a minimum of 4.5V.

Somphorn

Member level 1
If you're convinced it is the power supply problem, and as you mentioned the voltage regulator is working fine then I really don't know how else to figure this out.

Apart from the schematic I think it would help if you could show the actual PCB/stripboard layout. Sometimes in an attempt to simplify the copper routing we end up shorting or put the components in wrong order.

betwixt

Super Moderator
Staff member
If your schematic is correct, you are missing decoupling capacitors.

Try connecting two capacitors 1 x 10uF and 1x 100nF in parallel, directly across the VSS & VDD pins and as close as possible to the chip. Without them the clock may not be stable and there is a risk of noise on the supply pins causing a reset or crash in your program.

Brian.

Somphorn

Member level 1
Perhaps 5V supply from your PC's PSU (red & black) is stable enough.

amith.srivatsa

Junior Member level 1
Hello guys,
I am really stumped with this problem. Have been stuck with it for quite sometime now. I also have the capacitors across VDD & VSS, but still, doesnt work. Although the LED blinks promptly when powered using PIC KIT 2.

kroko

Newbie level 2
Just change resistor R2 to 470 or 330 ohm. It should work.

cheers

Also remove ground from RB3. Sorry I noticed this after sending message.

emergenbernz

Newbie level 3
Try to use another PIC16F876 if kroko's suggestion won't work.

eanema

Member level 2
ok, I could be way off on this, but I just recently fixed a very similar problem I was having when trying to program a pic18f4550 with an ICD2 clone. It turns out that the problem I was having was that I was programing the pic in debug mode, meaning I was able to step through the code, run it halt it etc. If you want to run your code on your chip with out the programmer/debugger attached, you need to program the PIC with the debugger set up as a programmer. To do this select your pickit2 from the programmer menu instead of from the debugger menu.

Hope this helps, know I had to search for it

Status
Not open for further replies.