• Pic® Basic


  • PIC24 Flash Memory Write

    Here is an example on how to save data at Run-Time in program flash memory of PIC24F without an EEPROM. Also known as RTSP (Run-Time Self-Programming)

    Code:
    '********************************************************************
    '*  Name    : PIC24 FLASH MEMORY WRITE.BAS                          *
    '*  Author  : KNC                                                   *
    '*  Notice  : Copyright (c) 2018 [select VIEW...EDITOR OPTIONS]     *
    '*          : All Rights Reserved                                   *
    '*  Date    : 3/31/2018                                             *
    '*  Version : 1.0                                                   *
    '********************************************************************
        Device = 24FJ16GA004
        Declare Xtal= 16                 ' Set OSC to 16MHz
        Declare Hserial_Baud = 9600      ' USART1 baud rate
        Declare HRSOut1_Pin = PORTB.14   ' Select the pin for TX with USART1
        Declare HRSIn1_Pin  = PORTB.15   ' Select the pin for RX with USART1
    
    
        Include "Pull_Up.inc"            ' Include internal pullup macros
        
        OSCCON = $2200                   ' Use external XT w/o PLL
        CLKDIV = 0                       ' CPU peripheral clock ratio set to 1:1
        
        RPOR7 = 3                        ' Make RP14(PORTB.14) the pin for UART1 TX
        RPINR18 = 15                     ' Make RP15(PORTB.15) the pin for UART1 RX    
    
    
        TRISB = 00111111111111        ' Input 1 to 12 / TX and RX
    '------------------------------------
    '   Enable internal Pull_up resistor
    '------------------------------------
        Pullup_Enable_RB0()
        Pullup_Enable_RB1()
        Pullup_Enable_RB2()
        Pullup_Enable_RB3()
        Pullup_Enable_RB4()
        Pullup_Enable_RB5()
        Pullup_Enable_RB6()
        Pullup_Enable_RB7()
        Pullup_Enable_RB8()
        Pullup_Enable_RB9()
        Pullup_Enable_RB10()
        Pullup_Enable_RB11()
    
    
    '----------------------------
    '   Buffers variables
    '----------------------------
        Dim uStorage[64]  As Word        ' 64 element Word size array / user variable
        Dim vMemory[64]   As Word        ' 64 element Word size array / virtual EEPROM
    '----------------------------
    '   variables
    '----------------------------
        Dim Input_port As Word
        Dim wAddress As Word
        Dim x       As Byte
    
    
    GoTo MAIN
    '----------------------------
    '     FLASH ERASE
    '----------------------------
    F_ERASE:                         ' Setup block erase operation
        NVMCON = $4042               ' Initialize NVMCON bits
        TBLPAG = 0                   ' Initialize PM Page Boundary SFR
        WREG8 = $1400                ' Initialize in-page EA<15:0> pointer and 
                                     ' base address of erase block
        Asm
        TblwtL W8, [W8]              ' Set base address of erase block
        Disi #5                      ' Block all interrupts with priority <7
        EndAsm                       ' for next 5 instructions
        
        NVMKEY = $55                 ' Write the 0x55 key
        NVMKEY = $AA                 ' Write the 0xAA key
        NVMCON.15 = 1                ' Start the erase/write sequence
        Nop                          ' Insert two NOPs after the write sequence
        Nop
        While NVMCON.15 = 1 : Wend   ' wait until erase/write operation is complete
        Return
    '----------------------------
    '     FLASH WRITE
    '----------------------------
    F_WRITE:                         ' Setup write by row operation
        NVMCON = $4001               ' Initialize NVMCON
        TBLPAG = 0                   ' Initialize PM Page Boundary SFR
        WREG8 = $1400                ' starting address for writing data
    
    
        For x = 0 To 63              ' perform TBLWT to write the latches
            WREG4 = vMemory[x]       ' move variable to W4
            Asm
            Mov #0x0000, W5 ;        ' clear High Word          
            TblwtL W4, [W8]          ' Write PM low word into program latch
            TblwtH W5, [W8++]        ' Write PM high byte into program latch
            EndAsm
        Next
    
    
        Asm                          ' Apply Write operation
        Disi #5                      ' Block all interrupts with priority <7
        EndAsm                       ' for next 5 instructions
        
        NVMKEY = $55                 ' Write the 0x55 key
        NVMKEY = $AA                 ' Write the 0xAA key
        NVMCON.15 = 1                ' Start the erase/write sequence
        Nop                          ' Insert two NOPs after the write sequence
        Nop
        While NVMCON.15 = 1 : Wend   ' wait until erase/write operation is complete
        Return
    '----------------------------
    '     FLASH READ
    '----------------------------
    F_Read:                                       ' Read Virtual EEPROM
        wAddress = $1400                          ' Load Virtual EEPROM base address
        For x = 0 To 63                           ' x64 location (starting at 1st register)
          uStorage[x] = cPtr16 (wAddress++)       ' Read from Virtual EEPROM address 0x1400 to 0x143F
        Next                                      ' and load to uStorage
        Return
    
    
    '----------------------------
    '   Main Code
    '----------------------------
    MAIN:
        DelayMS 500
        test = 0
        
        While 1 = 1
          Input_port = PORTB                      ' read input port
          If Input_port <> 0 Then                 ' if input signal is present
            For x = 0 To 63                       ' load 64 address locations
                vMemory[x] = x + Input_port       ' plus input values to vMemory
            Next                                  '
            
            GoSub F_ERASE                         ' Erase vEEPROM registers
            GoSub F_WRITE                         ' Write vEEPROM registers
          EndIf
         
          GoSub F_Read                            ' Read vEEPROM registers
          For x = 0 To 63                         ' Send all 64 word data out
            HSerOut [uStorage[x]]        
          Next
          DelayMS 100
        Wend
    
    
    End
    
    
    '-------------------------------------------------------------------------------------------------------------
    ' Fuse configuration / Flash Configuration Word
    ' For External 16MHz oscillator without PLL
    ' OSC pins FOSC/2
    '  
        Config Config1 = JTAGEN_OFF, GCP_OFF, GWRP_OFF, BKBUG_OFF, COE_OFF,_
                         ICS_PGx1, FWDTEN_OFF, WINDIS_ON, FWPSA_PR128, WDTPOST_PS256 
        Config Config2 = IOL1WAY_OFF, COE_OFF, IESO_OFF, FNOSC_PRI,_
                         FCKSM_CSDCMD, OSCIOFNC_OFF, POSCMOD_XT 
    
    
    '-------------------------------------------------------------------------------------------------------------
    Erasing per block of 64 pages is the minimum way of erasing data in program flash memory and programming is done in the following order.
    • Flash Page Erases
    • Row Programming (either latch-based or RAM-based)
    • Word Programming
    • Double-Word Programming (select devices only)

    For more information please check the link below;

    http://ww1.microchip.com/downloads/e.../30009715c.pdf
  • Recent Activity

    Maxi-15375

    Driving SK9822 or APA102 RGB LED's

    Thread Starter: rcurl

    I've been trying to figure out how to drive SK9822 or APA102 RGB LED's using the MSSP module in a PIC16F1503 but I'm getting nowhere. Part of the...

    Maxi Today, 17:07 Go to last post