PDA

View Full Version : How to control step motor in accurate timeing



Pichai
- 20th August 2012, 10:55
Hi All:
I am new in Picbasic Pro. I have big problem ,how to write a program to control a stepping motor in accurate timeing.
I use 16F628A and external crystal, two push switch botton(A and B), ULN2003 drive a 5V stepping motor.
The concept is when swith on... motor run clockwise something like 10steps every 2Hz all the time and if
push switch A motor run clockwise as fast as the moter can,or push swith B motor run anticlockwise fast
also. If release swith, motor run 10steps ever 2Hz all the time.
I aleady study in PIC like run stepping motor clockwise, anticlockwise, count step to run, digital clock.

I found Paul Borgmeier made ''Easy & Accurate clock wih out RTC IC''
His code like this(I do test His code realy accurate )
Use PIC 12F629

CMCON=7 'all digital for low power use
GPIO=0
TRISIO=0
OPTION_REG=%10000111 'weak pullups off, TMRO prescale = 256
INTCON=0 'interrupts off
'************************************************* *********
HzTimer VAR Word '1/2 second counter (2 Hz)
HH VAR Byte ' Hours 0-23
MM VAR ByTE ' Minutes 0-59
SS VAR Byte ' Seconds 0-59
col VAR Bit ' colon 1=on, 0=0ff
HzTimer=$7A12 'for 1/2 Sec
HH=12:MM=0:SS=0:col=0 'initial conditions (12:00 O'clock noon)
'************************************************* *********
Main:
ClockLoop: IF intcon.2=0 THEN ClockLoop 'Poll for TMRO overflow
INTCON.2=0 ' Clear TMRO overflow flag
HzTimer=HzTimer-$1000 'decrement timer count
IF HzTimer < $1000 THEN
IF Col=1 THEN ' update time'
SS=SS+1
GPIO.1=0 ' Turn off GP1
IF SS=60 THEN ' minute
SS=0
MM=MM+1
IF MM=60 THEN ' hour
MM=0
HH=HH+1
IF HH=24 THEN HH=0
IF HH=20 then GPIO.0 = 1 ' Turn on GP0 at 8:00pm
GPIO.1=1 ' Turn On GP1 for 1 second on the hour
ENDIF ' Adjust these to meet your needs
if MM=15 then GPIO.0 = 0 ' Turn off GP0 at 8:15pm
ENDIF
ENDIF
Col=Col+1
HzTimer=HzTimer+$7A12 ' Add 0.5 seconds to count
ELSE
' Do something here but must be less than 65.5 mSec
ENDIF
GOTO Main 'forever
' ************************************************** ************
END

My code run stepping motor like this
Use PIC16F628A 4Mhz x'tal, with IC4060 x'tal32.768kHz generate 2Hz to PORTB.3


TRISA = %00000000
DIS VAR BYTE
J VAR BYTE
K VAR BYTE
V VAR BYTE
A VAR BYTE
D VAR BYTE
O VAR BYTE
Start : INPUT PORTB.0
INPUT PORTB.1
IF PORTB.0 = 1 THEN Setclock
IF PORTB.1 = 1 THEN Setacw
IF PORTB.0 = 0 THEN Startclock
IF PORTB.1 = 0 THEN Startclock
GOTO Start

Setclock : TRISA = 0
Mstep VAR BYTE
Steps VAR BYTE
PORTA = 0
Mstep = 0
FOR Steps = 1 TO 4
PORTA = DCD Mstep
PAUSE 3
PORTA = 0
Mstep = Mstep+1
IF Mstep>3 THEN Mstep = 0
NEXT Steps
GOTO Start
Setacw : TRISA = 0
PORTA = 0
Mstep = 0
FOR Steps = 1 TO 4
PORTA = DCD (3-Mstep)
PAUSE 3
PORTA = 0
Mstep = Mstep+1
IF Mstep>3 THEN Mstep = 0
NEXT Steps
GOTO Start
Startclock : FOR V = 0 TO 87
FOR K = 0 TO 1
GOSUB Negative
NEXT K
FOR J = 0 to 3

Motorrun : DIS = 00000001
M : PORTA = DIS
PAUSE 3
DIS = DIS<<1
NEXT J
IF DIS <> %00100000 THEN M
IF PORTB.0 = 1 THEN Setclock
IF PORTB.1 = 1 THEN Setacw
NEXT V

Startclock2: FOR O = 0 TO 1
FOR A = 0 TO 211
GOSUB Negative
FOR D = 0 to 3

Motorrun2 : DIS = 00000001
M2 : PORTA = DIS
PAUSE 3
DIS = DIS<<1
NEXT D
IF DIS <> %00100000 THEN M2
IF PORTB.0 = 1 THEN Setclock
IF PORTB.1 = 1 THEN Setacw
NEXT A
NEXT O
GOTO Startclock

Negative : IF PORTB.3 = 0 THEN Negative
Positive : IF PORTB.3 = 1 THEN Positive
RETURN
end

I am very poor in INTERRUPT(and speak english!) so any one please tell me how to combine two code in one
program. And I don;t need IC CD4060/Xtal 32.768kHz any more.
Pichai Han from Bangkok Thailand.