Author Topic: PIC Assembly Program Trouble  (Read 4418 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrisB4Z

  • Newbie
  • Posts: 2
PIC Assembly Program Trouble
« on: February 19, 2011, 06:07:00 am »
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, Chris


   list      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 DEFINITIONS
TEMP_VAR    UDATA
temp        RES     1             ;example variable definition
DELAY1      equ      0x11
DELAY2      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    0x000
   movwf   OSCCAL            ; update register with factory cal value
   GOTO   start

Init
   movlw   b'11111111'
   movwf   DELAY1
   movlw   b'11111111'
   movwf   DELAY2
   retlw   0

Delayloop1
   decfsz   DELAY1,1
   goto   Delayloop1    
   decfsz   DELAY2,1
   goto   Delayloop1
   retlw   0

start
   Call   Init   
   movlw   b'00001000'
   TRIS   GPIO
   movlw   b'11111111'
   movwf   GPIO
   Call   Delayloop1
   clrf   GPIO
   Call   Delayloop1
   END                       ; directive 'end of program'
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: PIC Assembly Program Trouble
« Reply #1 on: February 19, 2011, 07:04:40 am »
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 this

void main
{
    // init variables
    i = 0;
    k = 0;

    // function to setup timers and various startup stuff
    SetupStuff();  

    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 this

void main
{
    // init variables
    i = 0;
    k = 0;

    // function to setup timers and various startup stuff
    SetupStuff();  

    // 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
« Last Edit: February 19, 2011, 07:28:31 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline ziq8tsi

  • Regular Contributor
  • *
  • Posts: 79
Re: PIC Assembly Program Trouble
« Reply #2 on: February 19, 2011, 07:23:05 am »
   END                       ; directive 'end of program'

This 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: [Select]
forever:
        GOTO forever

        END
 

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 13306
  • Country: gb
  • 0999
Re: PIC Assembly Program Trouble
« Reply #3 on: February 19, 2011, 03:11:21 pm »
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
« Last Edit: February 19, 2011, 03:19:48 pm by Hero999 »
 

Offline Zad

  • Super Contributor
  • ***
  • Posts: 1013
  • Country: gb
    • Digital Wizardry, Analogue Alchemy, Software Sorcery
Re: PIC Assembly Program Trouble
« Reply #4 on: February 19, 2011, 04:36:38 pm »
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.

Offline ChrisB4Z

  • Newbie
  • Posts: 2
Re: PIC Assembly Program Trouble
« Reply #5 on: February 19, 2011, 10:17:24 pm »
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
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: PIC Assembly Program Trouble
« Reply #6 on: February 20, 2011, 12:20:22 am »
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.
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf