Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MPLAB X adding two RETLW 0x00 lines at the start.

Status
Not open for further replies.

Diver300

Well-Known Member
Most Helpful Member
I've just started using a PIC12F1571, which is only supported with MPLAB X. I'm programming in assembly, and the only file that is included is the p12F1571 file that Microchip provide.

For some reason, MPLAB X adds two RETLW 0x00 lines at the start, which prevents the program from running due to stack underflow.

How do I stop that happening?
 
Your using the generated asm file or a blank. Mine didn't add nothing i tried a blink. I'm using 4.05 of mplab
 
I'm using MPLAB X IDE v3.55

I started with a blank asm file. Here is what is contains:-
Code:
#include "p12f1571.inc"

; CONFIG1
; __config 0x3FFC
 __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF
; CONFIG2
; __config 0x3EFF
 __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_ON



#pragma code

#define count1 0x30
#define count2 0x31


    nop
    movlb    0x01
    bsf    TRISA, 4
    movlb    0x00
   
   
mainloop
    bsf    PORTA, 4
   
loop1
    decfsz    count1, f
    goto    loop1
    decfsz    count2, f
    goto    loop1
   
    bcf    PORTA, 4
   
loop2
    decfsz    count1, f
    goto    loop2 
    decfsz    count2, f
    goto    loop2
   
    goto    mainloop
   
   
    end

Here is what the output of the assembler is:-
Code:
      Line      Address       Opcode      Label            DisAssy         
       1     000           3400                    RETLW 0x0               
       2     001           3400                    RETLW 0x0               
       3     002           0000                    NOP                     
       4     003           0021                    MOVLB 0x1               
       5     004           160C                    BSF TRISA, 0x4           
       6     005           0020                    MOVLB 0x0               
       7     006           160C                    BSF PORTA, 0x4           
       8     007           0BB0                    DECFSZ 0x30, F           
       9     008           2807                    GOTO 0x7                 
      10     009           0BB1                    DECFSZ 0x31, F           
      11     00A           2807                    GOTO 0x7                 
      12     00B           120C                    BCF PORTA, 0x4           
      13     00C           0BB0                    DECFSZ 0x30, F           
      14     00D           280C                    GOTO 0xC                 
      15     00E           0BB1                    DECFSZ 0x31, F           
      16     00F           280C                    GOTO 0xC                 
      17     010           2806                    GOTO 0x6
 
I have not seen the statement "#pragma code" in assembler. (I think I have seen it in a "C" program but I dont understand "C") What does this do ? Also you have no org statment to tell the assembler which address to start the code at. I have just had a look at MPASN v5.55 (Which I use at the moment.) and the PIC12F1571 is supported in that.

Les.
 
This is what I got
Code:
Disassembly Listing for test
Generated From:
/home/burt/code/asm/dist/default/production/asm.production.cof
Nov 28, 2017 5:15:49 AM

---  /home/burt/code/asm/newpic_8b_general.asm  ---------------------------------------------------------
                                                  1:     #include "p12f1571.inc"
                                                  2:    
                                                  3:     ; CONFIG1
                                                  4:     ; __config 0x3FFC
                                                  5:      __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF
                                                  6:     ; CONFIG2
                                                  7:     ; __config 0x3EFF
                                                  8:      __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_ON
                                                  9:    
                                                  10:  
                                                  11:  
                                                  12:    #pragma code
                                                  13:  
                                                  14:  
                                                  15:    #define count1 0x30
                                                  16:    #define count2 0x31
                                                  17:  
                                                  18:  
0002  0000     NOP                                19:        nop
0003  0021     MOVLB 0x1                          20:        movlb    0x01
0004  160C     BSF TRISA, 0x4                     21:        bsf    TRISA, 4
0005  0020     MOVLB 0x0                          22:        movlb    0x00
                                                  23:      
                                                  24:      
                                                  25:    mainloop
0006  160C     BSF PORTA, 0x4                     26:        bsf    PORTA, 4
                                                  27:      
                                                  28:    loop1
0007  0BB0     DECFSZ 0x30, F                     29:        decfsz    count1, f
0008  2807     GOTO 0x7                           30:        goto    loop1
0009  0BB1     DECFSZ 0x31, F                     31:        decfsz    count2, f
000A  2807     GOTO 0x7                           32:        goto    loop1
                                                  33:      
000B  120C     BCF PORTA, 0x4                     34:        bcf    PORTA, 4
                                                  35:      
                                                  36:    loop2
000C  0BB0     DECFSZ 0x30, F                     37:        decfsz    count1, f
000D  280C     GOTO 0xC                           38:        goto    loop2
000E  0BB1     DECFSZ 0x31, F                     39:        decfsz    count2, f
000F  280C     GOTO 0xC                           40:        goto    loop2
                                                  41:      
0010  2806     GOTO 0x6                           42:        goto    mainloop
                                                  43:      
                                                  44:      
8007  0000     NOP                                45:        end
 
What happens if you use a more conventional beginning based on the old templates, e.g.,

Code:
    ORG     0x0000            ;processor reset vector
    PAGESEL START
    BRA     START             ;According to Microchip, when using debug
                              ;header, first inst. may be passed over by ICD2.

;******************************************************************************
;                        INTERRUPT SERVCE ROUTINE
;******************************************************************************
     ORG       0x0004  
     nop
     retfie

;******************************************************************************
START
 
I use this to start with in Mplab x

Code:
;*******************************************************************************
;                                                                              *
;    Microchip licenses this software to you solely for use with Microchip     *
;    products. The software is owned by Microchip and/or its licensors, and is *
;    protected under applicable copyright laws.  All rights reserved.          *
;                                                                              *
;    This software and any accompanying information is for suggestion only.    *
;    It shall not be deemed to modify Microchip?s standard warranty for its    *
;    products.  It is your responsibility to ensure that this software meets   *
;    your requirements.                                                        *
;                                                                              *
;    SOFTWARE IS PROVIDED "AS IS".  MICROCHIP AND ITS LICENSORS EXPRESSLY      *
;    DISCLAIM ANY WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING  *
;    BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS    *
;    FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL          *
;    MICROCHIP OR ITS LICENSORS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,         *
;    INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO     *
;    YOUR EQUIPMENT, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR    *
;    SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY   *
;    DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER      *
;    SIMILAR COSTS.                                                            *
;                                                                              *
;    To the fullest extend allowed by law, Microchip and its licensors         *
;    liability shall not exceed the amount of fee, if any, that you have paid  *
;    directly to Microchip to use this software.                               *
;                                                                              *
;    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF    *
;    THESE TERMS.                                                              *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Filename:                                                                 *
;    Date:                                                                     *
;    File Version:                                                             *
;    Author:                                                                   *
;    Company:                                                                  *
;    Description:                                                              *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Notes: In the MPLAB X Help, refer to the MPASM Assembler documentation    *
;    for information on assembly instructions.                                 *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Known Issues: This template is designed for relocatable code.  As such,   *
;    build errors such as "Directive only allowed when generating an object    *
;    file" will result when the 'Build in Absolute Mode' checkbox is selected  *
;    in the project properties.  Designing code in absolute mode is            *
;    antiquated - use relocatable mode.                                        *
;                                                                              *
;*******************************************************************************
;                                                                              *
;    Revision History:                                                         *
;                                                                              *
;*******************************************************************************



;*******************************************************************************
; Processor Inclusion
;
; TODO Step #1 Open the task list under Window > Tasks.  Include your
; device .inc file - e.g. #include <device_name>.inc.  Available
; include files are in C:\Program Files\Microchip\MPLABX\mpasmx
; assuming the default installation path for MPLAB X.  You may manually find
; the appropriate include file for your device here and include it, or
; simply copy the include generated by the configuration bits
; generator (see Step #2).
;
;*******************************************************************************

; TODO INSERT INCLUDE CODE HERE

;*******************************************************************************
;
; TODO Step #2 - Configuration Word Setup
;
; The 'CONFIG' directive is used to embed the configuration word within the
; .asm file. MPLAB X requires users to embed their configuration words
; into source code.  See the device datasheet for additional information
; on configuration word settings.  Device configuration bits descriptions
; are in C:\Program Files\Microchip\MPLABX\mpasmx\P<device_name>.inc
; (may change depending on your MPLAB X installation directory).
;
; MPLAB X has a feature which generates configuration bits source code.  Go to
; Window > PIC Memory Views > Configuration Bits.  Configure each field as
; needed and select 'Generate Source Code to Output'.  The resulting code which
; appears in the 'Output Window' > 'Config Bits Source' tab may be copied
; below.
;
;*******************************************************************************

; TODO INSERT CONFIG HERE

;*******************************************************************************
;
; TODO Step #3 - Variable Definitions
;
; Refer to datasheet for available data memory (RAM) organization assuming
; relocatible code organization (which is an option in project
; properties > mpasm (Global Options)).  Absolute mode generally should
; be used sparingly.
;
; Example of using GPR Uninitialized Data
;
;   GPR_VAR        UDATA
;   MYVAR1         RES        1      ; User variable linker places
;   MYVAR2         RES        1      ; User variable linker places
;   MYVAR3         RES        1      ; User variable linker places
;
;   ; Example of using Access Uninitialized Data Section (when available)
;   ; The variables for the context saving in the device datasheet may need
;   ; memory reserved here.
;   INT_VAR        UDATA_ACS
;   W_TEMP         RES        1      ; w register for context saving (ACCESS)
;   STATUS_TEMP    RES        1      ; status used for context saving
;   BSR_TEMP       RES        1      ; bank select used for ISR context saving
;
;*******************************************************************************

; TODO PLACE VARIABLE DEFINITIONS GO HERE

;*******************************************************************************
; Reset Vector
;*******************************************************************************

RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

;*******************************************************************************
; TODO Step #4 - Interrupt Service Routines
;
; There are a few different ways to structure interrupt routines in the 8
; bit device families.  On PIC18's the high priority and low priority
; interrupts are located at 0x0008 and 0x0018, respectively.  On PIC16's and
; lower the interrupt is at 0x0004.  Between device families there is subtle
; variation in the both the hardware supporting the ISR (for restoring
; interrupt context) as well as the software used to restore the context
; (without corrupting the STATUS bits).
;
; General formats are shown below in relocatible format.
;
;------------------------------PIC16's and below--------------------------------
;
; ISR       CODE    0x0004           ; interrupt vector location
;
;     <Search the device datasheet for 'context' and copy interrupt
;     context saving code here.  Older devices need context saving code,
;     but newer devices like the 16F#### don't need context saving code.>
;
;     RETFIE
;
;----------------------------------PIC18's--------------------------------------
;
; ISRHV     CODE    0x0008
;     GOTO    HIGH_ISR
; ISRLV     CODE    0x0018
;     GOTO    LOW_ISR
;
; ISRH      CODE                     ; let linker place high ISR routine
; HIGH_ISR
;     <Insert High Priority ISR Here - no SW context saving>
;     RETFIE  FAST
;
; ISRL      CODE                     ; let linker place low ISR routine
; LOW_ISR
;       <Search the device datasheet for 'context' and copy interrupt
;       context saving code here>
;     RETFIE
;
;*******************************************************************************

; TODO INSERT ISR HERE

;*******************************************************************************
; MAIN PROGRAM
;*******************************************************************************

MAIN_PROG CODE                      ; let linker place main program

START

    ; TODO Step #5 - Insert Your Program Here

    MOVLW 0x55                      ; your instructions
    GOTO $                          ; loop forever

    END
[code]
 
You do know that you can start with the simple asm file and set you configure and it add's org for you.
You get a file like this to start......
Code:
; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
#include "p12f1571.inc"

; CONFIG1
; __config 0xFFFF
 __CONFIG _CONFIG1, _FOSC_ECH & _WDTE_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF
; CONFIG2
; __config 0xFFFF
 __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_ON
RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

MAIN_PROG CODE                      ; let linker place main program

START

    GOTO $                          ; loop forever

    END
Then edit with your code.
the new 4.05 mplab x is way better the 3.xx there been lots of bug fixes.
 
Diver300

Glad it worked for you. Just FYI, I was playing with relocatable code and MPLab 8.92 (I always write absolute). There was no problem if I included an operand for the #pragma directive:

upload_2017-11-28_5-58-40.png


John
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top