2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Spehro Pefhany Mar 9 '14 at 13:19
  • \$\begingroup\$ 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). \$\endgroup\$ – Shombol-shagol Mar 9 '14 at 13:37
  • \$\begingroup\$ Did you add a series resistor to the LED? \$\endgroup\$ – Spehro Pefhany Mar 9 '14 at 13:56
  • \$\begingroup\$ I tried to get out put in GP1 by (GP1 = 1;) but the voltage drop to 2 volt !! do you know why ? \$\endgroup\$ – Shombol-shagol Mar 9 '14 at 13:56
  • \$\begingroup\$ Is your breadboard perhaps haunted? Maybe a photo would help. Is the incoming power solid regulated 5.0Vdc? \$\endgroup\$ – Spehro Pefhany Mar 9 '14 at 14:06
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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 ? \$\endgroup\$ – Shombol-shagol Mar 9 '14 at 17:51
  • \$\begingroup\$ @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 (?) \$\endgroup\$ – alexan_e Mar 9 '14 at 17:54
  • \$\begingroup\$ 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. \$\endgroup\$ – Shombol-shagol Mar 9 '14 at 18:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Adam Lawrence Mar 9 '14 at 18:47

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.