' Stepper Motor V23
' 18.11.2013
' Software for Sternwarte ÜB
' Stepper motor control, bipolar stepper motor 8HS15-0604S-PG90,
' with PWM control
'
'
' Siderisch: 5.50746 * 10^-3 U/sec oder 181.572 sec Umlaufdauer
' Sonne: 5.492 * 10^-3 U/sec oder 182.083 sec Umlaufdauer
' Mond: 5.307 * 10^-3 U/sec oder 188.430 sec Umlaufdauer
'
' Program compiled with Great Cow BASIC (0.9 3/2/2010)
'
'
' PIC16F690
' ------------
' +5V -| 1 20 |- GND
' PORTA.5 CLKIN -| 2 19 |- PORTA.0 POS_1
' PORTA.4 CLKOUT -| 3 18 |- PORTA.1 POS_2
' PORTA.3 PUSH_BUTTON -| 4 17 |- PORTA.2
' PORTC.5 PWM -| 5 16 |- PORTC.0 PIN_1A
' PORTC.4 -| 6 15 |- PORTC.1 PIN_2A
' PORTC.3 PIN_4A -| 7 14 |- PORTC.2 PIN_3A
' PORTC.6 -| 8 13 |- PORTB.4 LED_G
' PORTC.7 LEFT_ROT -| 9 12 |- PORTB.5 LED_R
' PORTB.7 RIGHT_ROT -| 10 11 |- PORTB.6
' ------------
'
' L293D
' PIC PIN_1A = IC Pin 2 L293D (1A)
' PIC PIN_2A = IC Pin 7 L293D (2A)
' PIC PIN_3A = IC Pin 10 L293D (3A)
' PIC PIN_4A = IC Pin 15 L293D (4A)
' PIC PWM = IC Pin 1/9, 1,2EN, 3,4EN
'
' L293D
' ------------
' 1,2EN -| 1 16 |- Vcc1 (V5+, Logic Supply)
' 1A -| 2 15 |- 4A
' 1Y (BLK Motor) -| 3 14 |- 4Y (RED Motor)
' GND -| 4 13 |- GND
' GND -| 5 12 |- GND
' 2Y (GRN Motor) -| 6 11 |- 3Y (BLU Motor)
' 2A -| 7 10 |- 3A
' Vcc2 (V12+, Motor Supply) -| 8 9 |- 3,4EN
' ------------
#chip 16F690,10
'#config WDT = OFF, BODEN = ON, OSC = HS_OSC, PWRTE = ON, CPD = OFF, CP = OFF, FCMEN = OFF
'FOSC1 = 1, G540 Programmer
'execution time for one half step is average 5 us over 8 half steps
'Siderisch (5029.6-5) us per 0.9° half Step
'Sonne (5043.8-5) us per 0.9° half Step
'Mond (5219.6-5) us per 0.9° half Step
#define START_RAMP_5MS 117 'initial value for ramp 12x 0.3 sec
#define SIDERISCH 5 'SIDERISCH 3rd loop constant, +/- 1 = +/- 2.8 us
#define SONNE 10 'SONNE 3rd loop constant, +/- 1 = +/- 2.8 us
#define MOND 73 'MOND 3rd loop constant, +/- 1 = +/- 2.8 us
'-------------------------- declaration of I/O Pin's ----------------
#define POS_1 PORTA.0
dir POS_1 in
#define POS_2 PORTA.1
dir POS_2 in
#define LED_G PORTB.4
dir LED_G Out
#define LED_R PORTB.5
dir LED_R Out
#define PIN_1A PORTC.0
dir PIN_1A Out
#define PIN_2A PORTC.1
dir PIN_2A Out
#define PIN_3A PORTC.2
dir PIN_3A Out
#define PIN_4A PORTC.3
dir PIN_4A out
#define PWM PORTC.5
dir PWM out
#define LEFT_ROT PORTC.7
dir LEFT_ROT in
#define RIGHT_ROT PORTB.7
dir RIGHT_ROT in
'-------------------------- declaration of variables ----------------
dim const_1 as byte
dim const_2 as byte
dim const_3 as byte
dim const1_12x as byte
dim const2_12x as byte
dim const3_12x as byte
dim SM_Step as byte
dim SM_ENABLE as byte
dim SM_PATTERN_CW_F(4) '4 byte's full step cw
dim SM_PATTERN_CCW_F(4) '4 byte's full step ccw
dim SM_PATTERN_CCW_H(8) '8 byte's half step ccw
'--------------------------- allocation of constants ----------------
'full step pattern for SM control
'CW rotation
SM_PATTERN_CW_F(1) = b'1010'
SM_PATTERN_CW_F(2) = b'1001'
SM_PATTERN_CW_F(3) = b'0101'
SM_PATTERN_CW_F(4) = b'0110'
'full step pattern for SM control
'CCW rotation
SM_PATTERN_CCW_F(1) = b'0110'
SM_PATTERN_CCW_F(2) = b'0101'
SM_PATTERN_CCW_F(3) = b'1001'
SM_PATTERN_CCW_F(4) = b'1010'
'half step pattern for SM control
'CCW rotation
SM_PATTERN_CCW_H(1) = b'0010'
SM_PATTERN_CCW_H(2) = b'0110'
SM_PATTERN_CCW_H(3) = b'0100'
SM_PATTERN_CCW_H(4) = b'0101'
SM_PATTERN_CCW_H(5) = b'0001'
SM_PATTERN_CCW_H(6) = b'1001'
SM_PATTERN_CCW_H(7) = b'1000'
SM_PATTERN_CCW_H(8) = b'1010'
'for SM_SPEED Syderisch, Sonne, Mond
const_1 = 8 'first loop constant
const_2 = 222 'second loop constant
'for SM_SPEED 12x
const1_12x = 15
'const2_12x = START_RAMP_5MS ' 117 - 17, ramp 5 ms down to 827 us (4 rpm)
const3_12x = 17
'------------------------------------ Init --------------------------
wait 255 ms
bsf IOC,IOCA3 'enable Interrupt PORTABChange on PORTA.3
On Interrupt PORTABChange Call ISR1
START_SM:
PORTC = b'0000'
const2_12x = START_RAMP_5MS
set LED_G off
set LED_R off
PUSH_BUTTON = FALSE
SM_Step = 0
SM_ENABLE = FALSE
wait 255 ms
if (POS_1 = on) and (POS_2 = off) then
set LED_R on
set LED_G off
const_3 = SIDERISCH
end if
if (POS_1 = off) and (POS_2 = on) then
set LED_R off
set LED_G on
const_3 = SONNE
end if
if (POS_1 = on) and (POS_2 = on) then
set LED_R on
set LED_G on
const_3 = MOND
end if
wait 1 s
HPWM 1, 30, 204 'PWM 16 kHz 80% duty cycle, 255 = 100% duty cycle,
'Fast Mode 12x
FAST_SM:
if (LEFT_ROT = on) then
if (RIGHT_ROT = on) then
const2_12x = START_RAMP_5MS' Initial value of ramp 12x, ramp time = 0.3 sec
PORTC = b'0000'
end if
end if
if (not PUSH_BUTTON) then ' 800 ns
if (LEFT_ROT = off) then ' 800 ns
'Wippschalter einrastend, Drehrichtung CCW
for SM_Step = 1 to 4 'full stepp movement
PORTC = SM_PATTERN_CCW_F(SM_Step)
SM_SPEED_12x
if (const2_12x >= 18) then const2_12x = const2_12x - 1
next 'for SM_Step = 1 to 4, 4 * 1.8° Step
end if '(LEFT_ROT = off)
if (RIGHT_ROT = off) then ' 800 ns
for SM_Step = 1 to 4 'full stepp movement
PORTC = SM_PATTERN_CW_F(SM_Step)
SM_SPEED_12x
if (const2_12x >= 18) then const2_12x = const2_12x - 1
next 'for SM_Step = 1 to 4, 4 * 1.8° Step
end if '(RIGHT_ROT = off)
goto FAST_SM ' 800 ns
end if '(PUSH_BUTTON = FALSE)
PORTC = b'0000'
wait 255 us
PUSH_BUTTON = FALSE
HPWM 1, 30, 166 'PWM 30 kHz 65% duty cycle, 255 = 100% duty cycle,
'Siderisch, Sonne or Mond
Do
for SM_Step = 1 to 8 'half stepp movement
PORTC = SM_PATTERN_CCW_H(SM_Step)
SM_SPEED
next 'for SM_Step = 1 to 8, 8 * 0.9° Step
if (PUSH_BUTTON) then SM_INIT
Loop 'main
'----------------------------------- subs ---------------------------
sub ISR1 ' Start/Stop movement of Stepper Motor, Interrupt
' on PORTABChange
wait 255 ms
PUSH_BUTTON = TRUE
end sub
'---------------------------------- SM_INIT -------------------------
sub SM_INIT
wait 255 us
goto START_SM
end sub
'---------------------------------- SM_SPEED ------------------------
sub SM_SPEED 'Siderisch, Sonne, Mond
for counter_1 = 1 to const_1
for counter_2 = 1 to const_2
nop
next
next
for counter_3 = 1 to const_3
nop
next
nop
end sub
'--------------------------------- SM_SPEED_12x ---------------------
sub SM_SPEED_12x
for counter_1 = 1 to const1_12x
for counter_2 = 1 to const2_12x
nop
next
next
for counter_3 = 1 to const3_12x
nop
next
nop
nop
end sub