0

I have a simple PIC16F18877 circuit setup on my breadboard and I've successfully gotten an LED to blink within an infinite while loop. I attempted to put the same code inside a for loop that should only execute 5 times, but the LED keeps blinking.

My Code (MPLAB with XC8 Compiler):

#include <xc.h>

#define _XTAL_FREQ 8000000

int main()
{
    TRISD1 = 0;

    for (int i = 0; i < 5; i++)
    {
        RD1 = 1;
        __delay_ms(500);
        RD1 = 0;
        __delay_ms(500);
    }

    return 0;
}

3 Answers 3

5

Where do you expect the CPU to jump on return from main? Or rather, what do you expect it to do when you don't tell it what to do? On a desktop computer, the program would normally return to the OS - On an embedded system, there is none.

Most probably, the return from main returns to the startup code and, eventually (either "by accident" or deliberately) to the reset vector, starting your program from the beginning.

If you want the MCU to "stop" actually, "do nothing" you need to force it into an infinite loop instead of return. This is, however, not a common approach on an MCU.

Sign up to request clarification or add additional context in comments.

4 Comments

Not probably, certainly: "After execution reaches the end of main(), code added by the compiler jumps back to the Reset vector. The device then executes the runtime startup code and the main() function again." - Source: ww1.microchip.com/downloads/en/DeviceDoc/50002173A.pdf.
@Unimportant what's really certain these days at all?
Death and taxes, like they always have.
I tried adding a while loop after the for loop and something strange occurred. It appeared to be executing the for and while loop simultaneously. The light would blink link normal and then blink really fast, almost like a stutter and then blink normally, etc.. but it never stopped blinking
1

I tried adding a while loop after the for loop and something strange occurred. It appeared to be executing the for and while loop simultaneously. The light would blink link normal and then blink really fast, almost like a stutter and then blink normally, etc.. but it never stopped blinking

Check the watchdog timer. If it's set, the mcu will just reset after a set amount of clock cycles and run the code again and again. You can use CLRWDT() to reset the watchdog timer or just turn the WDT off. I highly recommend to go through these steps in order to be sure that the mcu does as expected:

  1. Check the PIC configuration bits, are they setup properly? See the documentation in the microchip program folder /docs/chips
  2. Make sure the oscillator is setup correctly.
  3. Read the datasheet and make sure the ports are set correctly, especially the analogue ports using the analogue selection registers.

(my reputation isn't high enough to comment, sorry about that.)

Comments

0

you are not on an operating system here, can you show us the disassembly to show the call to main and what it returns to? Or what if you put an infinite loop before main ends (while(1) continue;) do you get 5 blinks then?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.