Example: Writing NVEM using NVPopByte

This example is a stand-alone program that demonstrates writing to NVEM0 using the NVPopByte instruction.

Note: Due to limited endurance of the flash memory, writing NVEM0 is useful only for very infrequently updated data. For most SPLat boards, more than 10,000 individual NVPopByte operations may wear out the flash memory and void the warranty on the controller.

;Example of writing to NVEM using NVPopByte. This program writes incrementing counts to 3 contiguous MNVEM bytes,
;then reads them back to RAM so they can be viewed via SPLat/PC using SPLatLink ("S/L" button in the Module window)

        nvSetPtr        MyRec       ;Set the pointer to a pretend data base
        nvSetReclen     3           ;Set the pretend record length
        nvSetRecNum     1           ;Use one fixed record number

        LaunchTask      WriteNV     ;Write stuff
        LaunchTask      ReadNV      ;Read back stuff
        RunTasksForever

;===========================================================================================================       
;Task that continuously writes NVEM. It stores 3 incrementing numbers at a certain
;non-zero location in NVEM, running once per second.
;To prevent excessive wear on the NVEM it deliberately stops writing after 60 cycles (180 writes, over 1 minute).
;Be aware that the nominal lifetime of NVEM in most SPLat boards is 10,000 writes, so don't
;run this program too many times or disable the counter. (The warranty does not cover worn out NVEM!!!!)
 
WrData  defByte  
WrCount defByte
    
WriteNV:
        SetMem          WrCount,60

WrLoop:
        Recall          WrData 
        Push         
        nvpopbyte       0           ;Store a value
        IncX                        ;increment the value
        Push
        nvpopbyte       1           ;Store a value      
        IncX                        ;increment the value
        Push
        nvpopbyte       2           ;Store a value 
        Store           WrData
        Pause           100         ;Delay 1S between writes
        DMGNZ            WrCount,WrLoop  ;Only do 256 writes before crashing, to protect Flash from wear-out
        
WrKill: YieldTask
        GoTo            WrKill    ;Deliberately stop writing. Program must be restarted to write again.           
                                                                 
;===========================================================================================================       
;This task reads out the NVMem that's written by WriteNV, and stores in in ordinary RAM in locations 0-2.
;From there it can be viewed in SPLat/PC using the SPLatLink monitoring accessed through the
;Module window ("S/L")
                                                                 
ReadNV:
        Pause           1
        NVReadRec       0       ;Read back the NVEM record, as NVRecLen bytes, to RAM 0
        GoTo            ReadNV
                                                                        
;=============== NVEM declarations ========================================        
        NVEM0
        
        NV0Space        512        ;NVEM may not be written within one block length of the end of the program.
                                   ;The block length is 512 bytes for most models. This line wastes that much space, unavoidably


MyRec   NV0Space        200        ;Arbitrary (in this case) allocation to the writable data.