EEPROM - Read/Write
I am using a device with EEPROM -> 24FV16KM102
How to read and write in it?
How to read and write in it?
// builtins.bas
// fw interface to XC16 __builtin_xxxxx functions
// v1.00
// examples of accessing the XC16 _builtin functions
module builtins
imports "builtins.c"
public external sub _builtin_write_OSCCONL(value as byte) "fw_builtin_write_OSCCONL"
public external sub _builtin_write_OSCCONH(value as byte) "fw_builtin_write_OSCCONH"
public external function _builtin_tblrdh(offset as ushort) as ushort "fw_builtin_tblrdh"
public external function _builtin_tblrdl(offset as ushort) as ushort "fw_builtin_tblrdl"
public external sub _builtin_tblwth(offset as ushort, data as ushort) "fw_builtin_tblwth"
public external sub _builtin_tblwtl(offset as ushort, data as ushort) "fw_builtin_tblwtl"
public external sub _builtin_write_NVM() "fw_builtin_write_NVM"
end module
//
// builtins.c
// fw interface to XC16 __builtin_xxxxx functions (and builtins.bas)
// (see Appendix G in the MPLAB XC16 C Compilers Users Guide)
// v1.00
//
/*
//-------------------------------------------------------
__builtin_write_OSCCONL
Description:
Unlocks and writes its argument to OSCCONL.
Interrupts may need to be disable for proper operation.
This builtin function can be used as a part of a complex sequence discussed in the data
sheet or family reference manual.
See this documentation for more information.
Prototype:
void __builtin_write_OSCCONL(unsigned char value);
Argument:
value character to be written
Return Value:
None.
Assembler Operator/Machine Instruction:
mov #0x46, w0
mov #0x57, w1
mov #_OSCCON, w2
mov.b w0, [w2]
mov.b w1, [w2]
mov.b value, [w2]
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_OSCCONL(unsigned char value)
{
__builtin_write_OSCCONL(value);
}
/*
//-------------------------------------------------------
__builtin_write_OSCCONH
Description:
Unlocks and writes its argument to OSCCONH.
Interrupts may need to be disable for proper operation.
This builtin function can be used as a part of a complex sequence discussed in the data
sheet or family reference manual.
See this documentation for more information.
Prototype:
void __builtin_write_OSCCONH(unsigned char value);
Argument:
value character to be written
Return Value:
None.
Assembler Operator/ Machine Instruction:
mov #0x78, w0
mov #0x9A, w1
mov #_OSCCON+1, w2
mov.b w0, [w2]
mov.b w1, [w2]
mov.b value, [w2]
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_OSCCONH(unsigned char value)
{
__builtin_write_OSCCONH(value);
}
/*
//-------------------------------------------------------
__builtin_tblrdh
Description:
Issues the tblrdh.w instruction to read a word from Flash or EEDATA memory. You must
set up the TBLPAG to point to the appropriate page. To do this, you may make use of
__builtin_tbloffset() and __builtin_tblpage().
Please refer to the data sheet or dsPIC Family Reference Manual for complete details
regarding reading and writing program Flash.
Prototype:
unsigned int __builtin_tblrdh(unsigned int offset);
Argument:
offset desired memory offset
Return Value:
Contents of the memory address in Flash or EEDATA memory.
Assembler Operator/ Machine Instruction:
tblrdh
Error Messages None.
//-------------------------------------------------------
*/
unsigned int fw_builtin_tblrdh(unsigned int offset)
{
return __builtin_tblrdh(offset);
}
/*
//-------------------------------------------------------
__builtin_tblrdl
Description:
Issues the tblrdl.w instruction to read a word from Flash or EEDATA memory. You must
set up the TBLPAG to point to the appropriate page. To do this, you may make use of
__builtin_tbloffset() and__builtin_tblpage().
Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
plete details regarding reading and writing program Flash.
Prototype:
unsigned int __builtin_tblrdl(unsigned int offset);
Argument:
offset desired memory offset
Return Value:
Contents of the memory address in Flash or EEDATA memory.
Assembler Operator/ Machine Instruction:
tblrdl
Error Messages None.
//-------------------------------------------------------
*/
unsigned int fw_builtin_tblrdl(unsigned int offset)
{
return __builtin_tblrdl(offset);
}
/*
//-------------------------------------------------------
__builtin_tblwth
Description:
Issues the tblwth.w instruction to write a word to Flash or EEDATA memory. You must set
up the TBLPAG to point to the appropriate page. To do this, you may make use of
__builtin_tbloffset() and __builtin_tblpage().
Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
plete details regarding reading and writing program Flash.
Prototype:
void __builtin_tblwth(unsigned int offset, unsigned int data);
Argument:
offset desired memory offset
data data to be written
Return Value:
None.
Assembler Operator/ Machine Instruction:
tblwth
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_tblwth(unsigned int offset, unsigned int data)
{
__builtin_tblwth(offset, data);
}
/*
//-------------------------------------------------------
__builtin_tblwtl
Description:
Issues the tblrdl.w instruction to write a word to Flash or EEDATA memory. You must set
up the TBLPAG to point to the appropriate page. To do this, you may make use of
__builtin_tbloffset() and __builtin_tblpage().
Please refer to the data sheet or “dsPIC30F Family Reference Manual” (DS70046) for com-
plete details regarding reading and writing program Flash.
Prototype:
void __builtin_tblwtl(unsigned int offset, unsigned int data);
Argument:
offset desired memory offset
data data to be written
Return Value:
None.
Assembler Operator/ Machine Instruction:
tblwtl
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_tblwtl(unsigned int offset, unsigned int data)
{
__builtin_tblwtl(offset, data);
}
/*
//-------------------------------------------------------
__builtin_write_NVM
Description:
Enables the Flash for writing by issuing the correct unlock sequence and enabling the Write
bit of the NVMCON register.
Interrupts may need to be disable for proper operation.
This builtin function can be used as a part of a complex sequence discussed in the data
sheet or family reference manual.
See this documentation for more information.
Prototype:
void __builtin_write_NVM(void);
Argument:
None.
Return Value:
None.
Assembler Operator/ Machine Instruction:
mov #0x55, Wn
mov Wn, _NVMKEY
mov #0xAA, Wn
mov Wn, _NVMKEY
bset _NVMCON, #15
nop
nop
Error Messages None.
//-------------------------------------------------------
*/
void fw_builtin_write_NVM(void)
{
__builtin_write_NVM();
}
imports builtins
dim offs as ushort
dim data as ushort
Sub Main()
offs = _builtin_tblrdh(offs)
offs = _builtin_tblrdl(offs)
_builtin_tblwth(offs, data)
_builtin_tblwtl(offs, data)
_builtin_write_NVM()
// from XC pps.h
// #define PPSUnLock __builtin_write_OSCCONL(OSCCON & 0xbf)
// unlock the Pin assignment registers
_builtin_write_OSCCONL(OSCCON and $BF) // to clear IOLOCK
// #define PPSLock __builtin_write_OSCCONL(OSCCON | 0x40)
// Relock the Pin assignment registers
_builtin_write_OSCCONL(OSCCON or $40) // to set IOLOCK
End Sub
__builtin_dmaoffset(_ram_protect_a)
__builtin_dmaoffset(_ram_protect_a)
char buffer[256] __attribute__((space(dma)));
Const EEAddr as Byte = &H0
EEPROM(EEAddr) = {15 As Byte}
...
Dim bValue as Byte = Eprom.ReadByte(EEAddr)
...
Eprom.Write(EEAddr,bValue)
the ROM routines doesn't work
// 1-row (96 bytes), 2-row (192 bytes) and 4-row (384 bytes) erase blocks
// selection must match NVMCON setting
#option ROM_BLOCK_ERASE = 384 ' was 1536
#option ROM_BLOCK_WRITE = 96 ' was 192