Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC12F629EEPROM problem help please

Status
Not open for further replies.

nicholastyc

New Member
Hi,

By the way, my new problem is i saved my data (0x01) into eeprom address 0x01, when i recall the data it does not recall the same data but something different from it and sometimes i can save the data and recall correctly but sometimes not.
What is the problem? is the setting or code problems? i already DOUBLExDouble check it in the DATASHEET for few hours and look for solutions on internet, i still cant recall correctly, PLEASe HELP.I write the data correctly...everytime, when i check in the EEDATA register b4 i reset the MPLAB SIM and rerun again.
Code Below, PIC12F629 ,
WRITE code from me,
ORIGINAL_PWM EQU 0x21
TEMP EQU 0x22
WRITE: BSF STATUS,5; switch to bank 1
MOVLW 0x01; go to addr 0x01
MOVWF EEADR;
MOVF ORIGINAL_PWM,w ; (ORIGINAL_PWM = 0x01)
MOVWF EEDATA; write 0x01 to EEDATA
BSF EECON1,WREN; < follow the datasheet write process
BCF INTCON,GIE ;<
MOVLW H'0055' ;<
MOVWF EECON2 ;<
MOVLW H'00AA' ;<
MOVWF EECON2 ;<
BSF EECON1,WR ;<
BSF INTCON,GIE ;<
BCF STATUS,5; switch to bank 0
RETURN

READ: BSF STATUS,5; switch to bank 1
MOVLW 0x01; goto addr 0x01 in eeprom
MOVWF EEADR;
BSF EECON1,RD ; read previously saved data 0x01
MOVF EEDATA,w ; move 0x01 to w
MOVWF TEMP; (TEMP register ) move 0x01 to TEMP
BCF STATUS,5; switch bank 0
RETURN


First, Thanks a lot to view and helps.
 
***********************************************
#include <p12f629.inc>
__CONFIG _CP_OFF & _MCLRE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _CPD_OFF &_PWRTE_OFF

ORIGINAL_PWM EQU H'0025'
TEMP EQU H'0022'

ORG 0x000
BCF STATUS,RP0;
CLRF GPIO;
MOVLW 07h ;
MOVWF CMCON ;
BSF STATUS,RP0 ;
CALL 3FFh;
MOVWF OSCCAL;
MOVLW H'0038';
MOVWF TRISIO;
BCF STATUS,5;

START
BTFSS GPIO,3; wait for ON/OFF
GOTO START;
CALL READ ; Restore previous PWM
RESTART_PWM
MOVWF ORIGINAL_PWM; save EEDATA to register, for PWM cycle
MOVWF TEMP; save to temporary counter
BSF GPIO,0; signal to output 0, high cycle
BSF GPIO,1; signal to output 1,high cycle
BSF GPIO,2; signal to output 2,high cycle
LOOP1
DECFSZ TEMP,1 ; PWM high cycle counter
GOTO LOOP1 ;
MOVLW d'255' ; 255 cycles
MOVWF TEMP ; save for counter
MOVF ORIGINAL_PWM,w ;
SUBWF TEMP,1 ;
BCF GPIO,0 ;low cycle
BCF GPIO,1 ;low cycle
BCF GPIO,2 ;low cycle
LOOP2
DECFSZ TEMP,1 ; PWM low cycle counter
GOTO LOOP2 ;
BTFSS GPIO,3 ; check for on/off switch
GOTO START ;
BTFSC GPIO,4 ;check for PWM cycle increase
CALL INCREASE ;increase PWM cycle
BTFSC GPIO,5 ; check for PWM Cycle reduce
CALL DECREASE ;decrease PWM cycle
GOTO RESTART_PWM; repeat all hi/lo pwm cycle

***********SUBROUTINE*************
READ:
BSF STATUS,5; switch to bank 1
MOVLW 0x01; goto addr 0x01 in eeprom
MOVWF EEADR;
BSF EECON1,RD ; read previously saved PWM cycle
MOVF EEDATA,w ; move saved data to w
BCF STATUS,5; switch bank 0
RETURN

WRITE:
BSF STATUS,5; switch to bank 1
MOVLW 0x01; go to addr 0x01
MOVWF EEADR;
MOVF ORIGINAL_PWM,w ; move new cycle to ORIGINAL_PWM
MOVWF EEDATA; write 0x01 to EEDATA
BSF EECON1,WREN; < follow the datasheet write process
BCF INTCON,GIE ;<
MOVLW H'0055' ;<
MOVWF EECON2 ;<
MOVLW H'00AA' ;<
MOVWF EECON2 ;<
BSF EECON1,WR ;<
BSF INTCON,GIE ;<
BCF STATUS,5; switch to bank 0
RETURN

DECREASE:
DECF ORIGINAL_PWM,0 ; decrease cycle by 1
MOVWF ORIGINAL_PWM; save to register
CALL WRITE;
GOTO RESTART_PWM ;
RETURN
INCREASE:
INCF ORIGINAL_PWM,0 ; increase cycle by 1
MOVWF ORIGINAL_PWM; save to register
CALL WRITE;
GOTO RESTART_PWM ;
RETURN

END

**************************************************
Above is my code...for normal PWM control LED dimmer.
My main problem is i cant restore my saved PWM when i start up again.
I simulated in MPLAB SIM ver 8 , the old PWM keep called. I want my saved PWM to be recalled when new start. I monitor each register, the EEDATA did save the PWM newly set, but when recall again at new start up, it will be the old 1. For example, my new PWM cycle is 0x05 and old PWM is 0x02 , after 0x05 was saved. then i send input to GPIO,3 to turn off the PWM and send a new input to turn on again, but it restored the old PWM data which is 0x02.
Where is the problem? is it the config? or??? thanks
 
Above is my code...
Here's a nice tip. :) When you post code here, be sure to put it inside Code tags. Easiest way is to click the # in the menu just before pasting your code. Alternately, you can type in
Code:
 before your code and [/co[COLOR="Black"]de][/COLOR] after your code. This forces the web-site to keep your formatting so people can actually read your code. When you don't do this, it loses all indentation and turns into the unreadable lump in your original post.

I took the liberty of neatening up your code, adding a proper cblock for the variables and changing the bsf/bcf bank changes to banksel's (and removed one unnecessary bank change). I also changed most of it to what is, in my opinion, much easier to read lower case. All upper case makes my eyes go wonky when I code. Everything looks the same. :p
[CODE]	#include <p12f629.inc>
	__CONFIG _CP_OFF & _MCLRE_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _CPD_OFF &_PWRTE_OFF

	cblock	0x20
	original_pwm,temp
	endc

	org	0x000
	clrf	GPIO
	movlw	07h
	movwf	CMCON
	banksel	OSCCAL		;bank 1
	call	3FFh
	movwf	OSCCAL
	movlw	H'0038'
	movwf	TRISIO
	banksel	GPIO		;bank 0

start	btfss	GPIO,3		;wait for ON/OFF
	goto	start
	call	read		;Restore previous PWM
restart_pwm
	movwf	original_pwm	;save EEDATA to register, for PWM cycle
	movwf	temp		;save to temporary counter
	bsf	GPIO,0		;signal to output 0, high cycle
	bsf	GPIO,1		;signal to output 1,high cycle
	bsf	GPIO,2		;signal to output 2,high cycle
loop1	decfsz	temp,1		;PWM high cycle counter
	goto	loop1
	movlw	d'255'		;255 cycles
	movwf	temp		;save for counter
	movf	original_pwm,w
	subwf	temp,1
	bcf	GPIO,0		;low cycle
	bcf	GPIO,1		;low cycle
	bcf	GPIO,2		;low cycle
loop2	decfsz	temp,1		;PWM low cycle counter
	goto	loop2
	btfss	GPIO,3		;check for on/off switch
	goto	start
	btfsc	GPIO,4		;check for PWM cycle increase
	call	increase	;increase PWM cycle
	btfsc	GPIO,5		;check for PWM Cycle reduce
	call	decrease	;decrease PWM cycle
	goto	restart_pwm	;repeat all hi/lo pwm cycle

***********SUBROUTINE*************
read	banksel	EEADR		;bank 1
	movlw	0x01		;goto addr 0x01 in eeprom
	movwf	EEADR
	bsf	EECON1,RD	;read previously saved PWM cycle
	movf	EEDATA,w	;move saved data to w
	banksel	GPIO		;bank 0
	return

write	banksel	EEADR		;bank 1
	movlw	0x01		;go to addr 0x01
	movwf	EEADR
	movf	original_pwm,w	;move new cycle to original_pwm
	movwf	EEDATA		;write 0x01 to EEDATA
	bsf	EECON1,WREN	;< follow the datasheet write process
	bcf	INTCON,GIE	;<
	movlw	H'0055'		;<
	movwf	EECON2		;<
	movlw	H'00AA'		;<
	movwf	EECON2		;<
	bsf	EECON1,WR	;<
	bsf	INTCON,GIE	;<
	banksel	GPIO		;bank 0
	return

decrease
	decf	original_pwm,0	;decrease cycle by 1
	movwf	original_pwm	;save to register
	call	write
	goto	restart_pwm
	return
increase
	incf	original_pwm,0	;increase cycle by 1
	movwf	original_pwm	;save to register
	call	write
	goto	restart_pwm
	return

	END
 
Last edited:
Here's another nice tip. :) In commands that give you the option of storing the result in either W or F, like these, for instance:
Code:
decrease
	decf	original_pwm,0	;decrease cycle by 1
Code:
increase
	incf	original_pwm,0	;increase cycle by 1
use W or F instead of 0 and 1. The include file has those equates defined properly so you don't have to do it. Upper or lower case both work.

For example:
Code:
decrease
	decf	original_pwm,w	;decrease cycle by 1
Code:
increase
	incf	original_pwm,w	;increase cycle by 1

-------------------

Another thing I just noticed is this. In decrease and increase you're using decf to decrement the variable and storing the result in W (0 = W). Then you save W back to the variable.
Code:
decrease
	decf	original_pwm,0	;[COLOR="Red"]or the equivalent,[/COLOR] [COLOR="DarkOrange"]original_pwm,w[/COLOR]
	movwf	original_pwm	;save to register
That's silly! :p Just save the result to F instead and leave out the second line, like this:
Code:
decrease
	decf	original_pwm,f	;decrease cycle by 1
 
Last edited:
hi, thanks lots...

btw, can i know wat is the problem that cause my program did not read what it should be in MPLAM SIM?

is it just because of my code repeatedly saved?

...still wondering what is the causes ... your help is very much appreaciated.

thank you again..

i am still newbie to programming PIC...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top