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
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