flash program memory internal write problem

Discussion in 'Embedded Systems and Microcontrollers' started by dreamcatcher, Aug 29, 2010.

  1. dreamcatcher

    Thread Starter New Member

    Jan 5, 2010
    27
    0
    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
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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>
     
  4. dreamcatcher

    Thread Starter New Member

    Jan 5, 2010
    27
    0
    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
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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?
     
  6. dreamcatcher

    Thread Starter New Member

    Jan 5, 2010
    27
    0
    nothing has changed
     
  7. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Code ( (Unknown Language)):
    1.  include<p16f877.inc>
    2.   org 00
    3.     cblock 0x20
    4.      ADDR ;use MPLAB to change the
    5.   VALUE;memory content for these two values
    6.     endc
    7. START:
    8.    banksel ADDR
    9.   MOVF ADDR, W ;Address to
    10.   banksel EEADR
    11.   MOVWF EEADR ;write to
    12.   banksel VALUE
    13.   MOVF VALUE, W ;Data to
    14.   banksel EEDATA
    15.   MOVWF EEDATA ;write
    16.   banksel EECON1
    17.   BCF EECON1, EEPGD ;Point to Data memory
    18.   BSF EECON1, WREN ;Enable writes
    19.   ;Only disable interrupts
    20.   BCF INTCON, GIE ;if already enabled,
    21.   ;otherwise discard
    22.   MOVLW 0x55 ;Write 55h to
    23.   MOVWF EECON2 ;EECON2
    24.   MOVLW 0xAA ;Write AAh to
    25.   MOVWF EECON2 ;EECON2
    26.   BSF EECON1, WR ;Start write operation
    27.   ;Only enable interrupts
    28.   BSF INTCON, GIE ;if using interrupts,
    29.   ;otherwise discard
    30.   BCF EECON1, WREN ;Disable writes
    31.   banksel EECON1
    32.   BTFSC EECON1, WR ;Wait for
    33.      GOTO $-1 ;write to finish
    34.         GOTO START
    35.   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.
    Code ( (Unknown Language)):
    1. BTFSC EECON1, WR ;Wait for
    2. GOTO $-1 ;write to finish
    Hope this help
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
    Code ( (Unknown Language)):
    1.  banksel EECON1
    2.   BTFSC EECON1, WR ;Wait for
    3.      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
     
Loading...