flash program memory internal write problem

Thread Starter

dreamcatcher

Joined Jan 5, 2010
27
Hi,

I've written he following program to test the writting in the falsh program memory internal write in 16f877.
I am supposed to have first porta = 0x03, then it will be 0xFF
I simulated it with MPLAB SIM and it is not working.
Please would you show me the error that i've done.
Thank you

List p=16f877
include<p16f877.inc>
__CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_ON & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC

errorlevel-302



ADDRL EQU 0X20
ADDRH EQU 0X21
VALUEL EQU 0X22
VALUEH EQU 0X23

;;;;;;;;;;;;;**********PORT CONFIG***********;;;;;;;;;;;;;;;;;;;;;;;

ORG 0X0000


BANK0 macro
bcf STATUS,RP0
BCF STATUS,RP1
endm
BANK1 macro
BSF STATUS,RP0
BCF STATUS,RP1
endm

;;;;;;;;;;;;;;;;;****************PORT OUTPUT CONFIG**************;;;;;;;;;;;

BANKSEL OPTION_REG
BCF OPTION_REG,5 ; SET THE PIN RA4 AS Internal instruction cycle clock (CLKOUT)

BANK1

MOVLW B'00000111'
MOVWF ADCON1 ; SET ALL THE PINS TO DEGITALS INPUT/OUTPUT

MOVLW 0xff
MOVWF TRISB ;
movwf TRISD
movwf TRISC
MOVWF TRISE

MOVLW 0x00 ;
MOVWF TRISA


BANK0

GOTO TEST



TEST ORG 0X0020

MOVLW b'00000011'
MOVWF PORTA



IN MOVLW 0x22
MOVWF ADDRL


CLRF ADDRH

MOVLW 0XFF
MOVWF VALUEL

MOVLW 30
MOVWF VALUEH

BSF STATUS, RP1 ;
BCF STATUS, RP0 ;Bank 2
MOVF ADDRL, W ;Write address
MOVWF EEADR ;of desired
MOVF ADDRH, W ;program memory
MOVWF EEADRH ;location
MOVF VALUEL, W ;Write value to
MOVWF EEDATA ;program at
MOVF VALUEH, W ;desired memory
MOVWF EEDATH ;location
BSF STATUS, RP0 ;Bank 3
BSF EECON1, EEPGD ;Point to Program memory
BSF EECON1, WREN ;Enable writes
;Only disable interrupts
MOVLW 0x55 ;Write 55h to
MOVWF EECON2 ;EECON2
MOVLW 0xAA ;Write AAh to
MOVWF EECON2 ;EECON2
BSF EECON1, WR ;Start write operation
NOP ;Two NOPs to allow micro
NOP ;to setup for write
;Only enable interrupts
BCF EECON1, WREN ;Disable writes

GOTO TEST



END
 

t06afre

Joined May 11, 2009
5,934
Have you use copy and past from the code example in the data sheet? That code should be correct. I am not sitting on a MPLAB machine now. But I think I had this problem once. Then using the MPLAB SIM tool, do not run at max speed. In the MPLAB toolbar select Debugger->setting and the select the OSC/TRace tab. Set your CPU speed to 4MHz. Even if you just are single stepping in the internal MPLAB simulator. Somewhat strange:confused: but it worked for me
 

Markd77

Joined Sep 7, 2009
2,806
I think I remember this one. The example in the datasheet is dodgy. Because it keeps changing banks you either have to modify it or move the variables to ram which is accessable from all banks. eg:
ADDRL EQU 0X70
ADDRH EQU 0X71
VALUEL EQU 0X72
VALUEH EQU 0X73
but you only get 16 of these special registers so it might be worth modifying the code to keep them free for other things.
<ed> Also you might notice that it doesen't change back to bank 0 at the end of the example, which is also true for other code snippets in the dataheets </ed>
 

Thread Starter

dreamcatcher

Joined Jan 5, 2010
27
I changed the code ti the following by taking in consideration the bank select, but it still don't work.............


List p=16f877
include<p16f877.inc>
__CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_ON & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC

errorlevel-302



ADDRL EQU 0X130
ADDRH EQU 0X131
VALUEL EQU 0X132
VALUEH EQU 0X133

ORG 0X0000

BANK0 macro
bcf STATUS,RP0
BCF STATUS,RP1
endm

BANK1 macro
BSF STATUS,RP0
BCF STATUS,RP1
endm

BANK2 macro
BSF STATUS, RP1 ;
BCF STATUS, RP0
endm
;;;;;;;;;;;;;;;;;****************PORT OUTPUT CONFIG**************;;;;;;;;;;;

BANKSEL OPTION_REG
BCF OPTION_REG,5 ; SET THE PIN RA4 AS Internal instruction cycle clock (CLKOUT)

BANK1

MOVLW B'00000111'
MOVWF ADCON1 ; SET ALL THE PINS TO DEGITALS INPUT/OUTPUT

MOVLW 0xff
MOVWF TRISB ;
movwf TRISD
movwf TRISC
MOVWF TRISE

MOVLW 0x00 ;
MOVWF TRISA



GOTO TEST

TEST ORG 0X0020
BANK0

MOVLW b'00000011'
MOVWF PORTA

BANK2

MOVLW 0x22
MOVWF ADDRL

CLRF ADDRH

MOVLW 0XFF
MOVWF VALUEL

MOVLW 0x30
MOVWF VALUEH

BSF STATUS, RP1 ;
BCF STATUS, RP0 ;Bank 2
MOVF ADDRL, W ;Write address
MOVWF EEADR ;of desired
MOVF ADDRH, W ;program memory
MOVWF EEADRH ;location
MOVF VALUEL, W ;Write value to
MOVWF EEDATA ;program at
MOVF VALUEH, W ;desired memory
MOVWF EEDATH ;location
BSF STATUS, RP0 ;Bank 3
BSF EECON1, EEPGD ;Point to Program memory
BSF EECON1, WREN ;Enable writes
;Only disable interrupts
MOVLW 0x55 ;Write 55h to
MOVWF EECON2 ;EECON2
MOVLW 0xAA ;Write AAh to
MOVWF EECON2 ;EECON2
BSF EECON1, WR ;Start write operation
NOP ;Two NOPs to allow micro
NOP ;to setup for write
;Only enable interrupts
BCF EECON1, WREN ;Disable writes


GOTO TEST

END
 

Markd77

Joined Sep 7, 2009
2,806
Have you got the program memory window from the view menu open? What happens when you step through the code into and past the NOPs?
 

Markd77

Joined Sep 7, 2009
2,806
I think maybe MPLAB SIM does not support it. I've run the code and everything seems to be fine, but no change.
Also there is no mention of EEPROM program memory in the SIM help file.
Along with core support, MPLAB SIM supports the following peripheral modules, in addition to general purpose I/O. Consult the data sheet for the particular device you are using for information on which symbols are implemented.


Note: Even if peripheral functionality is not supported in the simulator, the peripheral registers will be available as read/write.


The delays and interrupt latency are implemented on all peripherals.
 

t06afre

Joined May 11, 2009
5,934
Rich (BB code):
 include<p16f877.inc>
  org 00
    cblock 0x20
     ADDR ;use MPLAB to change the 
  VALUE;memory content for these two values
    endc
START:
   banksel ADDR
  MOVF ADDR, W ;Address to
  banksel EEADR
  MOVWF EEADR ;write to
  banksel VALUE
  MOVF VALUE, W ;Data to
  banksel EEDATA
  MOVWF EEDATA ;write
  banksel EECON1
  BCF EECON1, EEPGD ;Point to Data memory
  BSF EECON1, WREN ;Enable writes
  ;Only disable interrupts
  BCF INTCON, GIE ;if already enabled,
  ;otherwise discard
  MOVLW 0x55 ;Write 55h to
  MOVWF EECON2 ;EECON2
  MOVLW 0xAA ;Write AAh to
  MOVWF EECON2 ;EECON2
  BSF EECON1, WR ;Start write operation
  ;Only enable interrupts
  BSF INTCON, GIE ;if using interrupts,
  ;otherwise discard
  BCF EECON1, WREN ;Disable writes
  banksel EECON1
  BTFSC EECON1, WR ;Wait for
     GOTO $-1 ;write to finish
        GOTO START
  end
The simulation of the EEPROM works. And I think the simulator is set up to mimic the real world. In the real world writing to the EEPROM is a slow process. You can try to run the code I have made, it is the EEPROM write example from the data sheet, but slightly modified. Two thing to try. Place the cursor on the "GOTO START" line. Then right-click and select "Run To Cursor". Or in the MPLAB toolbar select Debugger->setting and the select the OSC/TRace tab. Set your CPU speed to 100 KHz. Then push the animate button (perhaps set the animation speed to max) .
You will see that the animation will hang on the this segment for a while until the condition is met.
Rich (BB code):
BTFSC EECON1, WR ;Wait for
GOTO $-1 ;write to finish
Hope this help
 

t06afre

Joined May 11, 2009
5,934
I think then writing to the EEPROM in the MPLAB SIM simulator, the timing is calculated by the settings in the Debugger->setting->OSC/TRace tab. In the real world the EEPROM write cycle is independent of the clock speed. So then simulating using MPLAB SIM. A low clock speed it will require fewer clock cycles to finish the write. Also include the code
Rich (BB code):
 banksel EECON1
  BTFSC EECON1, WR ;Wait for
     GOTO $-1 ;write to finish
To ensure the EEPROM is updated 100% at some point. It is perhaps best use the Run to cursor option. Since the settings in the OSC/TRace tab will not have much to say then using this option
 
Top