5

I was setting up a PIC16F628A for a simple Hello World program (blinking LED) and somewhere in the process, I have failed. The LED stays lit at the specified pin and does not "blink". I am using the Internal Oscillator (which resonates at 4 MHz). This is my code (Using the XC8 Compiler):

#include <xc.h>

__CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_ON & LVP_ON & CPD_OFF & CP_OFF);

#define _XTAL_FREQ 4000000

int main() {
    TRISB0 = 0;
    while(0) {
        RB0 = 1;
        __delay_ms(1000);
        RB0 = 0;
        __delay_ms(1000);
    }
    return (0);
}

And my schematic:

I have read that this might be a MCLR problem but I have configured the fuses to set MCLR as an input pin as shown in the code.

JYelton
  • 32,302
  • 33
  • 134
  • 249
Seif Shawkat
  • 301
  • 1
  • 6
  • 13

1 Answers1

7

You have a couple of issues with your code and circuit.

  1. A while loop with a false condition will never loop. Do you mean to put "while(0)" or is that a typo? Either way, that loop is never looping. Change the 0 to a 1.

  2. The PIC16F628A has a PGM pin that it uses in LVP (Low Voltage Programming) mode. It shares the same pin as RB4. The PGM pin must be tied to ground under normal operation and pulled high during programming (your programmer should pull it high during a programming sequence). If the PGM pin is left floating, the chip will randomly reset for no explicable reason. Or just not run at all.

  3. Microcontroller based systems are typically single-purpose hardware. There is no operating system, so the code you program into the MCU has no higher-level entity to return to. For that reason, the main() function should be void and there should be no return statement at the end. Defining main() to return an int will probably not break anything, but it's considered poor coding practice.

Dan Laks
  • 8,504
  • 4
  • 27
  • 43
  • 1
    1. I thought about that too when I was viewing an example somewhere but I thought I'd just copy it to be safe. 2. This is what did it for me! I changed the config to be `LPV_OFF` instead of `LPV_ON`. – Seif Shawkat Oct 06 '14 at 20:43
  • 3
    I would be very wary of where you got that example from in the future. – Dan Laks Oct 06 '14 at 20:55
  • 1
    I've spent two days trying to understand why my MCU does not operate normally (I tried switching off any interruptions, comparators etc.), the reason was that PGM wasn't grounded in LVP! Thanks @DanLaks, you probably saved me few more days :) – pandomic Sep 19 '19 at 10:43