I am trying to write to flash memory on my PIC18F87J11 but I have a problem understanding assembly. The datasheet for my PIC has only assembly and I am using C compiler. I was wondering if someone can help me translate this portion of code to C language. This code can be found here, Section 7.5.
MOVLW CODE_ADDR_UPPER ; Load TBLPTR with the base address
MOVWF TBLPTRU
MOVLW CODE_ADDR_HIGH
MOVWF TBLPTRH
MOVLW CODE_ADDR_LOW
MOVWF TBLPTRL
MOVLW DATA0
MOVWF TABLAT
TBLWT*+
MOVLW DATA1
MOVWF TABLAT
TBLWT*
PROGRAM_MEMORY
BSF EECON1, WPROG ; enable single word write
BSF EECON1, WREN ; enable write to memory
BCF INTCON, GIE ; disable interrupts
MOVLW H'55'
MOVWF EECON2 ; write H'55'
MOVLW H'AA'
MOVWF EECON2 ; write H'AA'
BSF EECON1, WR ; start program (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
BCF EECON1, WPROG ; disable single word write
BCF EECON1, WREN ; disable write to memory
I know this is not a typical question to ask, but if I can receive some help it would be awesome.
It may be easiest to break this into a couple of steps. The first thing that I would do is look at the general overview of what needs to be accomplished prior to looking at the assembly. That can be found in 7.5.2 of your reference.
It's important to note that the ability to perform a word write is enabled when the WPROG bit is set and that the memory location must already be erased. Here are the steps:
We can then break the assembly into the given steps for clarity. I have also commented every line of code so that you can see what the assembly is doing (I would never do this in actual code as it is just distracting).
1. Load Table Pointer with address of data to be written.
2. Write the 2 bytes into the holding registers and perform a table write.
The rest of the steps are included in the PROGRAM_MEMORY section.
Since I have neither the PIC compiler or IDE with me, I know the following C code will not be perfect (please feel free to edit). However, this should give you a good idea of how to construct the C code based on the assembly example.
Hope that helps!