PIC Assembly Program Trouble - Page 1
Forum section: electronics / beginners

author: ChrisB4Z
author_rank: Newbie
timestamp: February 19, 2011, 06:07:00 am
content: Hi,I am atempting to learn PIC microcontrollers and assembly for a project. I am using a PIC 12F510, with a PICKIT2 and MPLAB v7.40. I am having problems with the simple program below. The program is to simply turn a LED on for some time then off for some time and stop. However what actually happens is that the program appears to loop and the LED blinks. My question is why does my program loop and not stop at END? I have included my program below.Thanks, Chrislist p=12F510 ; list directive to define processor#include <p12F510.inc> ; processor specific variable definitions__CONFIG _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC & _IOSCFS_ON; '__CONFIG' directive is used to embed configuration word within .asm file.; The lables following the directive are located in the respective .inc file.; See respective data sheet for additional information on configuration word.;***** VARIABLE DEFINITIONSTEMP_VAR UDATAtemp RES 1 ;example variable definitionDELAY1 equ 0x11DELAY2 equ 0x12;**********************************************************************RESET_VECTOR CODE 0x3FF ; processor reset vector; Internal RC calibration value is placed at location 0x3FF by Microchip; as a movlw k, where the k is a literal value.MAIN CODE 0x000movwf OSCCAL ; update register with factory cal valueGOTO startInitmovlw b'11111111'movwf DELAY1movlw b'11111111'movwf DELAY2retlw 0Delayloop1decfsz DELAY1,1goto Delayloop1decfsz DELAY2,1goto Delayloop1retlw 0startCall Initmovlw b'00001000'TRIS GPIOmovlw b'11111111'movwf GPIOCall Delayloop1clrf GPIOCall Delayloop1END ; directive 'end of program'

author: Psi
author_rank: Super Contributor
timestamp: February 19, 2011, 07:04:40 am
content: It's normal in the world of micros for the program to automatically start again once its finished, eg It just runs forever.Most of the time you don't want it to restart itself though, you might put your main code inside a never ending loop so you can put start-up code first before the loop starts.eg, in C it might look like thisvoid main{// init variablesi = 0;k = 0;// function to setup timers and various startup stuffSetupStuff();while(1) // a loop that never ends{// do main code forever}}Or if you really want the micro to do something when its powered on and then do nothing forever after you might code like thisvoid main{// init variablesi = 0;k = 0;// function to setup timers and various startup stuffSetupStuff();// do main code once* code *while(1) // a loop that never ends{}}You could put the micro into sleep mode inside that while loop so it consumes less power if you wanted.Sorry that my examples are in C but i dont know any PIC asm

author: ziq8tsi
author_rank: Regular Contributor
timestamp: February 19, 2011, 07:23:05 am
content: Quote from: ChrisB4Z on February 19, 2011, 06:07:00 amThis directive generates no code; it just tells the assembler that the input file is complete.There is nothing to tell the MCU that the program is finished, so it carries on executing whatever junk instructions are in the rest of the program memory, until it eventually loops back to address zero and starts the program again.You want to finish with something like:Code:forever:GOTO foreverEND

author: Zero999
author_rank: Super Contributor
timestamp: February 19, 2011, 03:11:21 pm
content: Yes, you need to loop round otherwise the MCU will just freeze or worse, behave unpredictably.EDIT:Here's a site with some good PIC tutorials on:http://www.gooligum.com.au/tut_baseline.html

author: Zad
author_rank: Super Contributor
timestamp: February 19, 2011, 04:36:38 pm
content: As ziq8tsi said, it will just execute whatever is in memory (probably the equivelent of a string of NOOP instructions) until it hits the end of memory, whereupon it loops round to 0000 again.

author: ChrisB4Z
author_rank: Newbie
timestamp: February 19, 2011, 10:17:24 pm
content: Hi Everyone, I implemented a loop at the end like ziq8tsi suggested and it is now working like I wanted. Thanks for the help, I'm sure I'll have more questions later.Chris

author: Psi
author_rank: Super Contributor
timestamp: February 20, 2011, 12:20:22 am
content: Interesting, i just assumed because it performs a chip erase before reprogramming with your hex binary that the contents after the program ends would all be blank, 0xFF or whatever the nand erased state is.But yeah, forgot that there is also program data stored in flash and the micro would try to execute that as it wraps around, hence the unpredictable behaviour.