News:

Let's find out together what makes a PIC Tick!

Main Menu

Flash memory reading and writing examples for different mcu's.

Started by OG, Mar 21, 2021, 06:40 PM

Previous topic - Next topic

OG

Use of program memory for eeprom purposes for 12F1572.
(Les's examples were used for writing and erasing routines.)



High-Endurance Flash (datasheet page 1 and page 15)
12F1572_1.JPG


You should make sure that your main program does not interfere with the area you reserved for eeprom purposes.
To test this, you can temporarily turn off rows containing ASM ORG EndAsm, as below; 
''Asm
''Org 2000   
''EndAsm
Check the program end word after compiling it with this shape.
After you are sure, open the relevant lines and compile them again.

The value 2000 is a chosen value for this mcu.
It is possible to select different values, description below.


Important notes (before the eeprom_purpose tag);
''' Must be selected from High endurance Flash memory 
        ''' Last 128 bytes for 12F1572  0780h-07FFh (1920-2047)
''' Write and erase should be done in 1 row.
        ''' For 12F1572 it is 1 row 16 words. (datasheet page 92)

'' The address where I started to read and write
    '' The High Endurance Flash Memory starting point must be +0 or +16 or a multiple of +16.

FLASH MEMORY ORGANIZATION BY DEVICE (page 92)

12F1572_2.JPG

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings
Device = 12F1572
Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
__Config _Config1, 0x391C ;FOSC_INTOSC & WDTE_ON & PWRTE_ON & MCLRE_OFF & CP_ON & BOREN_OFF & CLKOUTEN_OFF
__Config _Config2, 0x14FF ;WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_LO & LPBOREN_ON & LVP_OFF
Endasm-
Declare Reminders On
;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

Declare Xtal = 16
Declare All_Digital= TRUE
   
$define Flash_Erase() GoSub _Flash_Erase
   
OSCCON = %01111010                  ' Setup the device for internal clock at 16MHz

PORTA = 0
TRISA = %00111011
PORTA = 0 

ANSELA = %00000000            
OPTION_REG = %10001111
WDTCON = %00011001  ''' bit 5-1 1:131072 (217) (Interval 4s nominal)(Reset value) page 89

Dim my_Array[7] As Byte
Dim count_1 As Byte

Dim reg_adr As Word
Dim var_1 As Byte

'''  The component names for the array (Aliases).
Dim power As my_Array#0     ''' byte my_Array[0]
Dim p1_L As my_Array#1
Dim p1_H As my_Array#2
Dim p2_L As my_Array#3
Dim p2_H As my_Array#4
Dim p3_L As my_Array#5
Dim p3_H As my_Array#6      ''' byte my_Array[6]

'''''''''''
'''  Les's examples were used for writing and erasing routines.
'''  Note;  Clrwdt; To avoid being reset, it may not be used depending on the state of WDT.

Main:
    GoSub read_power
    ''' Program Flow
    ''' bla bla
   
    GoSub block_read    '' Read the variables to be used from flash memory.

    ''' Program Flow
    ''' Program Flow 

    GoSub block_write   '' Write the variables To be stored in the Flash memory.

    ''' bla bla
GoTo Main

'''''''''''''''

''  Read your power state from flash memory. 
read_power:
    count_1 = 0         '' adr  0
    GoSub single_read   '' Read the address eeprom_purpose + 0. 
    power = var_1       '' var_1 = returning value
Return

''''''''''''''''
single_read:
    var_1 = CRead8 eeprom_purpose[count_1]
Return
'''''''''''''
block_read:
    For count_1 = 0 To 6
        my_Array[count_1] = CRead8 eeprom_purpose[count_1] 
   
        '''  or these can be used instead of the upper line (If desired to reduce the code)
            '''  GoSub single_read
            '''  my_Array[count_1] = var_1   
    Next
    Clrwdt
Return
''''''''''''

block_write:
    Clrwdt     
    reg_adr = eeprom_purpose
    Flash_Erase()      
    Clrwdt   
    CWrite eeprom_purpose ,[power,p1_L,p1_H,p2_L,p2_H,p3_L,p3_H,0,0,0,0,0,0,0,0,0]   ''' 0~15 (16 byte)
    Clrwdt
Return

''''''''''
_Flash_Erase:
    PMADRL = reg_adr.LowByte
    PMADRH = reg_adr.HighByte 
   
    Clear PMCON1.6   ' Not configuration space
    Set PMCON1.4     ' Specify an erase operation
    Set PMCON1.2     ' Enable writes
    PMCON2 = $55            ' \ Issue the required sequence to initiate erase
    PMCON2 = $AA            ' /
    Set PMCON1.1       ' Set WR bit to begin erase
    Nop                     ' Nop instructions are forced as processor starts
    Nop                     ' Row erase of program memory
    Clear PMCON1.2   ' Disable writes
Clrwdt
Return
''''''''''''

''' You should make sure that your main program does not interfere with the area you reserved for eeprom purposes.
    ''' To test this, you can temporarily turn off rows containing ASM ORG EndAsm, as below;
''Asm
''Org 2000    
''EndAsm
''    Check the program end word after compiling it with this shape.
''    After you are sure, open the relevant lines and compile them again. 


;;;;; ****   WRITE and ERASE area for eeprom purposes.   *** ;;;;;
''' Must be selected from High endurance Flash memory 
        ''' Last 128 bytes for 12F1572   0780h-07FFh (1920-2047)
''' Write and erase should be done in 1 row.
        ''' For 12F1572 it is 1 row 16 words. (datasheet page 92)

'' The address where I started to read and write
    '' The High Endurance Flash Memory starting point must be +0 or +16 or a multiple of +16. 
Asm
Org 2000      ''' 2000 = $07D0  (adr  0,  adr 0 below) eeprom_purpose tag's address value in flash memory
EndAsm         

eeprom_purpose:
CData $FF,_  '' adr  0,         (for variable power)          eeprom_purpose + 0 = 2000
$FE,_        '' adr  1, p1_L    (for variable p1 Low.Byte )   eeprom_purpose + 1 = 2001
$10,_        '' adr  2, p1_H    (For Variable p1 High.Byte)   eeprom_purpose + 2
$FE,_        '' adr  3, p2_L    other variables               eeprom_purpose + 3
$10,_        '' adr  4, p2_H    other variables               eeprom_purpose + 4
$FE,_        '' adr  5, p3_L    other variables               eeprom_purpose + 5
$10,_        '' adr  6, p3_H    other variables               eeprom_purpose + 6 = 2006
0,_          '' adr  7,         other variables - not in use  eeprom_purpose + 7 = 2007
0,_          '' adr  8,         other variables - not in use  eeprom_purpose + 8 = 2008
0,_          '' adr  9,         other variables - not in use 
0,_          '' adr 10,         other variables - not in use 
0,_          '' adr 11,         other variables - not in use 
0,_          '' adr 12,         other variables - not in use 
0,_          '' adr 13,         other variables - not in use 
0,_          '' adr 14,         other variables - not in use 
0         '' adr 15,       1 row end for 12F1572. see TABLE 10-1 (page 92)
'0,_          '' adr 16, 
'0,_          '' adr 17,
'0,_          '' adr 18,
'0,_          '' adr 19,
'0,_          '' adr 20,
'0,_          '' adr 21,
'0,_          '' adr 22,
'0,_          '' adr 23,
'0,_          '' adr 24,
'0,_          '' adr 25,
'0,_          '' adr 26,
'0,_          '' adr 27,
'0,_          '' adr 28,
'0,_          '' adr 29,
'0,_          '' adr 30,
'0         '' adr 31,  It can go up to adr 127.  adr 127 = flash memory 2047