If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Hello all, this is a thread to show code i want to program for driving stepper motors, currently we have an ongoing talk bout it in chatroom, so for clearer help i am posting it here. Feel free to modify and use in any way.
BE AWARE IT IS NOT FINAL CODE
#include <p16F628A.inc>
__CONFIG (_PWRTE_ON & _WDT_OFF & _BODEN_OFF & _LVP_OFF & _INTRC_OSC_NOCLKOUT)
org 0
#DEFINE TLACH PORTA,1 ; tlacidlo nahor
#DEFINE TLACD PORTA,2 ; tlacidlo nadol
#DEFINE TLACSET PORTA,3 ; tlacidlo set
#DEFINE TLACL PORTA,0 ; tlacidlo vlavo
#DEFINE TLACR PORTA,7 ; tlacidlo vpravo
#DEFINE BUSY PORTB,0 ; Busy
#DEFINE O1 PORTB,7 ; vystupy pre krok motor
#DEFINE O2 PORTB,6 ;
#DEFINE O3 PORTB,5 ;
#DEFINE O4 PORTB,4 ;
#DEFINE XAXIS PORTB,1 ; led a enable pre X os
#DEFINE YAXIS PORTB,2 ;
#DEFINE ZAXIS PORTB,3 ;
mem1 equ 20h
mem2 equ 21h
mem3 equ 22h
mem4 equ 23h ; hodnota odchylky
memX equ 24h ; hodnota kroku
memY equ 25h ; hodnota kroku
memZ equ 26h ; hodnota kroku
Start:
MOVLW B'00000111'
MOVWF CMCON
bsf STATUS,RP0 ;
MOVLW B'11101111' ; Busy(A4) output
MOVWF TRISA ;
MOVLW B'00000000' ; PortB vystupny
MOVWF TRISB
bcf STATUS,RP0 ; back to Register Page 0
BCF O1 ;vynuluje vsetky vystpy
BCF O2
BCF O3
BCF O4
BSF XAXIS
BCF YAXIS
BCF ZAXIS
BCF BUSY
MOVLW 10 ;nastavi pociatocnu rychlost 10
MOVWF mem4
MOVLW B'00010001' ;
MOVWF memX
MOVWF memY
MOVWF memZ
GOTO MainLoop
Odchylka:
CLRF mem1 ; podprogram ochylka
MOVLW 15 ; zmenou tohoto cisla muzete zmenit rychlost
MOVWF mem2
MOVFW mem4 ; nakopiruje hodnotu z mem4 do mem3, aby m4 ostalo rovnake aj nabuduce
MOVWF mem3
GOTO Subrutina
Subrutina:
INCFSZ mem1,1
GOTO Subrutina
DECFSZ mem2,1
GOTO Subrutina
DECFSZ mem3,1
GOTO Subrutina
RETURN
MainLoop:
BTFSC TLACSET
GOTO Setmenu ; vyvola setmenu na nastavenie rychlosti
BTFSC TLACR
GOTO RotR ; rotuj vystup vpravo X/Y/Z
BTFSC TLACL
GOTO RotL ; rotuj vystup vlavo Z/Y/X
BTFSC TLACH ; sprav krok vpravo v aktivnej osi
GOTO StepR
BTFSC TLACD ; sprav krok vlavo v aktivnej osi
GOTO StepL
GOTO MainLoop ;vracia sa na hlavny program
StepR:
BSF BUSY
;part1
BCF O4
BSF O1
CALL Odchylka
;part2
BCF O1
BSF O2
CALL Odchylka
;part3
BCF O2
BSF O3
CALL Odchylka
;part4
BCF O3
BSF O4
CALL Odchylka
BCF O4
BCF BUSY
GOTO MainLoop
StepL:
BSF BUSY
;part1
BCF O1
BSF O4
CALL Odchylka
;part2
BCF O4
BSF O3
CALL Odchylka
;part3
BCF O3
BSF O2
CALL Odchylka
;part4
BCF O2
BSF O1
CALL Odchylka
BCF O1
BCF BUSY
GOTO MainLoop
RotR:
CALL Odchylka
BSF BUSY
BTFSC TLACR
GOTO $-1
BCF BUSY
BTFSC XAXIS ;ak je x aktivna, nastavi aktivnu y
GOTO SetActiveY
BTFSC YAXIS ;ak je y aktivna, nastavi aktivnu z
GOTO SetActiveZ
BTFSC ZAXIS ;ak je z aktivna, nastavi aktivnu x
GOTO SetActiveX
GOTO MainLoop
RotL:
CALL Odchylka
BSF BUSY
BTFSC TLACR
GOTO $-1
BCF BUSY
BTFSC XAXIS ;test ci je X axis 1
GOTO SetActiveZ
BTFSC YAXIS
GOTO SetActiveX
BTFSC ZAXIS
GOTO SetActiveY
GOTO MainLoop
SetActiveX: ;nastavi os X ako aktivnu
BSF XAXIS
BCF YAXIS
BCF ZAXIS
GOTO MainLoop
SetActiveY: ;nastavi os y ako aktivnu
BCF XAXIS
BSF YAXIS
BCF ZAXIS
GOTO MainLoop
SetActiveZ: ;nastavi os z ako aktivnu
BCF XAXIS
BCF YAXIS
BSF ZAXIS
GOTO MainLoop
krokXL:
BSF BUSY
RLF memX,1 ;vyrotuje akukolvek hodnotu v pamati memx
CALL Odchylka
BCF BUSY
GOTO MainLoop
krokYL:
BSF BUSY
RRF memY,1
CALL Odchylka
BCF BUSY
GOTO MainLoop
krokZL:
BSF BUSY
RRF memZ,1
CALL Odchylka
BCF BUSY
GOTO MainLoop
krokXR:
BSF BUSY
RRF memX,1
CALL Odchylka
BCF BUSY
GOTO MainLoop
krokYR:
BSF BUSY
RRF memY,1
CALL Odchylka
BCF BUSY
GOTO MainLoop
krokZR:
BSF BUSY
RRF memZ,1
CALL Odchylka
BCF BUSY
GOTO MainLoop
Setmenu:
BSF BUSY ;indikuje vstup do nastavenia
CALL Odchylka
BTFSC TLACSET ;skontrolovat ci je tlacidlo este stlacene,
GOTO $-1 ;skace ak je 0, preskoci navrat a pokracuje
MOVLW 10
BTFSC TLACH ;ak je tlacidlo stlacene, skoci dalej a pricita W ku mem4
CALL RychlH
BTFSC TLACD ;ak je tlacidlo stlacene, skoci dalej a odcita W ku mem4
CALL RychlD
BCF BUSY ;zhasne pre opustenie menu
BTFSC TLACL
GOTO MainLoop
GOTO Setmenu
GOTO MainLoop ;navrat z podprogramu
RychlH:
BCF BUSY
BTFSC TLACH ;ak je TlacH stlacene, vrati o jeden dozadu
GOTO $-1
INCF mem4,O'10' ;Odrata hodnotu W
BSF BUSY
RETURN
RychlD:
BCF BUSY
BTFSC TLACD ;ak je TlacD stlacene, vrati o jeden dozadu
GOTO $-1
DECF mem4,W
BSF BUSY
RETURN
GOTO MainLoop
END
EDIT> BUG1: Switching between outputs fixed (with TLACR/TLACL) BUG2: Switching from setting menu fixed (not able due to getting into it, changed to TLACR for leaving. Added delay(odchylka) for startup BUG3: PORTB:4 double defined, fixed BUG4: BUSY signal could not get out of PORTA:4 for unknown reason to me, switched to PORTB:0, now indicates fine.
BUG5: Delay shortening/longering does not work, something wrong with sintax of RychlH part (INCF) BUG6: Remembering of last step position not working yet, currently makes all 4 steps at once so it can continue from same place even when outputs are switched
Registered Member #3414
Joined: Sun Nov 14 2010, 05:05PM
Location: UK
Posts: 4245
I'm also working on a driver for two stepper motors for a coil winding machine (I should be posting something this weekend). I envisage that my control program will be a lot simpler than this. I'm debating at the moment whether to use two I/O pins for each stepper motor, or four.
I'm planning to keep mine as simple as possible, and to control it from an Andriod phone and Rasberry Pi. but it's interesting to see how others go about it.
Registered Member #27
Joined: Fri Feb 03 2006, 02:20AM
Location: Hyperborea
Posts: 2058
Doing things like "BCF PORTB,7" is unsafe.
The reason is that the processor will read the state of the pins, modify and write back. There are many reasons why pins could be in a different state than you expect. This sort of error is also extremely hard to debug simce it tend to go away when you start looking for it.
As an example PORTB contains 0b01000000: BCF PORTB,6 BSF PORTB,7 If the capacitive load on Pin 6 is large enough the second instruction will read the pin as 1 and write it back, making the first instruction a no operation. If you single step through the code or simulate it the problem disappears.
So always build up your value in a memory register or accumulator, then move the whole byte to the port. That way there will be no read-modify-write cycle. More advanced microcontrollers have a special set bits register to avoid this problem.
Worst case is that your drivers and motors go up in flames or that the device will chop someones fingers off when noise/ringing on a pin flips a bit in the output register.
BUG4: BUSY signal could not get out of PORTA:4 for unknown reason to me, switched to PORTB:0, now indicates fine.
RA4 is an open drain output that can only sink current. If you want to source current you need to add a suitable pull-up resistor.
BUG4: BUSY signal could not get out of PORTA:4 for unknown reason to me, switched to PORTB:0, now indicates fine.
RA4 is an open drain output that can only sink current. If you want to source current you need to add a suitable pull-up resistor.
Aaaah... the art of not reading the whole datasheet - my mistake.
For an update, currently i am trying to remember last step made and rotate next from that memorized value, with no luck yet. Will try to tinker with it a bit, or try other way. Also, rotating enable XYZ outputs right works fine with 'i will not continue until you leave that button untouched' feature, but not to left, even when it is the same piece of code. (just different outputs and inputs). Rotating left is too fast even when there is same time delay as in rotate right... Maybe reading outputs bug just appeared?
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.