0
\$\begingroup\$

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 : enter image description here

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ has CMCON register any thing to do with that????? \$\endgroup\$ – mikmik Jul 15 '14 at 15:29
4
\$\begingroup\$

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.

\$\endgroup\$
15
  • \$\begingroup\$ im making A1 output not A0 where do you see A0, and why RB0 can turn on the led \$\endgroup\$ – mikmik Jul 15 '14 at 15:14
  • \$\begingroup\$ LED is defined as RA1 (which is pin 18 as per your circuit diagram) but "Make RA0 pin output" is in your code comments... \$\endgroup\$ – carveone Jul 15 '14 at 15:16
  • \$\begingroup\$ no matter what put in comment, but u see that I put RA1 output, \$\endgroup\$ – mikmik Jul 15 '14 at 15:19
  • 1
    \$\begingroup\$ TRISA0 = 0; sets pin A0 to be an output! \$\endgroup\$ – Majenko Jul 15 '14 at 15:23
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Majenko Jul 15 '14 at 15:33
1
\$\begingroup\$
  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.

\$\endgroup\$
4
  • \$\begingroup\$ 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?? \$\endgroup\$ – mikmik Jul 15 '14 at 17:43
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Dan Laks Jul 16 '14 at 6:00
  • \$\begingroup\$ 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?? \$\endgroup\$ – mikmik Jul 16 '14 at 11:36
  • \$\begingroup\$ 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. \$\endgroup\$ – Dan Laks Jul 16 '14 at 15:55
0
\$\begingroup\$

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.

\$\endgroup\$
0

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.