C program for the PIC18F1220

JohnInTX

Joined Jun 26, 2012
4,787
Add:
while(1);
Just before the last bracket in the program (at line 51). The way it is written, after the 4 flashes, your code exits ‘main’ and will restart (XC8 adds that code). Embedded programs like this always need some sort of loop to stay in main. Without it, the code ‘exits’ but there is no operating system to return to so the C startup code provides a jump back to the beginning. Note that this just hangs your code there but it will stop the flashing.

You should be using LATBbits, not PORTBbits for outputs - especially single bit outputs.

Also try our handy code tags to post your code. I added them your post 19 so you can see how it's done. Use the ‘INSERT’ button on the edit toolbar.

Good luck!
 
Last edited:

dl324

Joined Mar 30, 2015
18,329
im using the pickit3 to flash the middle segment which is RB 4, and it seems to be not working (it does not stop at 4 flash
Since you need the program to loop forever, add a while (1) loop around the code.
Code:
i = 0;
while (1) {
  for (; i < 4; i++) {
    PORTBbits.RB4=1;//LED ON
     __delay_ms(500);
     PORTBbits.RB4=0;//LED OFF
     __delay_ms(500);
  }
}
Or using only while loops:
Code:
i = 0;
while (1) {
  while (i++ < 4) {
    PORTBbits.RB4=1;//LED ON
     __delay_ms(500);
     PORTBbits.RB4=0;//LED OFF
     __delay_ms(500);
  }
}
 

Thread Starter

omar1990

Joined Sep 7, 2018
12
Add:
while(1);
Just before the last bracket in the program (at line 51). The way it is written, after the 4 flashes, your code exits ‘main’ and will restart (XC8 adds that code). Embedded programs like this always need some sort of loop to stay in main. Without it, the code ‘exits’ but there is no operating system to return to so the C startup code provides a jump back to the beginning. Note that this just hangs your code there but it will stop the flashing.

You should be using LATBbits, not PORTBbits for outputs - especially single bit outputs.

Also try our handy code tags to post your code. I added them your post 19 so you can see how it's done. Use the ‘INSERT’ button on the edit toolbar.

Good luck!
Hey john,
i did exactly what you said and its working thanks for your help
 
Top