Assembly in MPLAB X

Discussion in 'Embedded Systems and Microcontrollers' started by Dawud Beale, Nov 23, 2013.

  1. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone. I have mainly been using MPLAB X for c. I am attempting to write some code in assembly and am a bit confused. I created a new source file with a general MPASM selected (as opposed to simple MPASM) and got all this up. How do I put my code in here, ready to upload to a PIC? This is what is contained within the template:

    Code ( (Unknown Language)):
    1.  
    2. ;    Microchip licenses this software to you solely for use with Microchip     *
    3. ;    products. The software is owned by Microchip and/or its licensors, and is *
    4. ;    protected under applicable copyright laws.  All rights reserved.          *
    5. ;                                                                              *
    6. ;    This software and any accompanying information is for suggestion only.    *
    7. ;    It shall not be deemed to modify Microchip?s standard warranty for its    *
    8. ;    products.  It is your responsibility to ensure that this software meets   *
    9. ;    your requirements.                                                        *
    10. ;                                                                              *
    11. ;    SOFTWARE IS PROVIDED "AS IS".  MICROCHIP AND ITS LICENSORS EXPRESSLY      *
    12. ;    DISCLAIM ANY WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING  *
    13. ;    BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS    *
    14. ;    FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL          *
    15. ;    MICROCHIP OR ITS LICENSORS BE LIABLE FOR ANY INCIDENTAL, SPECIAL,         *
    16. ;    INDIRECT OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO     *
    17. ;    YOUR EQUIPMENT, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR    *
    18. ;    SERVICES, ANY CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY   *
    19. ;    DEFENSE THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER      *
    20. ;    SIMILAR COSTS.                                                            *
    21. ;                                                                              *
    22. ;    To the fullest extend allowed by law, Microchip and its licensors         *
    23. ;    liability shall not exceed the amount of fee, if any, that you have paid  *
    24. ;    directly to Microchip to use this software.                               *
    25. ;
    26. ;    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF
    27. ;    THESE TERMS.
    28. ;                                                                              *
    29. ;                                                                              *
    30. ;    Filename:                                                                 *
    31. ;    Date:                                                                     *
    32. ;    File Version:                                                             *
    33. ;    Author:                                                                   *
    34. ;    Company:                                                                  *
    35. ;    Description:                                                              *
    36. ;                                                                              *
    37. ;*******************************************************************************
    38. ;                                                                              *
    39. ;    Notes: In the MPLAB X Help, refer to the MPASM Assembler documentation    *
    40. ;    for information on assembly instructions.                                 *
    41. ;                                                                              *
    42. ;*******************************************************************************
    43. ;                                                                              *
    44. ;    Known Issues: This template is designed for relocatable code.  As such,   *
    45. ;    build errors such as "Directive only allowed when generating an object    *
    46. ;    file" will result when the 'Build in Absolute Mode' checkbox is selected  *
    47. ;    in the project properties.  Designing code in absolute mode is            *
    48. ;    antiquated - use relocatable mode.                                        *
    49. ;                                                                              *
    50. ;*******************************************************************************
    51. ;                                                                              *
    52. ;    Revision History:                                                         *
    53. ;                                                                              *
    54. ;*******************************************************************************
    55.  
    56.  
    57.  
    58. ;*******************************************************************************
    59. ; Processor Inclusion
    60. ;
    61. ; TODO Step #1 Open the task list under Window > Tasks.  Include your
    62. ; device .inc file - e.g. #include <device_name>.inc.  Available
    63. ; include files are in C:\Program Files\Microchip\MPLABX\mpasmx
    64. ; assuming the default installation path for MPLAB X.  You may manually find
    65. ; the appropriate include file for your device here and include it, or
    66. ; simply copy the include generated by the configuration bits
    67. ; generator (see Step #2).
    68. ;
    69. ;*******************************************************************************
    70.  
    71. ; TODO INSERT INCLUDE CODE HERE
    72.  
    73. ;*******************************************************************************
    74. ;
    75. ; TODO Step #2 - Configuration Word Setup
    76. ;
    77. ; The 'CONFIG' directive is used to embed the configuration word within the
    78. ; .asm file. MPLAB X requires users to embed their configuration words
    79. ; into source code.  See the device datasheet for additional information
    80. ; on configuration word settings.  Device configuration bits descriptions
    81. ; are in C:\Program Files\Microchip\MPLABX\mpasmx\P<device_name>.inc
    82. ; (may change depending on your MPLAB X installation directory).
    83. ;
    84. ; MPLAB X has a feature which generates configuration bits source code.  Go to
    85. ; Window > PIC Memory Views > Configuration Bits.  Configure each field as
    86. ; needed and select 'Generate Source Code to Output'.  The resulting code which
    87. ; appears in the 'Output Window' > 'Config Bits Source' tab may be copied
    88. ; below.
    89. ;
    90. ;*******************************************************************************
    91.  
    92. ; TODO INSERT CONFIG HERE
    93.  
    94. ;*******************************************************************************
    95. ;
    96. ; TODO Step #3 - Variable Definitions
    97. ;
    98. ; Refer to datasheet for available data memory (RAM) organization assuming
    99. ; relocatible code organization (which is an option in project
    100. ; properties > mpasm (Global Options)).  Absolute mode generally should
    101. ; be used sparingly.
    102. ;
    103. ; Example of using GPR Uninitialized Data
    104. ;
    105. ;   GPR_VAR        UDATA
    106. ;   MYVAR1         RES        1      ; User variable linker places
    107. ;   MYVAR2         RES        1      ; User variable linker places
    108. ;   MYVAR3         RES        1      ; User variable linker places
    109. ;
    110. ;   ; Example of using Access Uninitialized Data Section (when available)
    111. ;   ; The variables for the context saving in the device datasheet may need
    112. ;   ; memory reserved here.
    113. ;   INT_VAR        UDATA_ACS
    114. ;   W_TEMP         RES        1      ; w register for context saving (ACCESS)
    115. ;   STATUS_TEMP    RES        1      ; status used for context saving
    116. ;   BSR_TEMP       RES        1      ; bank select used for ISR context saving
    117. ;
    118. ;*******************************************************************************
    119.  
    120. ; TODO PLACE VARIABLE DEFINITIONS GO HERE
    121.  
    122. ;*******************************************************************************
    123. ; Reset Vector
    124. ;*******************************************************************************
    125.  
    126. RES_VECT  CODE    0x0000            ; processor reset vector
    127.     GOTO    START                   ; go to beginning of program
    128.  
    129. ;*******************************************************************************
    130. ; TODO Step #4 - Interrupt Service Routines
    131. ;
    132. ; There are a few different ways to structure interrupt routines in the 8
    133. ; bit device families.  On PIC18's the high priority and low priority
    134. ; interrupts are located at 0x0008 and 0x0018, respectively.  On PIC16's and
    135. ; lower the interrupt is at 0x0004.  Between device families there is subtle
    136. ; variation in the both the hardware supporting the ISR (for restoring
    137. ; interrupt context) as well as the software used to restore the context
    138. ; (without corrupting the STATUS bits).
    139. ;
    140. ; General formats are shown below in relocatible format.
    141. ;
    142. ;------------------------------PIC16's and below--------------------------------
    143. ;
    144. ; ISR       CODE    0x0004           ; interrupt vector location
    145. ;
    146. ;     <Search the device datasheet for 'context' and copy interrupt
    147. ;     context saving code here.  Older devices need context saving code,
    148. ;     but newer devices like the 16F#### don't need context saving code.>
    149. ;
    150. ;     RETFIE
    151. ;
    152. ;----------------------------------PIC18's--------------------------------------
    153. ;
    154. ; ISRHV     CODE    0x0008
    155. ;     GOTO    HIGH_ISR
    156. ; ISRLV     CODE    0x0018
    157. ;     GOTO    LOW_ISR
    158. ;
    159. ; ISRH      CODE                     ; let linker place high ISR routine
    160. ; HIGH_ISR
    161. ;     <Insert High Priority ISR Here - no SW context saving>
    162. ;     RETFIE  FAST
    163. ;
    164. ; ISRL      CODE                     ; let linker place low ISR routine
    165. ; LOW_ISR
    166. ;       <Search the device datasheet for 'context' and copy interrupt
    167. ;       context saving code here>
    168. ;     RETFIE
    169. ;
    170. ;*******************************************************************************
    171.  
    172. ; TODO INSERT ISR HERE
    173.  
    174. ;*******************************************************************************
    175. ; MAIN PROGRAM
    176. ;*******************************************************************************
    177.  
    178. MAIN_PROG CODE                      ; let linker place main program
    179.  
    180. START
    181.  
    182.     ; TODO Step #5 - Insert Your Program Here
    183.  
    184.     MOVLW 0x55                      ; your instructions
    185.     GOTO $                          ; loop forever
    186.  
    187.     END
    188.  
     
    Last edited by a moderator: Nov 23, 2013
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Your pic should have a template file located here ".......\mpasmx\templates\Code" I often copy those into my assembler project. As it is a template do not overwrite it. However your code should go into here the main section. Also remember to create some sort of endless loop in your main section. And also set your config bits correct
    Code ( (Unknown Language)):
    1. MAIN_PROG CODE ; let linker place main program
    2.  
    3. START
    4.  
    5. ; TODO Step #5 - Insert Your Program Here
    6.  
    7. MOVLW 0x55 ; your instructions
    8. GOTO $ ; loop forever
    9.  
    10. END
     
Loading...