CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18f45J10 flash programming problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tojape



Joined: 14 Jan 2007
Posts: 20
Location: Hungary

View user's profile Send private message

PIC18f45J10 flash programming problem
PostPosted: Thu May 24, 2007 12:35 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:15 pm     Reply with quote

What compiler are you using CCS or MPASM?

If you are using MPASM your question would get better response on the MPASM forum.

http://forum.microchip.com/tt.aspx?forumid=16
tojape



Joined: 14 Jan 2007
Posts: 20
Location: Hungary

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 24, 2007 1:56 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 24, 2007 2:00 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri May 25, 2007 2:33 am     Reply with quote

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?
PostPosted: Mon Jun 18, 2007 2:46 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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