Skip to content

Instantly share code, notes, and snippets.

@ivand58
Created June 25, 2019 19:31
Show Gist options
  • Save ivand58/47e9f5c0a1d6c65163e10763bb7d7c03 to your computer and use it in GitHub Desktop.
Save ivand58/47e9f5c0a1d6c65163e10763bb7d7c03 to your computer and use it in GitHub Desktop.
stepper motor control demo
\ stepper motor 5 wires connected to pb12..pb15 @ bluepill stm32f103 ,
\ tested at mecrisp-stellaris 2.5.0
\ port B base addr
$40010C00 constant GPIOB
GPIOB 4 + constant GPIOB_CRH
GPIOB $10 + constant GPIOB_BSRR
\ init pb12..pb15 omode-pp, all coils to gnd
: sminit ( -- )
$F0000000 ( clr b12-b15 )
GPIOB_BSRR !
\ pp output 2MHz
GPIOB_CRH @
$0000FFFF and
$22220000 or
GPIOB_CRH !
;
\ table with 8 phases - with half-steps
create a-ab-b
%0001 12 lshift dup $F000 xor 16 lshift or ,
%0011 12 lshift dup $F000 xor 16 lshift or ,
%0010 12 lshift dup $F000 xor 16 lshift or ,
%0110 12 lshift dup $F000 xor 16 lshift or ,
%0100 12 lshift dup $F000 xor 16 lshift or ,
%1100 12 lshift dup $F000 xor 16 lshift or ,
%1000 12 lshift dup $F000 xor 16 lshift or ,
%1001 12 lshift dup $F000 xor 16 lshift or ,
0 variable smix \ index
\ next io according to the index
: smwr ( u -- )
cells a-ab-b + @
GPIOB_BSRR !
;
\ one step, clockwise
: smcw ( -- )
smix @
1+ 7 and dup
smix !
smwr
;
\ delay in microsec
850 variable dly
\ mainloop
: main ( -- )
sminit
begin
dly @ dup 1000 > if 1000 / ms else us then
smcw
key? until
;
\ try with custom delay
: try ( u -- )
dly !
main
;
\ after reset just run
: init
72MHz
1000 systick-hz
main
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment