Hi,
I tried to writing to Flash memory of PIC16F877A device. But, tried all the ways. Failed again and again. I know how to write the EEPROM locations. But, writing to Flash memory is not working.
The link of the datasheet I am referring is given below :
http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf
In the "Writing to Flash Program Memory" section in the above datasheet I have seen that "Flash program memory may only be written to if the destination address is in a segment of memory that is not write-protected, as defined in bits WRT1:WRT0 of the device configuration word (Register 14-1)."
What does this mean? How can I configure these settings in the program using the _CONFIG Statement? I have configured like this. Please check, whether this is correct or not.
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
I have followed the sample program given in the above datasheet. Page #40.
I have placed the registers in these locations :
cblock 0x70
ADDRH
ADDRL
DATAADDR
endc
I am giving some parts of my program. The register inizializing sections....
Actually I want to start writing from location 0x300. The data which has to be written in to this location is assigned in variables as given below :
cblock 0x30
Data1
Data2
Data3
Data4
Data5
Data6
Data7
Data8
endc
These variables are assigned with value 0xFF. So, from location 0x300 it will write with this value.
movlw 0xFF
movwf Data1
movwf Data2
movwf Data3
movwf Data4
movwf Data5
movwf Data6
movwf Data7
movwf Data8
*******************************************************************************************************************************************
banksel ADDRH
movlw 0x03
movwf ADDRH
movlw 0x00
movwf ADDRL
movlw 0x30
movwf DATAADDR
BSF STATUS,RP1 ;
BCF STATUS,RP0 ; Bank 2
MOVF ADDRH,W ; Load initial address
MOVWF EEADRH ;
MOVF ADDRL,W ;
MOVWF EEADR ;
MOVF DATAADDR,W ; Load initial data address
MOVWF FSR ;
LOOP
MOVF INDF,W ; Load first data byte into lower
MOVWF EEDATA ;
INCF FSR,F ; Next byte
MOVF INDF,W ; Load second data byte into upper
MOVWF EEDATH ;
INCF FSR,F ;
BSF STATUS,RP0 ; Bank 3
BSF EECON1,EEPGD ; Point to program memory
BSF EECON1,WREN ; Enable writes
; BCF INTCON,GIE ; Disable interrupts (if using)
MOVLW 55h ; Start of required write sequence:
MOVWF EECON2 ; Write 55h
MOVLW 0AAh ;
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit to begin write
NOP ; Any instructions here are ignored as processor
; halts to begin write sequence
NOP ; processor will stop here and wait for write complete
; after write processor continues with 3rd instruction
BCF EECON1,WREN ; Disable writes
; BSF INTCON,GIE ; Enable interrupts (if using)
BCF STATUS,RP0 ; Bank 2
INCF EEADR,F ; Increment address
MOVF EEADR,W ; Check if lower two bits of address are 00
ANDLW 0x03 ; Indicates when four words have been programmed
XORLW 0x03 ;
BTFSC STATUS,Z ; Exit if more than four words,
GOTO LOOP ; Continue if less than four words
*******************************************************************************************************************************************
In the above program, I am not using any interrupts so I have disabled that in the subroutine. Rest all I exactly followed as given in the datasheet. But, nothing is working. After executing the program, I have checked the microcontroller using the Programmer. And I can see that, the program location starting from 300 does not have any changes happenend. What Is the mistake I have made? Please help me.
Also, I tried placing the below code:
TH
btfsc EECON1, WR
goto TH
I have placed in below the two "nop instructions to check the completion of the write operation. assuming that once the write operation is completed the "WR" bit becomes low.
But, any how data is not getting programmed to the location.
If any one could help me, I will be really thankfull....[]
Regards,
Amjad A.R.
[]
I tried to writing to Flash memory of PIC16F877A device. But, tried all the ways. Failed again and again. I know how to write the EEPROM locations. But, writing to Flash memory is not working.
The link of the datasheet I am referring is given below :
http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf
In the "Writing to Flash Program Memory" section in the above datasheet I have seen that "Flash program memory may only be written to if the destination address is in a segment of memory that is not write-protected, as defined in bits WRT1:WRT0 of the device configuration word (Register 14-1)."
What does this mean? How can I configure these settings in the program using the _CONFIG Statement? I have configured like this. Please check, whether this is correct or not.
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
I have followed the sample program given in the above datasheet. Page #40.
I have placed the registers in these locations :
cblock 0x70
ADDRH
ADDRL
DATAADDR
endc
I am giving some parts of my program. The register inizializing sections....
Actually I want to start writing from location 0x300. The data which has to be written in to this location is assigned in variables as given below :
cblock 0x30
Data1
Data2
Data3
Data4
Data5
Data6
Data7
Data8
endc
These variables are assigned with value 0xFF. So, from location 0x300 it will write with this value.
movlw 0xFF
movwf Data1
movwf Data2
movwf Data3
movwf Data4
movwf Data5
movwf Data6
movwf Data7
movwf Data8
*******************************************************************************************************************************************
banksel ADDRH
movlw 0x03
movwf ADDRH
movlw 0x00
movwf ADDRL
movlw 0x30
movwf DATAADDR
BSF STATUS,RP1 ;
BCF STATUS,RP0 ; Bank 2
MOVF ADDRH,W ; Load initial address
MOVWF EEADRH ;
MOVF ADDRL,W ;
MOVWF EEADR ;
MOVF DATAADDR,W ; Load initial data address
MOVWF FSR ;
LOOP
MOVF INDF,W ; Load first data byte into lower
MOVWF EEDATA ;
INCF FSR,F ; Next byte
MOVF INDF,W ; Load second data byte into upper
MOVWF EEDATH ;
INCF FSR,F ;
BSF STATUS,RP0 ; Bank 3
BSF EECON1,EEPGD ; Point to program memory
BSF EECON1,WREN ; Enable writes
; BCF INTCON,GIE ; Disable interrupts (if using)
MOVLW 55h ; Start of required write sequence:
MOVWF EECON2 ; Write 55h
MOVLW 0AAh ;
MOVWF EECON2 ; Write AAh
BSF EECON1,WR ; Set WR bit to begin write
NOP ; Any instructions here are ignored as processor
; halts to begin write sequence
NOP ; processor will stop here and wait for write complete
; after write processor continues with 3rd instruction
BCF EECON1,WREN ; Disable writes
; BSF INTCON,GIE ; Enable interrupts (if using)
BCF STATUS,RP0 ; Bank 2
INCF EEADR,F ; Increment address
MOVF EEADR,W ; Check if lower two bits of address are 00
ANDLW 0x03 ; Indicates when four words have been programmed
XORLW 0x03 ;
BTFSC STATUS,Z ; Exit if more than four words,
GOTO LOOP ; Continue if less than four words
*******************************************************************************************************************************************
In the above program, I am not using any interrupts so I have disabled that in the subroutine. Rest all I exactly followed as given in the datasheet. But, nothing is working. After executing the program, I have checked the microcontroller using the Programmer. And I can see that, the program location starting from 300 does not have any changes happenend. What Is the mistake I have made? Please help me.
Also, I tried placing the below code:
TH
btfsc EECON1, WR
goto TH
I have placed in below the two "nop instructions to check the completion of the write operation. assuming that once the write operation is completed the "WR" bit becomes low.
But, any how data is not getting programmed to the location.
If any one could help me, I will be really thankfull....[]
Regards,
Amjad A.R.
[]