Actions

Difference between revisions of "3d stepper source"

From Just in Time

 
m (2 revisions: copying content from old site)
 
(One intermediate revision by the same user not shown)
Line 77: Line 77:
 
  OutputXY EQU rc ; port C outputs the signals for X(0-3) and Y(4-7)  
 
  OutputXY EQU rc ; port C outputs the signals for X(0-3) and Y(4-7)  
 
  OutputZ EQU rb ; port B outputs the signals for Z on bits(0-3)
 
  OutputZ EQU rb ; port B outputs the signals for Z on bits(0-3)
  KeyboardColumns EQU ra ; port A serves keyboard columns (in a.0-a.2)
+
  KeyboardRows EQU ra ; port A serves keyboard rows (outputs, a.0-a.2)
  KeyboardRows    EQU rb ; port B serves keyboard rows (in b.1-b.3)
+
  KeyboardRow0 EQU ra.0
 +
KeyboardRow1 EQU ra.1
 +
KeyboardRow2 EQU ra.2
 +
KeyboardColumns EQU rb ; port B serves keyboard columns (inputs b.1-b.3)
 +
KeyboardCol0 EQU rb.1
 +
KeyboardCol1 EQU rb.2
 +
KeyboardCol2 EQU rb.3
 +
KeyboardMask EQU %00000111
 +
 
  SerialOut EQU ra.3 ; pins ra.3 and rb.0 are serial output and
 
  SerialOut EQU ra.3 ; pins ra.3 and rb.0 are serial output and
 
  SerialIn EQU rb.0 ; input respectively.
 
  SerialIn EQU rb.0 ; input respectively.
Line 102: Line 110:
 
  Error2 DS 2 ; cumulative errr in the first dimension
 
  Error2 DS 2 ; cumulative errr in the first dimension
 
  Error1 DS 2 ; cumulative error in the second dimension
 
  Error1 DS 2 ; cumulative error in the second dimension
 +
 +
; aliases for
 +
MovedX EQU StepsToGo
 +
MovedY EQU StepsToGo
 +
MovedZ EQU StepsToGo
 +
 
  StepperDelayCount DS 2  ; timeout between steps
 
  StepperDelayCount DS 2  ; timeout between steps
 
  PosX DS 1 ; X, Y and Z pos. 8 bits precision is more
 
  PosX DS 1 ; X, Y and Z pos. 8 bits precision is more
Line 112: Line 126:
 
   
 
   
 
  StepperState DS 1 ; current stepper state. see below.
 
  StepperState DS 1 ; current stepper state. see below.
 +
StepperFlags DS 1 ;
 +
StepperKeyMoved EQU StepperFlags.0
 
   
 
   
 
  ; interruptable states, these are pre-empted by a move command
 
  ; interruptable states, these are pre-empted by a move command
Line 194: Line 210:
 
  RB_ST equ %11111111 ;SX18/20/28/48/52 port B ST value
 
  RB_ST equ %11111111 ;SX18/20/28/48/52 port B ST value
 
  RB_LVL equ %00000000 ;SX18/20/28/48/52 port B LVL value
 
  RB_LVL equ %00000000 ;SX18/20/28/48/52 port B LVL value
  RB_PLP equ %11111111 ;SX18/20/28/48/52 port B PLP value
+
  RB_PLP equ %11110001 ;SX18/20/28/48/52 port B PLP value
 
   
 
   
 
  RC_latch equ %00000000 ;SX18/20/28/48/52 port C latch init
 
  RC_latch equ %00000000 ;SX18/20/28/48/52 port C latch init
Line 421: Line 437:
 
  jmp pc + w
 
  jmp pc + w
 
  jmp :ZeroStepperOutputs ; idle, do noting
 
  jmp :ZeroStepperOutputs ; idle, do noting
  jmp :EndStepper ; idle, handle keyboard
+
  jmp :DoKeyboard1 ; idle, handle keyboard
  jmp :EndStepper ; timeout while handling keyboard
+
  jmp :DoKeyboard2 ; timeout while handling keyboard
 
  jmp :EndStepper ; state 3 not used
 
  jmp :EndStepper ; state 3 not used
 
   
 
   
Line 431: Line 447:
 
  jmp :OutputStepper; latch x,y,z-positions to outputs
 
  jmp :OutputStepper; latch x,y,z-positions to outputs
 
   
 
   
  ; state #7 no jump, just sstart executing here  
+
  ; state #7 no jump, just start executing here  
 
  ; wait a while before moving to next position
 
  ; wait a while before moving to next position
 
  Dec16 StepperDelayCount
 
  Dec16 StepperDelayCount
Line 444: Line 460:
 
  Dec16 StepsToGo
 
  Dec16 StepsToGo
 
  mov w, Direction0
 
  mov w, Direction0
  call MakeStep
+
  call :MakeStep
 +
jmp :EndStepper
 +
 +
:DoKeyboard1
 +
Dec16 StepperDelayCount
 +
Test16 StepperDelayCount
 +
jnz :EndStepper; 2
 +
 +
MovLit16 StepperDelayCount, StepperDelay ;2
 +
clr StepsToGo ; 1
 +
clr StepsToGo + 1 ;1
 +
 +
; activate row 1
 +
mov m, #DDIR_W
 +
mov !KeyboardRows, #((RA_DDIR & ~KeyboardMask) | (KeyboardMask ^  (1 << . KeyboardRow1)))
 +
clrb KeyboardRow1 ; 1
 +
 +
sb KeyboardCol0 ; 1
 +
call :Key10 ; 8
 +
sb KeyboardCol1 ;1
 +
call :Key11
 +
sb KeyboardCol2
 +
call :Key12
 +
setb KeyboardRow1
 +
mov StepperState, #StepperCountKeys
 
  jmp :EndStepper
 
  jmp :EndStepper
 +
 +
:DoKeyboard2
 +
mov StepperState, #StepperIdleKeys
 +
; activate row 2
 +
mov m, #DDIR_W
 +
mov !KeyboardRows, #((RA_DDIR & ~KeyboardMask) | (KeyboardMask ^ (1 << . KeyboardRow2)))
 +
clrb KeyboardRow2
 +
sb KeyboardCol0
 +
call :Key20
 +
sb KeyboardCol1
 +
call :Key21
 +
sb KeyboardCol2
 +
call :Key22
 +
setb KeyboardRow2
 +
 +
 +
:EndKeyboard test StepsToGo
 +
jnz :DoOutputStepper
 +
 
   
 
   
 
  ; set all stepper outputs to zero
 
  ; set all stepper outputs to zero
Line 451: Line 510:
 
  ; idle.
 
  ; idle.
 
  :ZeroStepperOutputs
 
  :ZeroStepperOutputs
 +
setb KeyboardRow0
 
  clr OutputXY
 
  clr OutputXY
 
  mov w, OutputZ
 
  mov w, OutputZ
Line 457: Line 517:
 
  jmp :EndStepper
 
  jmp :EndStepper
 
   
 
   
 +
:Key10 dec PosX
 +
setb StepsToGo.0
 +
retp
 +
:Key11 dec PosY
 +
setb StepsToGo.0
 +
retp
 +
:Key12 inc PosX
 +
setb StepsToGo.0
 +
retp
 +
:Key20 inc PosY
 +
setb StepsToGo.0
 +
retp
 +
:Key21 dec PosZ
 +
setb StepsToGo.0
 +
retp
 +
:Key22 inc PosZ
 +
setb StepsToGo.0
 +
retp
 
   
 
   
 
  ; update position 1 state
 
  ; update position 1 state
Line 468: Line 546:
 
  Sub16 Error1, ErrorTreshold
 
  Sub16 Error1, ErrorTreshold
 
  mov w, Direction1
 
  mov w, Direction1
  call MakeStep
+
  call :MakeStep
 
  :EndUpdatePos1
 
  :EndUpdatePos1
 
  mov StepperState, #StepperUpdatePos2
 
  mov StepperState, #StepperUpdatePos2
Line 484: Line 562:
 
  Sub16 Error2, ErrorTreshold
 
  Sub16 Error2, ErrorTreshold
 
  mov w, Direction2
 
  mov w, Direction2
  call MakeStep
+
  call :MakeStep
 
   
 
   
 
  :TransitionToOutputPos
 
  :TransitionToOutputPos
Line 492: Line 570:
 
   
 
   
 
   
 
   
; output positions state
 
:OutputStepper ; now translate x,y and z to stepper
 
; line outputs
 
 
; first move x and y to combined
 
; XY output (4 bits each)
 
mov w, PosX
 
call ExpandPos
 
mov InterruptScratch, w
 
mov w, PosY
 
call ExpandPos2
 
or w, InterruptScratch
 
mov OutputXY, w
 
mov w, PosZ
 
call ExpandPos2 ; translate z pos to high bits
 
mov InterruptScratch, w
 
mov w, OutputZ
 
and w, #%00001111
 
or w, InterruptScratch
 
mov OutputZ, w
 
mov StepperState, #StepperCounting
 
 
Test16 StepsToGo
 
jnz :EndStepper ; if steps to go is zero, transition to idle
 
mov StepperState, #StepperIdle
 
 
:EndStepper
 
 
EndInterrupt mov w, #RetiwValue
 
retiw
 
 
   
 
   
 
   
 
   
  MakeStep ; increase or decrease an axis dimension, depending on the value of w
+
  :MakeStep ; increase or decrease an axis dimension, depending on the value of w
 
  ; w.7 determines whether we increase or decrease (1 = increase)
 
  ; w.7 determines whether we increase or decrease (1 = increase)
 
  ; w.1 and w.0 determine which axis (0 = x, 1 = y, 2 = z, 3 = crash horibly)
 
  ; w.1 and w.0 determine which axis (0 = x, 1 = y, 2 = z, 3 = crash horibly)
Line 553: Line 601:
 
  retp
 
  retp
 
   
 
   
  ExpandPos ; convert from number (0-3) to  
+
  :ExpandPos ; convert from number (0-3) to  
 
  ; bit pattern (0001, 0010, 0100, 1000)
 
  ; bit pattern (0001, 0010, 0100, 1000)
 
  and w, #7
 
  and w, #7
Line 561: Line 609:
 
  ; retw %0001, %0101, %0100, %0110, %0010, %1010, %1000, %1001
 
  ; retw %0001, %0101, %0100, %0110, %0010, %1010, %1000, %1001
 
   
 
   
  ExpandPos2 ; easiest way to create a bit pattern in upper
+
  :ExpandPos2 ; easiest way to create a bit pattern in upper
 
  ; bits is to have another version of Expand
 
  ; bits is to have another version of Expand
 
  ; instead of swapping
 
  ; instead of swapping
Line 569: Line 617:
 
  retw %00010000, %00110000, %00100000, %01100000, %01000000, %11000000, %10000000, %10010000
 
  retw %00010000, %00110000, %00100000, %01100000, %01000000, %11000000, %10000000, %10010000
 
   
 
   
 +
ORG $0100
 +
; output positions state
 +
:OutputStepper ; now translate x,y and z to stepper
 +
; line outputs
 +
 +
; first move x and y to combined
 +
; XY output (4 bits each)
 +
mov StepperState, #StepperCounting
 +
:DoOutputStepper
 +
mov w, PosX
 +
call :ExpandPos
 +
mov InterruptScratch, w
 +
mov w, PosY
 +
call :ExpandPos2
 +
or w, InterruptScratch
 +
mov OutputXY, w
 +
mov w, PosZ
 +
call :ExpandPos2 ; translate z pos to high bits
 +
mov InterruptScratch, w
 +
mov w, OutputZ
 +
and w, #%00001111
 +
or w, InterruptScratch
 +
mov OutputZ, w
 +
 +
Test16 StepsToGo
 +
jnz :EndStepper ; if steps to go is zero, transition to idle
 +
mov StepperState, #StepperIdle
 +
 +
:EndStepper
 +
 +
EndInterrupt mov w, #RetiwValue
 +
retiw
 
 
 
 
 
  ;------------------------ INITIALIZATION ROUTINE -----------------------
 
  ;------------------------ INITIALIZATION ROUTINE -----------------------
 
 
  Initialize
 
  Initialize
 
  ;<group comment>
 
  ;<group comment>
Line 588: Line 667:
 
  mov !rc,w
 
  mov !rc,w
 
   
 
   
 +
mov m, #PLP_W ;point MODE to write PLP register
 
  mov w,#RA_PLP            ;Setup RA Weak Pull-up, 0 = enabled, 1 = disabled
 
  mov w,#RA_PLP            ;Setup RA Weak Pull-up, 0 = enabled, 1 = disabled
 
  mov !ra,w  
 
  mov !ra,w  
Line 623: Line 703:
 
   
 
   
 
  Main
 
  Main
 +
bank LineDrawingBank
 +
mov StepperState, #StepperIdleKeys
 +
MovLit16 StepperDelayCount, StepperDelay
 +
 
  bank SERIAL
 
  bank SERIAL
 
  mov w, #OpeningMessage // 256; modulo to prevent warning
 
  mov w, #OpeningMessage // 256; modulo to prevent warning
Line 781: Line 865:
 
  ; now set the state to 'output position'
 
  ; now set the state to 'output position'
 
  ; this will activate the stepper.
 
  ; this will activate the stepper.
 +
clrb KeyboardRow0
 
  mov StepperState, #StepperOutputPos
 
  mov StepperState, #StepperOutputPos
 
  retp
 
  retp
Line 913: Line 998:
 
  jmp :wait                  ;
 
  jmp :wait                  ;
 
   
 
   
;not    w                      ;ready bits (inverse logic)
 
 
  mov    tx_high,w              ; store data byte
 
  mov    tx_high,w              ; store data byte
 
  clrb    tx_low.7                ; set up start bit
 
  clrb    tx_low.7                ; set up start bit
Line 948: Line 1,032:
 
  org $500
 
  org $500
 
  StringPage EQU $
 
  StringPage EQU $
  OpeningMessage DW '3D Stepper Control v1.0', 13
+
  OpeningMessage DW '3D Stepper Control v1.00.01', 13
 
  DW 'J&J Productions 2006', 13  
 
  DW 'J&J Productions 2006', 13  
 
  ; notice the missing zero at the above line.
 
  ; notice the missing zero at the above line.
 
  Prompt DW '>', 0
 
  Prompt DW '>', 0

Latest revision as of 22:07, 12 July 2010

;=======================================================================
;TITLE:         stepper.src
;
;PURPOSE:       control up to 3 stepper motors to move in straight lines
;		in space. Take commands from a serial port.
;
;AUTHOR:        Danny Havenith
;	Copyright (c) 2006 Danny Havenith
; Use, modification and distribution is subject to the Boost Software
; License, Version 1.0. (See copy at 
; http://www.boost.org/LICENSE_1_0.txt)
;
; UART VP and associated subroutines:
; Copyright © [01/26/1999] Scenix Semiconductor, Inc. All rights reserved.
;
;REVISIONS:
;
;CONNECTIONS:
; ra.0-2 3 keyboard row outputs
; rb.1-3 3 keyboard column inputs for keyboard matrix
;     ra.0 is the row output for the 'emergency keys' 
;     (min, max, emergency stop) and is always enabled (low-active) when motors move
;     This pin is also connected to a 'busy'-led.
; ra.3 serial out
; rb.0 serial in
; rb.4-7 Z-axis stepper motor output
; rc.0-3 X-axis stepper motor output
; rc.4-7 Y-axis stepper motor output
;
;
;DETAILS:
; Contains 2 VPs: 1) stepper motor controller 2) serial in/out
;=======================================================================


;-------------------------- DEVICE DIRECTIVES --------------------------

		DEVICE		SX28,OSCHS1,TURBO
		DEVICE		STACKX, OPTIONX

		IRC_CAL		IRC_SLOW

		RESET		Initialize
;------------------------------ CONSTANTS ------------------------------

;------------------------------ --------- ------------------------------
; The following constants are meant to be changed to configure for 
; diverse hardware.

Frequency		EQU	4_000_000 ; clock frequency
BaudRate		EQU	9600 	  ; serial port baudrate
InterruptsPerBit 	EQU	3	; samples per serial bit
HalfStepsPerSecond 	EQU 	350     ; stepper motor half steps/sec

; uncomment the following EQU if using a MAX232, since we need to
; reverse the rs-232 signals while not using a MAX232
UsingMAX232	EQU 1		; we're using a max232 

;------------------------------ --------- ------------------------------
; some derived constants (derived from the ones above)
; These are not meant to be changed manually.
;
		; clock ticks per interrupt
InterruptPeriod	EQU	Frequency/(InterruptsPerBit * BaudRate) + 1
		; value to put in W to obtain the clock ticks per interrupt
		; formulated in this particular way to get rid of 'Literal 
		; truncated to 8 bits' warning
RetiwValue	EQU 	256-InterruptPeriod 
		; Interrupts per stepper motor step
StepperDelay	EQU	Frequency/(HalfStepsPerSecond*InterruptPeriod)

; number of interrupts to pass between detection of the start bit ("flank") and
; the middle of the first bit, plus one (see code for that 'plus one').
StartDelay	EQU InterruptsPerBit/2 + InterruptsPerBit + 1

		; port definitions
OutputXY	EQU	rc ; port C outputs the signals for X(0-3) and Y(4-7) 
OutputZ		EQU	rb ; port B outputs the signals for Z on bits(0-3)
KeyboardRows	EQU	ra ; port A serves keyboard rows (outputs, a.0-a.2)
KeyboardRow0	EQU	ra.0
KeyboardRow1	EQU	ra.1
KeyboardRow2	EQU	ra.2
KeyboardColumns EQU	rb ; port B serves keyboard columns (inputs b.1-b.3)
KeyboardCol0	EQU	rb.1
KeyboardCol1	EQU	rb.2
KeyboardCol2	EQU	rb.3
KeyboardMask	EQU	%00000111

SerialOut	EQU	ra.3 ; pins ra.3 and rb.0 are serial output and
SerialIn	EQU	rb.0 ; input respectively.

;------------------------------ VARIABLES ------------------------------
			ORG	$08
; 'global' bank, registers that can be accessed regardles of the current
; bank.
ErrorTreshold	DS 2	; Error treshold (for Bresenhams)
Step1		DS 2	; Step siz