Hello Everyone,
I asked this question on the Microchip Forums but have not gotten a response yet so I decided to take a shot here as well.
Obviously I started to learn PIC ASM through Absolute Mode as this is what the tutorials which came with my PICKit used.
As I start to move to my own projects I realized I want a little more organization to the code as the projects iterate to larger firmware applications which helps design reusable code pieces across projects. To do this efficiently will less problems I found PIC ASM also supports Relocatable asm where the linker decides where to place everything in memory while still leaving you the ability to control where something goes if you have to.
I did come across one issue that I have resolved which had to do with program entry.
The Microchip documentation did a
Reset code 0x0000
This actually generated an error saying reset was an op code so I modified the code to use
reset_vec: code 0x0000
To learn this new style of development I decided to return to the simple hello world blink led application.
If anyone here has experience with PIC relocatable asm I am essentially looking for some best practices and tip to help me with the transition.
The best way I can find for you to make suggestions would be to evaluate my hello world and show me where I made mistakes and how it could be done in a best practice way. Yes the code works perfectly but that does not mean I am doing things correctly in a way that would aid the transition.
; =============================================================================
; Project: Hello World
; Author:
; Chip: PIC16F1829
; Assembler: MPASM v5.49
; Date: 5/2/2013
; =============================================================================
; =============================================================================
; A simple program to blink an LED at a rate of approx. 1/2 second.
; =============================================================================
#include <p16f1829.inc>
; Configure chip fuses
#define CFG1A _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF
#define CFG1B _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG1, CFG1A & CFG1B
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _LVP_OFF
radix dec
errorlevel -302 ; turn off not in bank 0 warning
; =========================================================================
; Shared cross bank data
; =========================================================================
udata_shr
cnt1 res 1
cnt2 res 1
; =========================================================================
; Start up code
; =========================================================================
reset_vec: code 0x0000 ; Reset vector
pagesel start
goto start
code
start:
movlw B'00111000' ; Set cpu clock to 500kHZ
banksel OSCCON ; bank1
movwf OSCCON ; Move w reg to OSCCON
movlw 0 ; Set cnt2 = 0
movwf cnt2
bcf TRISC, 0 ; In bank1 set PORTC0 as output
banksel LATC ; bank2
bsf LATC, 0 ; Turn on PORTC0 LED
call delay_500ms ; 500ms Delay
bcf LATC, 0 ; Turn off PORTC0 LED
call delay_500ms ; 500ms Delay
bra start ; Loop forever
; =============================================================================
; 500ms Delay loop actual speed is 0.50180 seconds.
; Uses the equation {[(3 * cnt1 + 5) * cnt2] + 4} * 8uS
; cnt1 = 80 and cnt2 = 256
; =============================================================================
delay_500ms:
outer:
movlw 80 ; Set cnt1 = 80
movwf cnt1
inner:
decfsz cnt1, f ; Decrement cnt1 if zero skip branch
bra inner
decfsz cnt2, f ; Decrement cnt2 if zero skip branch
bra outer
return
end