Hi Molten,
Here are some functions to get you started. It's not particularly neat (I would re-write it without having a union), but it works for me. Apologies, there might be some slight syntax errors, as I've pasted this from a much larger set of functions (with CRC, different sectors for different settings, and various other bits and pieces).
Be warned, many of the dspic33F's have a guaranteed flash life of 100 write cycles. You could call WriteNV() thousands of times a second and trash your PIC's flash in a second. Make sure you don't!!!
These functions are documented in the C30 manual, and the headers are fairly helpful, and if you search for them in the forum there will be many hits.
#define NV_NUM_PAGES 1
typedef union
{
int Ints[_FLASH_ROW * NVRAM_NUM_PAGES];
char Chars[_FLASH_ROW * NVRAM_NUM_PAGES * sizeof(int)];
} NVStorage;
typedef struct
{
int Ints[_FLASH_PAGE * NVRAM_NUM_PAGES];
} NVPage;
static NVStorage RAMParams;
__prog__ NVPage __attribute__((space(prog))) NVParams;
void ReadNV(void)
{
_prog_addressT p;
_init_prog_address(p, NVParams.Ints);
memset((void *)RAMParams.Chars, 0, sizeof(NVStorage));
_memcpy_p2d16((void *)RAMParams.Chars, p, sizeof(NVStorage));
}
void WriteNV(void)
{
int i;
_init_prog_address(p, NVParams.Ints);
_erase_flash(p);
_init_prog_address(p, NVParams.Ints);
for(i = 0; i < NVRAM_NUM_PAGES; i++)
{
_write_flash16(p, &RAMParams.Ints[i * _FLASH_ROW]);
p += (_FLASH_ROW * 2);
}
}
Pete