0

In the datasheet of PIC18F4550 the example to write to flash program memory is the following:

            MOVLW D'64’                    ; number of bytes in erase block
            MOVWF COUNTER
            MOVLW BUFFER_ADDR_HIGH         ; point to buffer
            MOVWF FSR0H
            MOVLW BUFFER_ADDR_LOW
            MOVWF FSR0L
            MOVLW CODE_ADDR_UPPER          ; Load TBLPTR with the base
            MOVWF TBLPTRU                  ; address of the memory block
            MOVLW CODE_ADDR_HIGH
            MOVWF TBLPTRH
            MOVLW CODE_ADDR_LOW
            MOVWF TBLPTRL
READ_BLOCK
            TBLRD*+                        ; read into TABLAT, and inc
            MOVF TABLAT, W                 ; get data
            MOVWF POSTINC0                 ; store data
            DECFSZ COUNTER                 ; done?
            BRA READ_BLOCK                 ; repeat
MODIFY_WORD
            MOVLW DATA_ADDR_HIGH           ; point to buffer
            MOVWF FSR0H
            MOVLW DATA_ADDR_LOW
            MOVWF FSR0L
            MOVLW NEW_DATA_LOW             ; update buffer word
            MOVWF POSTINC0
            MOVLW NEW_DATA_HIGH
            MOVWF INDF0
ERASE_BLOCK
            MOVLW CODE_ADDR_UPPER          ; load TBLPTR with the base
            MOVWF TBLPTRU                  ; address of the memory block
            MOVLW CODE_ADDR_HIGH
            MOVWF TBLPTRH
            MOVLW CODE_ADDR_LOW
            MOVWF TBLPTRL
            BSF EECON1, EEPGD              ; point to Flash program memory
            BCF EECON1, CFGS               ; access Flash program memory
            BSF EECON1, WREN               ; enable write to memory
            BSF EECON1, FREE               ; enable Row Erase operation
            BCF INTCON, GIE                ; disable interrupts

            MOVLW 55h 
Required    MOVWF EECON2                   ; write 55h
Sequence    MOVLW 0AAh
            MOVWF EECON2                   ; write 0AAh
            BSF EECON1, WR                 ; start erase (CPU stall)

            BSF INTCON, GIE                ; re-enable interrupts
            TBLRD*-                        ; dummy read decrement
            MOVLW BUFFER_ADDR_HIGH         ; point to buffer
            MOVWF FSR0H
            MOVLW BUFFER_ADDR_LOW
            MOVWF FSR0L
            MOVLW D’2’
            MOVWF COUNTER1
WRITE_BUFFER_BACK
            MOVLW D’32’                    ; number of bytes in holding register
            MOVWF COUNTER
            WRITE_BYTE_TO_HREGS
            MOVF POSTINC0, W               ; get low byte of buffer data
            MOVWF TABLAT                   ; present data to table latch
            TBLWT+*                        ; write data, perform a short write
                                           ; to internal TBLWT holding register.
            DECFSZ COUNTER                 ; loop until buffers are full
            BRA WRITE_WORD_TO_HREGS
PROGRAM_MEMORY
            BSF EECON1, EEPGD              ; point to Flash program memory
            BCF EECON1, CFGS               ; access Flash program memory
            BSF EECON1, WREN               ; enable write to memory
            BCF INTCON, GIE                ; disable interrupts

            MOVLW 55h
Required    MOVWF EECON2                   ; write 55h
Sequence    MOVLW 0AAh
            MOVWF EECON2                   ; write 0AAh
            BSF EECON1, WR                 ; start program (CPU stall)

            DECFSZ COUNTER1
            BRA WRITE_BUFFER_BACK
            BSF INTCON, GIE                ; re-enable interrupts
            BCF EECON1, WREN               ; disable write to memory

but i dont know how to interpret this code, i mean:

-in which instruction i select the location in memory to start writing

-where do i write the instructions to be written

for example, beside my main code, if i want to write in flash the next instructions after 5v are detected in some I/O of PORTB:

movlw    0CEh
movwf    TRISA

what changes do i have to do in the example code of the datasheet? or am i inerpreting wrong what 'writing to flash program memory' mean?

esencia
  • 23
  • 2
  • The method described in your question would be difficult even impossible to implement in a PIC18F4550. If it could be implemented the operational behavior is likely to be unstable. You should seek a solution that does not involve changing the op-code to be executed. – Dan1138 Nov 03 '18 at 19:17
  • so is not possible to write instructions in flash after programming the PIC? or just in this PIC? do u have an idea of how may i do what i want to do? – esencia Nov 03 '18 at 19:20
  • Using program flash for non-volatile storage is done using this approach, but the PIC18F4550 has 256 bytes of EEPROM available for that purpose. Would that work for you? – jolati Nov 03 '18 at 22:47

1 Answers1

0

in which instruction i select the location in memory to start writing

    MOVLW CODE_ADDR_UPPER          ; Load TBLPTR with the base
    MOVWF TBLPTRU                  ; address of the memory block
    MOVLW CODE_ADDR_HIGH
    MOVWF TBLPTRH
    MOVLW CODE_ADDR_LOW
    MOVWF TBLPTRL

-where do i write the instructions to be written

    MOVLW BUFFER_ADDR_HIGH         ; point to buffer
    MOVWF FSR0H
    MOVLW BUFFER_ADDR_LOW
    MOVWF FSR0L
Mike
  • 4,041
  • 6
  • 20
  • 37