Mike said:
Hi,
I'm trying to learn some basic embedded programming and I thought I would
start out with writing a few little programs for gold smart cards.
I've started out using MPLAB IDE and running my code in the simulator.
I've written a little program that writes a sequence to the file registers
0x0020 to 0x004F. The program works, sometimes. After resetting the
processor and clearing the memory the program will occasionally fail.
Rebuilding, adding a nop, deleting a blank line all seem to make it work
again, mostly.
The first three lines are:
CLRW
MOVLW 0x010
MOVWF FSR
I've set a break point after this and WREG is showing 0x006 and FSR is
0x0013.
I'm sure its something simple and I have no idea what it could be, can any
one suggest where I should be looking or any tests I can use to try to get
more info?
Thanks
-- Michael Heydon
Best thing to start with is flash a led or two.
Then build up from there.
Haven't use an 16f84 or 84A for years.
A good habit to get into is putting the config fuse line into the asm file,
so you can always find it easily.
; Program works for both 16F628 and 16F877 just change processor
definition
;
;
;------------------------------------------------------------------------------------------------------------------------
#define Processor 16F877 ; or 16F628
;************************************************************
if Processor == 16F628
list p=16f628 ; set processor type
#include <P16f628.INC>
__config _CP_OFF & _PWRTE_ON & _HS_OSC & _WDT_OFF & _BODEN_ON &
_LVP_OFF
else ;Must be the 16F877
LIST P=16F877, F=INHX8M
#include <P16F877.inc>
; code protect off debug off program memory write protection off
; low voltage programming off brown out detection on , watch dog timer
off
; High speed(>4MHz) xtal program write on
__config _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _LVP_OFF & _BODEN_ON
& _WDT_OFF & _HS_OSC & _PWRTE_ON
endif
;-------------------------------------------------------------------------------------------------------------------------
; VECTORS
;-------------------------------------------------------------------------------------------------------------------------
org 0x00
reset
call Setup
goto Main
org 0x04
goto IntService
Stop
goto Stop
;-------------------------------------------------------------------------------------------------------------------------
; Interrupt Service Routine
;-------------------------------------------------------------------------------------------------------------------------
IntService
retfie
;--------------------------------------------------------------------------------------------------------------------------
; Setup
;--------------------------------------------------------------------------------------------------------------------------
Setup
bsf STATUS,RP0 ;
bcf STATUS, RP1 ; Bank 1 selected
movlw H'F0'
movwf TRISB ; High Nibble input, low outputs
bcf STATUS, RP0 ; Bank 0 selected
clrf PORTB ; All outputs of Port B reset
return
;--------------------------------------------------------------------------------------------------------------------------
; Main
;--------------------------------------------------------------------------------------------------------------------------
Main
MainLoop
btfss PORTB,4
goto LedOff
bsf PORTB,0
goto MainLoop
LedOff
bcf PORTB,0
goto MainLoop
end
Another simple one but using a delay.
;--------------------------------------------------------------------------
list p=16f877 ; list directive to define processor
#include <p16f877.inc> ; processor specific variable definitions
; configuration fuse settings
; code protect off watch dog timer off, brown out enable , program
write on , high speed oscillator on
; write enable on , low voltage programming off and code protect data
off
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _HS_OSC &
_WRT_ENABLE_ON & _LVP_OFF & _DEBUG_OFF & _CPD_OFF
DT0 EQU 0X20 ; name location 0x20 to DT0
DT1 EQU 0x21 ; name location 0x21 to DT1
DT2 EQU 0x22 ; name location 0x22 to DT2
ORG 0x0000 ; program start address
MAIN ; Main loop
banksel 0 ; bit set flag RP0 in status
register ->select bank 0
clrf TRISB
clrf PORTB
banksel 0 ; bit clear flag -> select bank 0
;MOVLW .7
;MOVWF ADCON1 ;PortA and PortE all digital I/O
NOW CALL DELAY ; call subroutine delay
BSF PORTB,0 ; bit set flag porta location 0 to 1
CALL DELAY ; call subroutine delay
BCF PORTB,0 ; bit clear flag porta location 0 to 0
GOTO NOW ; goto label NOW
;********* DELAY LOOP ********
DELAY ; subroutine delay
MOVLW 0X05 ; move literal 0x05 to W register ->puts 5 in the w reg
MOVWF DT0 ; move contents of w reg to memory location DT0
SDEL
CLRF DT1 ;clear flag DT1
SD2
CLRF DT2 ;clear flag DT2 -> set it to 0
SD1
DECFSZ DT2 ; decrement DT2 skip next if DT2 is zero
GOTO SD1 ; GOTO label SD1
DECFSZ DT1 ; decrement DT1 skip next if DT10 is zero
GOTO SD2 ; GOTO label SD2
DECFSZ DT0 ; decrement DT0 skip next if DT0 is zero
GOTO SDEL ; GOTO label SDEL
RETURN ; return to calling location
END ; END of assembly file