Jump to content
milea

Flash_Write Example For Pic18F - Help

Recommended Posts

milea    0

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

Share this post


Link to post
Share on other sites
trossin    0

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

 

http://www.tedrossin.net46.net/Electronics/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

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×