# Simple blink LED with PIC12F629

I am new to microchip but I build an circuit with PIC12F629 and wrote this program:

#include <xc.h>
#define _XTAL_FREQ 20000000
#pragma config WDTE=OFF , BOREN=OFF , PWRTE=ON , MCLRE=OFF , FOSC=INTRCIO

void main(){

TRISIO    = 0;
GPIO   = 0;
while(1){
__delay_ms(1000);
GP0 = 1;
__delay_ms(1000);
GP0 = 0;
}

}


I compile it with XC8 MPLAB X and I have transfer hex file to chip, then I connect pin num1 to positive voltage and pin number 8 to ground, then pin num7(GP0) to positive side of led and grounded other side of LED.

The circuit doesn't work, how could I fix it? I don't quite get timing in the code, any idea?

• I measure voltage at LED sides, it is about 1 but incoming voltage is about 5.

I also removed all timing to lite GPO all time but still no luck:

#include <xc.h>
//#define _XTAL_FREQ 20000000
#pragma config WDTE=OFF , BOREN=OFF , PWRTE=ON , MCLRE=OFF , FOSC=INTRCIO

void main(){

//init GPIO pins
TRISIO    = 0;
GPIO   = 0;
while(1){
// __delay_ms(1000);
GP0 = 1;
//__delay_ms(1000);
//GP0 = 0;
}

}


Update

It strangely worked by adding positive voltage first and negative second in to the circuit, any idea why? It even works by just grounding pin3! I think it needs something in the circuit.

• Sounding like hardware. Do you have a bypass capacitor across the supply? Something like 100nF (0.1uF). Grounding pin 3? Is it possible the Vss pin (pin 8) is not making a solid connection to ground? Mar 9 '14 at 13:19
• The connection to the ground(pin 8) is solid sir and no i did not bypass a capacitor across the supply(i used it but it didn't work). Mar 9 '14 at 13:37
• Did you add a series resistor to the LED? Mar 9 '14 at 13:56
• I tried to get out put in GP1 by (GP1 = 1;) but the voltage drop to 2 volt !! do you know why ? Mar 9 '14 at 13:56
• Is your breadboard perhaps haunted? Maybe a photo would help. Is the incoming power solid regulated 5.0Vdc? Mar 9 '14 at 14:06

You haven't connected a resistor in series with the led, that can exceed the max allowed I/O current and damage the MCU

The connection you should use is like

simulate this circuit – Schematic created using CircuitLab

The resistor value is calculated using the following equation

$$R= \frac {V_{in}-ledV_F}{I_{LED}}$$

The actual value depends on the current you want through the LED, the forward voltage drop of the led (Vf) and the I/O voltage.

As an example, for Iled=10mA, VF= 2v and 5v output from I/O pins

$$R= \frac {5v-2v}{{0.010A}} =300 Ohm$$

So consider using a resistor value of 270 - 330 Ohm

• thank's ,I added the resistor,i think the circuit work fine with one LED but when i put second one to GP1 it don't work, do you know why ? Mar 9 '14 at 17:51
• @user38322 Have you added resistors in both pins?Can you ply with a different I/O pin and see if it works? Maybe the mcu has been damaged by connecting directly the leds (?) Mar 9 '14 at 17:54
• Yes,I added two resistor,and i test with other pin which i have never used but nothing happens,I have searched , i think MPLAB X,XC8 is not a good IDE for PIC programming,is it? and i find out i should change two pin in a same time but i dont know how. Mar 9 '14 at 18:02
• There's nothing wrong with MPLAB X and Microchip XC8 for PIC programming. If you want to change two GPIO values at a time, you have to write to the entire port instead of to individual bits. Mar 9 '14 at 18:47

try #define _XTAL_FREQ 2000000 or 4000000

can have a zero more #define _XTAL_FREQ 20000000

• Why do you think this is the answer to this (almost 7 year old) question? Mar 6 at 22:13
• hi bruno, first of all, you probably should add some more detail, otherwise it doesn't necessarily give much explanatory power, secondly the question is almost 7 years old, probably the person who asked about it already forgot about ever having this problem Mar 6 at 22:28
• Bruno Trócolis - Hi :-) As already commented, please add more detail about what you mean (the OP already tried what seems to be your 2nd suggestion) & you don't explain why your 1st suggestion might be the solution. How do we know what frequency xtal the OP was using? So why are you making those specific suggestions? Also note that the OP already solved their problem, without changing that #define because they said in a later edit that: "It strangely worked by adding positive voltage first and negative second in to the circuit". Mar 7 at 17:40