Electronic – Simple blink LED with PIC12F629

microcontrollermplabxpic

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.

Best Answer

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

schematic

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