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.

12f683 eeprom problem

Status
Not open for further replies.

Monchichack

New Member
Hello,

After 5 hours of trying to resolve this issue methodically I have decided to ask for some pointers.

The project.

Simple stepper motor running 1/2 step (8 o/p scenarios) with a change direction function on 1 pin and a faster/slower speed selection on another.

I would like to save my speed, direction and position in eeprom so I do not loose settings during power down.

My problem is that the direction and speed will save and reload fine but the Counter will not...

I have tryed everything I can think of but it seems as soon as the Counter register is copied to eeprom my program crashes. It only happens if my counter save address in eeprom is the same as my counter read address in eeprom is the same. I can have different addresses for each and manually input the start up position which works fine and the save to eeprom function works fine but as soon as I read and write to the same address the program crashes? The part I do not understand is how come it crashes during opperation? It should only read from the eeprom during initialisation and then loop the main program?

The issue I can think of is that for some reason the number being saved is interrupting the Counter register and jumping out of my lookup table but I can not figure out how this could happen?

I can tell you that the point of crash is during the write to eeprom in either the forward or reverse parts of the program (bsf EECON1,WR)

The momory addresses I am using are 2A, 2B & 2C

The 2C address in eeprom is used to count step position and is causing the problems.

I would appreciate any help or advice on this issue.

I know my code is not the best (amature) so I would appreciate any pointers on that as well.



THE OFFENDING CODE:


list p=12F683 ; list directive to define processor
#include <p12F683.inc> ; processor specific variable definitions

errorlevel -302 ; suppress message 302 from list file

__CONFIG _FCMEN_ON & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT


ORG 0x000 ; processor reset vector


;***** VARIABLE DEFINITIONS

Timer1 EQU 0x20
Timer2 EQU 0x21
Timer3 EQU 0x22
Counter EQU 0x23
T1 EQU 0x24
FwdRev EQU 0x25
Flag EQU 0x26



goto main ; go to beginning of program



;************************************************************************************************************************
;Subroutines

Delay movfw T1
movwf Timer2
del_loop1 movfw T1
movwf Timer1
del_loop2 decfsz Timer1,F
goto del_loop2
decfsz Timer2,F
goto del_loop1
retlw 0

DelayFunc movlw D'100'
movwf Timer2
del_loop3 movlw D'100'
movwf Timer1
del_loop4 decfsz Timer1,F
goto del_loop4
decfsz Timer2,F
goto del_loop3
retlw 0


Sequence addwf PCL,f
retlw b'000001' ; 0000 ;bit's 2&3 are to skip GP2&3
retlw b'000011' ; 0001
retlw b'000010' ; 0010
retlw b'010010' ; 0011
retlw b'010000' ; 0100
retlw b'110000' ; 0101
retlw b'100000' ; 0110
retlw b'100001' ; 0111


Faster
decf T1,f
movfw T1
bsf STATUS,RP0
movwf EEDATA
movlw 0x2A
movwf EEADR ;write address is 0
bsf EECON1,WREN ;Enable write
movlw 0x55 ;Unlock write
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;Start the write
bcf STATUS,RP0
retlw 0


Slower
incf T1,f
movfw T1
bsf STATUS,RP0
movwf EEDATA
movlw 0x2A
movwf EEADR ;write to address 0
bsf EECON1,WREN ;Enable write
movlw 0x55 ;Unlock write
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;Start the write
bcf STATUS,RP0
retlw 0



Direction incf FwdRev,f
movfw FwdRev
bsf STATUS,RP0
movwf EEDATA
movlw 0x2B
movwf EEADR ;write to address 1
bsf EECON1,WREN ;Enable write
movlw 0x55 ;Unlock write
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;Start the write
bcf STATUS,RP0
retlw 0




;*******************************************************************************************************

main

bcf STATUS,RP0
clrf GPIO
bsf STATUS,RP0
movlw b'001100' ;GP0,1,4,5 = o/p GP,2 = i/p GP, 3 = Speed SW
movwf TRISIO
clrf ANSEL ;ANSEL and CMCON0 must be configured to allow digital i/p's to be read
bcf STATUS,RP0
movlw B'111'
movwf CMCON0
clrf Counter
clrf FwdRev
movlw B'11111111'
movwf Timer3
bcf Flag,0


bsf STATUS,RP0 ;Restore Speed settings on power up
movlw 0x2A
movwf EEADR ;Address to read
bsf EECON1,RD ;EE Read
movfw EEDATA ;Move data to W
bcf STATUS,RP0
movwf T1

bsf STATUS,RP0
movlw 0x2B ;restore Direction settings on power up
movwf EEADR
bsf EECON1,RD
movfw EEDATA
bcf STATUS,RP0
movwf FwdRev

bsf STATUS,RP0
movlw 0x2C ;restore Counter settings on power up
movwf EEADR
bsf EECON1,RD
movfw EEDATA
bcf STATUS,RP0
movwf Counter

goto Start


;********************************************************************************************************************


Start bcf STATUS,RP0
btfss Flag,0 ;delay set on how frequently reverse function can opperate to prevent the jitters!!
goto timesup
decfsz Timer3,f
goto Speedtest
bcf Flag,0

timesup btfss GPIO,2 ;is GPIO,2 pressed? If so call delay, change direction and continue
goto Speedtest
call DelayFunc
call Direction
movlw D'255'
movwf Timer3
bsf Flag,0
goto Continue

Speedtest btfss GPIO,3 ;Test speed button for forward or if held longer - Reverse
goto Continue
call DelayFunc
call DelayFunc
call DelayFunc
call DelayFunc
btfss GPIO,3
goto Speedup
release call DelayFunc
btfsc GPIO,3
goto release
call Slower
goto Continue

Speedup call Faster
goto Continue







;************************************************************************************************

Continue
btfsc FwdRev,0
goto Reverse


Forward
movfw Counter
call Sequence
movwf GPIO
call Delay
incf Counter,f
movlw b'1000' ;make sure counter does not overflow
subwf Counter,w
btfsc STATUS,Z
clrf Counter
movfw Counter
bsf STATUS,RP0
movwf EEDATA
movlw 0x2C
movwf EEADR ;write address
bsf EECON1,WREN ;Enable write
movlw 0x55 ;Unlock write
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;Start the write
bcf STATUS,RP0
goto Start



Reverse
movfw Counter
call Sequence
movwf GPIO
call Delay
decf Counter,f
movlw b'11111111' ;make sure counter does not overflow
subwf Counter,w
btfss STATUS,Z
goto Skip
movlw b'111'
movwf Counter
Skip bsf STATUS,RP0
movwf EEDATA
movlw 0x2C
movwf EEADR ;write address
bsf EECON1,WREN ;Enable write
movlw 0x55 ;Unlock write
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;Start the write
bcf STATUS,RP0
goto Start


END


;Thanks
 
I suspect that counter is overflowing. Try adding,
Code:
Sequence	[COLOR="Red"]andlw	0x07
		[/COLOR]addwf	PCL,f
		retlw	b'000001'	; 0000 ;bit's 2&3 are to skip GP2&3
		retlw	b'000011'	; 0001
		retlw	b'000010'	; 0010
		retlw	b'010010'	; 0011
		retlw	b'010000'	; 0100
		retlw	b'110000'	; 0101
		retlw	b'100000'	; 0110
		retlw	b'100001'	; 0111

I also notice you aren't waiting for any previous write to finish before starting a new one. To check add the following,
Code:
Slower			 
		incf	T1,f
		movfw	T1 
		bsf	STATUS,RP0
[COLOR="Red"]WaitWr		btfsc	EECON1,WR
		goto	WaitWr[/COLOR]
		movwf	EEDATA
		movlw	0x2A 
		movwf	EEADR		;write to address 0
		bsf	EECON1,WREN	;Enable write
		movlw	0x55		;Unlock write
		movwf	EECON2
		movlw	0xAA 
		movwf	EECON2
		bsf	EECON1,WR	;Start the write
		bcf	STATUS,RP0
		retlw	0

Mike.
 
Hi Mike,

Thanks for the reply.

Checking the WR bit sounds like good practice and I will implement it but if the counter write address is different from the original initialising counter read address the code works which I think shows that the time between writes is not an issue.

Adding the and command at the start of my lookup table gets the code running but I have included measures to prevent the Counter file from exceeding it''s limits after any change to it's value. The code works without the writing to eeprom command which confirms I am not exceeding my lookup table limit's so how is my Counter being changed by the write to eeprom command?
 
I noticed you had code to check for overflow but it only checked if the counter was equal to 8. If the counter gets any value (from EEPROM) that is out of range it will crash. You should change the code so any value greater than 7 resets it or just leave the AND in the code.

Mike.
 
I thought about that and loaded the eeprom with 0 in MPLAB before programming so technically the eeprom should start up effectively cleared and never go out of range?

It has to be something allong those lines because if I put clrf Counter in the initialisation part after loading the eeprom value into Counter everything works fine. But that does not answer why the program does not freeze up when I have the read and write eeprom address different from each other (having the read address cleared in MPLAB before programming).

I am stumped and although AND'ing the code before the lookup helps I would rather keep the Counter in line before it gets to that point otherwise something random must be happening and the steps may jump.
 
If the EEPROM has its default value of 0xff and you decrement it then it will fall outside of your check and cause a crash. I don't know why counter is getting outside the range of 0-7 but it is. Instead of your overflow checks just AND with 7.

So, instead of,
Code:
		incf	Counter,f
		movlw	b'1000'		;make sure counter does not overflow
		subwf	Counter,w
		btfsc	STATUS,Z
		clrf	Counter
		movfw	Counter
You have,
Code:
		incf	Counter,w
		andlw	0x07	;make sure counter does not overflow
		movwf	Counter
Same with the decrement.

Mike.
 
Hi Mike,

Sorry for the late response.

That is a very useful tip you have given me and very code efficient. Thanks!

I have used it in my code but there is still something strange going on when I read and write to the same address? I have lost my place with this program now but hopefully I will realise the error of my ways as I get back up to speed.

Thanks again.

Chris
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top