# pic16f628a successfully programmed but not working

I want to blink led with pic16f628a using pickit2 I generated the hex file and successfully write it, according to code, RB are input, but when I put led between RB0,1,2,3.. and ground it turn on. all the pin turn on the led, I'm using 5 volt output

code :

#include<htc.h>

// Config word
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);

// Define LED pin
#define LED  RA1

#define _XTAL_FREQ   8000000

void main()
{
TRISA1 = 0;                // Make RA1 pin output
TRISB = 1;
LED    = 0;                // Make RA1 low

while(1)
{
__delay_ms(500);       // Half sec delay
LED = 0;               // LED off
__delay_ms(500);       // Half sec delay
LED = 1;               // LED on
}
}


scheme :

• has CMCON register any thing to do with that????? – mikmik Jul 15 '14 at 15:29

You're setting A0 to be an output, then you're blinking A1. Which is it? A0 or A1? Make your mind up, and then make them both the same.

Another thing I notice on your schematic. You're setting the oscillator to be HS, yet I'm not seeing ant crystal or loading caps on your schematic. Did you just omit them from that drawing, or do they truly not exist?

The same goes for the (missing) obligatory decoupling capacitor on the power pin.

• im making A1 output not A0 where do you see A0, and why RB0 can turn on the led – mikmik Jul 15 '14 at 15:14
• LED is defined as RA1 (which is pin 18 as per your circuit diagram) but "Make RA0 pin output" is in your code comments... – carveone Jul 15 '14 at 15:16
• no matter what put in comment, but u see that I put RA1 output, – mikmik Jul 15 '14 at 15:19
• TRISA0 = 0; sets pin A0 to be an output! – Majenko Jul 15 '14 at 15:23
• It does, but you have to tell it to use it. You're telling it to use an external crystal, so although it programs, it doesn't actually do anything. Add a crystal or set it to use the internal RC oscillator. – Majenko Jul 15 '14 at 15:33
1. As Majenko pointed out, you set up the configuration bits to use a high-speed crystal (FOSC_HS), yet you did not add a crystal to your schematic. Note that "#define _XTAL_FREQ" does not actually define the oscillator speed of the microcontroller. It is simply a constant value the delay functions use to calculate delay times. You must either add a crystal or just use the internal oscillator (much simpler). If you don't know how to do that, just ask.

2. You must put a pull-up resistor to VDD on the MCLR pin. A 10k resistor is typical. If you leave MCLR floating, the chip will either never run or it will randomly reset. You can avoid the pull-up resistor if you clear the MCLRE bit in the configuration settings, but I wouldn't necessarily recommend that right now.

• yes I searched all the web to find how to set internal oscillator for this pic16f628a in this program, I found nothing helpful, any help?? – mikmik Jul 15 '14 at 17:43
• Best place to find these answers is always in the datasheet. The datasheet might seem a little daunting at first, but I promise it's very useful once you read it. Anyway, the oscillator for this particular PIC is determined by the configuration bits. Right now you have "FOSC_HS" in your code. HS stands for high-speed crystal (or resonator). You compiler will have another macro defined for the internal oscillator. Probably something like "INTOSC" or similar. – Dan Laks Jul 16 '14 at 6:00
• i configured the internal oscillator electronics.stackexchange.com/questions/121441/… , and put resistor between VDD and MCLR as u said, but still nothing work, I successfully programmed the chip with pickit2 and then I just set like the scheme above, the led is not on?? – mikmik Jul 16 '14 at 11:36
• Looks like this chip has a PGM pin, which is used for low-voltage programming (LVP). I believe LVP is enabled by default in the CONFIG register, so you have to ground the PGM pin to prevent the chip from holding in programming mode. Connect the PGM pin to ground and see if that helps. – Dan Laks Jul 16 '14 at 15:55

If this is one of your first projects with 16f628a always turn off comparator(CMCON =0x07) and/or use PortB first. Also attach few leds on different pins and change values on whole port like PortB =0x00 and PortB=0xFF that way even if there is some conflict you didnt know about some pin will flash some led. That way you can learn on what you should pay attention in the future. You dont need to be worried about MCLR just use configuration word that turns it off and you will be fine. I never used any resistor on that pin ever and never had problems.

I just noticed in your picture of the pickit2 programmer that it wrote successfully, but I think something is wrong with your hex code — it seems to have data only in the first location. Make sure you check that as well.