btbass said:The place to go is Microchip web site.
There are numerous code examples for download that cover all aspects of pic programming.
blueroomelectronics said:Are you familiar with PIC programming?
/*--------------------------------------------------------------------
Title : Eeprom read and write routines Version 1.01
Filename : eeprom_rw.s
Copyright @ BobTheBass
/*------------------------------------------------------------------
CHANGE HISTORY
Issue Modifier Date Change Description
1.0 RW 01/11/07 First Issue
1.01 MD 11/06/10 (dd/mm/yy)
Software under version control,
see log file for changes.
--------------------------------------------------------------------*/
; NEW
; general config for dsPIC30xxx in MPLAB
.include "p30fxxxx.inc"
; NEW
; compile for absolute C address
; if not needed, comment this line
.equ absolute_C_address, 1
.global _WriteWord
.global _ReadWord
;------ eeprom address
; NEW
; conditionals for developing (30F6014A) and target (30F5013) in my case
.ifdef __30F5013
.equiv EEPROM_SIZE, 0x3ff
.equiv ADDRESS_HI, 0x007F
.equiv ADDRESS_LO, 0xfC00
.endif
.ifdef __30F6014A
.equiv EEPROM_SIZE, 0xfff
.equiv ADDRESS_HI, 0x007F
.equiv ADDRESS_LO, 0xf000
.endif
;------ Memory opType.
.equiv ERASE_WORD, 0x4044
.equiv WRITE_WORD, 0x4004
;------ Write a word to eeprom
_WriteWord:
push w4
mov #ADDRESS_HI,w4
mov w4,TBLPAG
mov w4,NVMADRU
mov #ADDRESS_LO,w4
;NEW
; simple :)
.ifdef absolute_C_address
sub w0,w4,w0
.endif
sl w0,#1,w0
add w0,w4,w0
mov w0,NVMADR
rcall EraseWord
mov #WRITE_WORD,w4
mov w4,NVMCON
TBLWTL w1,[w0]
rcall KeySequence
pop w4
return
;------ Read a word from eeprom
_ReadWord:
push w4
mov #ADDRESS_HI,w4
mov w4,TBLPAG
mov w4,NVMADRU
mov #ADDRESS_LO,w4
mov w4,NVMADR
;NEW
; simple :)
.ifdef absolute_C_address
sub w0,w4,w0
.endif
sl w0,#1,w0
add w0,w4,w4
tblrdl [w4],w0
pop w4
return
;------ Erase a word from eeprom
EraseWord:
mov #ERASE_WORD,w4
mov w4,NVMCON
;------ Required key sequence for write or erase.
KeySequence:
disi #16
mov #0x55,w4
mov w4,NVMKEY
mov #0xaa,w4
mov w4,NVMKEY
bset NVMCON,#WR
nop
nop
K1wait: btsc NVMCON,#WR
bra K1wait
return
.end
;------ End of file.
downybear said:Hi all,
very nice and working code.
I did a small improvements to easy use in ASM/C environment.
So,
I have some variables in C:
unsigned int var_1;
unsigned int var_2;
unsigned int _EEDATA(2) var_1_eep = {0x5678};
unsigned int _EEDATA(2) var_2_eep = {0x1234};
If I would like to write/read it to/from eeprom, I think that very good idea is:
WriteWord(&var_1_eep, var_1);
and
var_1 = ReadWord(&var_1_eep);
This is easier and cleaner - instead of this we need obtain eeprom address of the expected variable.
To do this is very easy, infos in code.
Enjoy
Regards
Mariusz
Code:/*-------------------------------------------------------------------- Title : Eeprom read and write routines Version 1.01 Filename : eeprom_rw.s Copyright @ BobTheBass /*------------------------------------------------------------------ CHANGE HISTORY Issue Modifier Date Change Description 1.0 RW 01/11/07 First Issue 1.01 MD 11/06/10 (dd/mm/yy) Software under version control, see log file for changes. --------------------------------------------------------------------*/ ; NEW ; general config for dsPIC30xxx in MPLAB .include "p30fxxxx.inc" ; NEW ; compile for absolute C address ; if not needed, comment this line .equ absolute_C_address, 1 .global _WriteWord .global _ReadWord ;------ eeprom address ; NEW ; conditionals for developing (30F6014A) and target (30F5013) in my case .ifdef __30F5013 .equiv EEPROM_SIZE, 0x3ff .equiv ADDRESS_HI, 0x007F .equiv ADDRESS_LO, 0xfC00 .endif .ifdef __30F6014A .equiv EEPROM_SIZE, 0xfff .equiv ADDRESS_HI, 0x007F .equiv ADDRESS_LO, 0xf000 .endif ;------ Memory opType. .equiv ERASE_WORD, 0x4044 .equiv WRITE_WORD, 0x4004 ;------ Write a word to eeprom _WriteWord: push w4 mov #ADDRESS_HI,w4 mov w4,TBLPAG mov w4,NVMADRU mov #ADDRESS_LO,w4 ;NEW ; simple :) .ifdef absolute_C_address sub w0,w4,w0 .endif sl w0,#1,w0 add w0,w4,w0 mov w0,NVMADR rcall EraseWord mov #WRITE_WORD,w4 mov w4,NVMCON TBLWTL w1,[w0] rcall KeySequence pop w4 return ;------ Read a word from eeprom _ReadWord: push w4 mov #ADDRESS_HI,w4 mov w4,TBLPAG mov w4,NVMADRU mov #ADDRESS_LO,w4 mov w4,NVMADR ;NEW ; simple :) .ifdef absolute_C_address sub w0,w4,w0 .endif sl w0,#1,w0 add w0,w4,w4 tblrdl [w4],w0 pop w4 return ;------ Erase a word from eeprom EraseWord: mov #ERASE_WORD,w4 mov w4,NVMCON ;------ Required key sequence for write or erase. KeySequence: disi #16 mov #0x55,w4 mov w4,NVMKEY mov #0xaa,w4 mov w4,NVMKEY bset NVMCON,#WR nop nop K1wait: btsc NVMCON,#WR bra K1wait return .end ;------ End of file.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?