Jump to content


Photo

Flash_Write Example For Pic18F - Help


2 replies to this topic

#1 milea

milea

    Newbrie

  • Members
  • 2 posts

Posted 09 April 2014 - 09:13 PM

Pls some example for write into the flash memory for pic18f, especually for pic18f4550. Flash_read work but Flash_write with no success even I try it lot of diffrent ways.

Thanks



#2 trossin

trossin

    Super Enthusiast

  • Moderator
  • PipPipPip
  • 243 posts
  • Gender:Male
  • Location:Colorado

Posted 10 April 2014 - 03:54 PM

Below is assembly code that I use in my boot loader for 18F parts. The full project can be found here:

http://www.tedrossin...cs/Pic/Pic.html

Scroll down and click on Serial Port PIC Boot Programmer.

I would think you could convert this to C code without much work but the problem is determining what are valid blocks to write.

I hope this helps.
 
; ************************************************************************
; ** CmdEraseFlashRow: Get High address then low address and erase the	**
; **                   row of 64 bytes.  Bits 6:0 of the address are 	**
; **		       ignored.  0 is returned to signal the operation 	**
; **		       has finished.					**
; ************************************************************************		
CmdEraseFlashRow:
	clrf 	TBLPTRU 		; address of the memory block
	call	GetRS232Byte 	; Fetch high address[15:8]
	movwf 	TBLPTRH
	call	GetRS232Byte 	; Fetch low address[7:6] [5:0] are ignored
	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 	0x55
	movwf	EECON2 ; write 55h
	movlw 	0xaa
	movwf 	EECON2 ; write 0AAh
	bsf 	EECON1, WR ; start erase (CPU stall)
;	bsf 	INTCON, GIE ; re-enable interrupts
	bra		CleanExit

; ************************************************************************
; ** CmdWriteFlashRow: Get High address then low address and 64 bytes   **
; **                   of data and write flash memory.  Bits 6:0 of the **
; ** 		       address are ignored. 0 is returned to signal the **
; **		       operation has finished.				**
; ************************************************************************	
CmdWriteFlashRow:
	clrf 	TBLPTRU ; address of the memory block
	call	GetRS232Byte 	; Fetch high address[15:8]
	movwf 	TBLPTRH
	call	GetRS232Byte 	; Fetch low address some bit should not be set
	movwf 	TBLPTRL
	
		; Write data to internal flash buffer
	movlw	WRITE_BLOCK_SIZE
	movwf	Tmp
CmdWriteFlashRowLoop:		
	call	GetRS232Byte	; Get data to write
	movwf 	TABLAT 			; present data to table latch
	TBLWT*+ 				; write data, perform a short write
							; to internal TBLWT holding register.	
	decfsz	Tmp
	bra		CmdWriteFlashRowLoop
	
	TBLRD*-					; Point back to original row
	
		; Write internal buffer to flash 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
	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		
	bra		CleanExi
P.S. It only took 5 tries to get this to post correctly!!!

Edited by trossin, 10 April 2014 - 04:05 PM.


#3 milea

milea

    Newbrie

  • Members
  • 2 posts

Posted 11 April 2014 - 07:51 AM

Thank you Ted, it helps me to solve flash_write.

Kind regards





Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users