|
|
View previous topic :: View next topic |
Author |
Message |
tojape
Joined: 14 Jan 2007 Posts: 20 Location: Hungary
|
PIC18f45J10 flash programming problem |
Posted: Thu May 24, 2007 12:35 pm |
|
|
Hi guys,
The following code results to a flash write to a different location than I expect. Namely I load 0x0400 as the write address and my code writes to 0x0440. I checked every registers displayig them on a terminal and everything seems ok, except for the final result. I'm almost lost, can anyone help?
Code: |
//PROC: PIC18F45J10, compiled with CCS PCWH V4.021
write_flash:
//GETTING PARAMS
MOVFF _target_upper,TBLPTRU // contains 0X00
MOVFF _target_high,TBLPTRH // contains 0X04
MOVFF _target_low,TBLPTRL // contains 0X00
//DIAGNOSTICS!
MOVF TBLPTRU,W
rcall hexout //CHECKING CONTENTS OF TBLPTRU IN HEX NOTATION ON RS232 - OK
MOVF TBLPTRH,W
rcall hexout //CHECKING CONTENTS OF TBLPTRUH IN HEX NOTATION ON RS232 - OK
MOVF _TBLPTRL,W
rcall hexout //CHECKING CONTENTS OF TBLPTRL IN HEX NOTATION ON RS232 - OK
rcall crlf //ASCII CR+LF ON RS232 FOR BETTER VISIBILITY
//END DIAGS
MOVLW flash_wr_block //defined as 0X40
MOVWF _flash_wr_counter
MOVFF pointer_to_datbuf,FSR0L // FRSR0 point to buffer
MOVFF &pointer_to_datbuf+1,FSR0H // surprising, but works :-)
//DIAGS AGAIN
MOVFF FSR0H,WREG
rcall hexout //CHECKING FSR0H IN HEX NOTATION ON RS232 - 0X00, in my case, OK
movff FSR0L,WREG
rcall hexout //CHECKING FSR0L IN HEX NOTATION ON RS232 - 0XBA, in my case, OK
rcall crlf
//END DIAGS
WRITE_BYTE_TO_HREGS:
MOVFF POSTINC0, WREG // transfer buffer data to internal holding registers
MOVWF TABLAT // present data to table latch
rcall hexout //CHECKING BUFFER - the sequence is the same as I expect
TBLWT+* // write data, perform a short write
// to internal TBLWT holding register.
DECFSZ _flash_wr_counter //loop until buffers are full
BRA WRITE_BYTE_TO_HREGS
BSF EECON1, bit_WREN ; enable write to memory
BCF INTCON, bit_GIE ; disable interrupts
MOVLW 0x55
MOVWF EECON2 // write 55h
MOVLW 0xAA
MOVWF EECON2 // write 0AAh
BSF EECON1, bit_WR ; start program (CPU stall)
BCF EECON1, bit_WREN ; disable write to memory
NOP
return
|
_________________ The computer helps solving the problems which would have never arised without computers
Last edited by tojape on Thu May 24, 2007 2:30 pm; edited 2 times in total |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
|
tojape
Joined: 14 Jan 2007 Posts: 20 Location: Hungary
|
|
Posted: Thu May 24, 2007 1:28 pm |
|
|
As you can see, the comments are '//' , not ';'
Unfortunately, write_flash() doesn't work with 18F45J10, as it was discussed in this forum some two months ago. That's why I have to implement the function in assembly. instead of C.
By the way, has anyone written a proggy that writes to its own flash memory using pic18f45j10? A working code is highly welcome!
The idea to pop up my problem in Microchip's forum is brillant, thank you :-) _________________ The computer helps solving the problems which would have never arised without computers |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Thu May 24, 2007 1:40 pm |
|
|
You are correct I missed the //
According to the version info page, problems with flash reading and writing were fixed in 4.026.
What version CCS are you using? |
|
|
tojape
Joined: 14 Jan 2007 Posts: 20 Location: Hungary
|
|
Posted: Thu May 24, 2007 1:56 pm |
|
|
V4.021 :-( _________________ The computer helps solving the problems which would have never arised without computers |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Thu May 24, 2007 2:00 pm |
|
|
There have been a lot of bugs fixed since 4.021
Send an email to CCS and explain your problem and they may let you download the latest version. |
|
|
tojape
Joined: 14 Jan 2007 Posts: 20 Location: Hungary
|
|
Posted: Fri May 25, 2007 2:33 am |
|
|
To everyone who is interested in this problem:
Even the most recent datasheet of PIC18F45J10 contains the mistake in the example program of programming flash memory. Here is the exact copy of the datasheet:
Code: |
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
ERASE_BLOCK:
BSF EECON1, WREN ; enable write to memory
BSF EECON1, FREE ; enable Row Erase operation
BCF INTCON, GIE ; disable interrupts
MOVLW 55h
MOVWF EECON2 ; write 55h
MOVLW 0AAh
MOVWF EECON2 ; write 0AAh
BSF EECON1, WR ; start erase (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
MOVLW D'16'
MOVWF WRITE_COUNTER ; Need to write 16 blocks of 64 to write
; one erase block of 1024
RESTART_BUFFER:
MOVLW D'64'
MOVWF COUNTER
MOVLW BUFFER_ADDR_HIGH ; point to buffer
MOVWF FSR0H
MOVLW BUFFER_ADDR_LOW
MOVWF FSR0L
FILL_BUFFER
... ; read the new data from I2C, SPI,
; PSP, USART, etc.
WRITE_BUFFER:
MOVLW D’64 ; number of bytes in holding register
MOVWF COUNTER
WRITE_BYTE_TO_HREGS
MOVFF POSTINC0, WREG ; 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, WREN ; enable write to memory
BCF INTCON, GIE ; disable interrupts
MOVLW 55h
;Required sequence
MOVWF EECON2 ; write 55h
MOVLW 0AAh
MOVWF EECON2 ; write 0AAh
BSF EECON1, WR ; start program (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
BCF EECON1, WREN ; disable write to memory
DECFSZ WRITE_COUNTER ; done with one write cycle
BRA RESTART_BUFFER ; if not done replacing the erase block |
[/code]
Finally I figured out how to write corectly (Thank to MFER a lot!)
1. They, at Microchip, used TBLWT+* instead of TBLWT*+
2. The code that loads target address in TBLPTR must be placed AFTER loading the internal table register.
The correct code is:
Code: |
...
WRITE_BUFFER:
MOVLW D’64 ; number of bytes in holding register
MOVWF COUNTER
WRITE_BYTE_TO_HREGS
MOVFF POSTINC0, WREG ; get low byte of buffer data
MOVWF TABLAT ; present data to table latch
TBLWT*+ ; Postincrement needed!
DECFSZ COUNTER ; loop until buffers are full
BRA WRITE_WORD_TO_HREGS
PROGRAM_MEMORY:
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, WREN ; enable write to memory
BCF INTCON, GIE ; disable interrupts
MOVLW 55h
;Required sequence
MOVWF EECON2 ; write 55h
MOVLW 0AAh
MOVWF EECON2 ; write 0AAh
BSF EECON1, WR ; start program (CPU stall)
BSF INTCON, GIE ; re-enable interrupts
BCF EECON1, WREN ; disable write to memory
DECFSZ WRITE_COUNTER ; done with one write cycle
BRA RESTART_BUFFER ; if not done replacing the erase block
|
What the heck can a programmer rely on if not on the datasheet! _________________ The computer helps solving the problems which would have never arised without computers |
|
|
Guest
|
Complete ASM code to write to P18F45J10? |
Posted: Mon Jun 18, 2007 2:46 pm |
|
|
Help... I've been struggling for weeks to write to Flash memory in the P18F45J10. I tried compiling the above assembly code in the Microchip MPASM compiler but was unable to get it to compile. Can someone post or send me working code to write to Flash memory? My email is: amoore@okeefe-electronics.com Thanks!
Andrew |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|