Jump to content
Sign in to follow this  
or.han

Eeprom Writes Just Once

Recommended Posts

Hello. I have six values which I want to save in the EEPROM-memory but I can't just write to the EEPROM once. My code for writing the first value:

 

			BSF			status,6; RP1 = 1
		BCF			status,5; RP0 = 0
							; BANK = 01 = 2
		MOVLW		d'0'
		MOVWF		eeadr
		MOVLW		d'5'
		MOVWF		eedata
		BSF			status,5; RP0 = 1 : BANK = 11 = 3
		BCF			eecon1,7; bit 7 = EEPGD
		BSF			eecon1,2; bit 2 = WREN
		MOVLW		h'55'
		MOVWF		eecon2
		MOVLW		h'aa'
		MOVWF		eecon2
		BSF			eecon1,1; bit 1 = WR
		BCF			eecon1,2
		BCF			status,5; RP0 = 0
		BCF			status,6; RP1 = 0
		BTFSS		pir2,4	; BANK = 00 = 0
		GOTO		$-1
		BCF			pir2,4

 

As you can see I write d'5' to d'0' in the EEPROM. If I have the exactly same code, but of course with other values and adresses, right after the first, then the program just stops. When checking the EEPROM, I see that all the values have correctly been written to the different adresses... Why does the program just stop??

Share this post


Link to post
Share on other sites
As you can see I write d'5' to d'0' in the EEPROM. If I have the exactly same code, but of course with other values and adresses, right after the first, then the program just stops. When checking the EEPROM, I see that all the values have correctly been written to the different adresses... Why does the program just stop??

 

Did you remember to disable interrupts ? This turns out to be VERY important when writing to internal EE.

 

Anyway, here's a chunk of handwritten MPASM assembly code that works on a PIC16F873. Pretty identical to yours (and to Microchip's datasheet EE example), I'd say, apart from the interrupt issue.

 

03CA   303C           00325             MOVLW   MyVar
03CB   0084           00326             MOVWF   FSR
03CC   3008           00327             MOVLW   8
03CD   00B2           00328             MOVWF   LoopCnt
                      00329 
03CE   3002           00330             MOVLW   2
03CF   1283 1703      00331             BANKSEL EEADR
03D1   008D           00332             MOVWF   EEADR
                      00333 
03D2   1283 1303      00334             BANKSEL INTCON
03D4   138B           00335             BCF     INTCON, GIE
                      00336 
03D5                  00337 STORE_LOOP
03D5   1683 1703      00338             BANKSEL EECON1
03D7   138C           00339             BCF     EECON1, EEPGD
03D8   150C           00340             BSF     EECON1, WREN
                      00341 
03D9   0800           00342             MOVF    INDF, W
03DA   1283 1703      00343             BANKSEL EEDATA
03DC   008C           00344             MOVWF   EEDATA
                      00345 
03DD   1683 1703      00346             BANKSEL EECON2
03DF   3055           00347             MOVLW   0X55
03E0   008D           00348             MOVWF   EECON2
03E1   30AA           00349             MOVLW   0XAA
03E2   008D           00350             MOVWF   EECON2
03E3   148C           00351             BSF     EECON1, WR
                      00352 
03E4   1283 1303      00353             BANKSEL PIR2
03E6                  00354 STORE_INNER
03E6   1E0D           00355             BTFSS   PIR2, EEIF
03E7   2BE6           00356             GOTO    STORE_INNER
                      00357 
03E8   120D           00358             BCF     PIR2, EEIF
                      00359 
03E9   1283 1703      00360             BANKSEL EEADR
03EB   0A8D           00361             INCF    EEADR, F
                      00362 
03EC   0A84           00363             INCF    FSR, F
03ED   1283 1303      00364             BANKSEL LoopCnt
03EF   0BB2           00365             DECFSZ  LoopCnt, F
03F0   2BD5           00366             GOTO    STORE_LOOP
                      00367 
03F1   1283 1303      00368             BANKSEL INTCON
03F3   178B           00369             BSF     INTCON, GIE
                      00370 
03F4   1683 1703      00371             BANKSEL EECON1
03F6   110C           00372             BCF     EECON1, WREN

 

Of course, if the watchdog is enabled, you really ought to hit it in the tight blocking loop referred to the label STORE_INNER.

 

Style note: just too many BANKSEL directives for my tastes, and the loop slows things down unnecessarily for such a low number of values.

Edited by DL1968

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...
Sign in to follow this  

×