
AN155
Rev. 1.1 11
5.2. User Interface
The primary user interface is an ASCII terminal with a
baud rate of 57,600 bps. The main loop of the code
parses the characters from the terminal and performs
the appropriate action. The flowchart for the main loop
is shown in Figure D1.
The main loop first checks to see if the motor is moving.
The LED bit is used to turn on the LED and is also used
as a state variable. The LED is turned on by the move
function and is turned off by the Timer ISR on
completion of the move. While the motor is moving the
main loop will periodically update the position to the
terminal display. This is accomplished by overwriting the
previous position. The position is written to the terminal
followed by a carriage return without the normal line
feed character. This overwrites the previous number.
The position is written to the terminal using the puts()
put string function. A string of four space characters are
used to blank trailing characters. This is necessary
when moving from a large 5-digit number to a small 1-
digit number. A short delay is used to prevent the
transmitter buffer from overflowing.
Next the main loop checks the doneFlag. The Timer
ISR sets the doneFlag when the move is complete. If
doneFlag is set, the main loop will call the doneMsg()
function. The doneMsg() function displays the word
“done” and readies the terminal for the next command.
The doneFlag is cleared by doneMsg().
Next the main loop enters the command parser. The
command parser is implemented using a large switch
statement. The parser gets one character from the
terminal and uses a case statement to do different
things depending on the character.
If the character is a carriage return, a newline and
prompt will be transmitted to the terminal. Repeatedly
entering a carriage return will display multiple prompt
characters, each on a new line.
If the character is a p, the parser will get a 16-bit
number from the terminal using the getuint()
function, and move to the new position.
If the character is an a, the parser will get an 8-bit
number from the terminal using the getuchar()
function, store the new value, and display the new value
to the screen for confirmation.
If the character is an s, the main loop will display the
current status information - position and acceleration.
The status command does not wait for any additional
characters.
If the parser does not recognize the character, an invalid
character message will be displayed.
5.3. Move Function
A flowchart for the move function is shown in Figure D2.
The move function first checks to see if the motor is
already at the target position. If the motor is already at
position zero and you enter the command p0, the motor
will not move and the done message will be displayed.
This is necessary to ensure that the algorithm works
properly. The smallest valid move is 1 step in either
direction. The current position is the value stored in the
global variable Position. The desired position,
passed as a parameter to the move() function, is called
targetPosition. The forward flag is set or cleared
depending on whether the target is greater than the
position. Both targetPosition and Position are
unsigned integers. The smaller is subtracted from the
larger to get the variable length.
The global variable TableMax defines how many steps
the motor will accelerate and decelerate. While
accelerating the TableIndex will be incremented until
it reaches TableMax. If length is less than 1024, the
value for TableMax is calculated by dividing length
by four. If length is greater than 1024, TableMax is
set to 255. This is the maximum allowable value for
TableIndex.
Taking length, subtracting two times TableMax for
the acceleration/deceleration steps, and subtracting one
more for the initial step gives the number of constant
velocity slewing steps. This ensures that the total
number of steps is exactly equal to the length.
The move function then schedules the timer to execute
the first step after a short delay. After the initial step, the
Timer Interrupt service routine Timer_ISR() will
control the stepper motor timing from then on.
5.4. Timer ISR
The timer interrupt service routine moves the motor
according to the prescribed profile. The interrupt service
routine will first commutate the motor. Then the
PatternIndex is incremented or decremented
according to the stepper motor direction. The
PatternIndex is used as a pointer to the step pattern.
It is not to be confused with the TableIndex that points
to the linear velocity table. The step pattern index for a
unipolar stepper motor is a modulus 8 counter. It counts
from zero to seven. The step pattern is written to P0. Bit
7 is set to a 1 to ensure that P0.7 remains configured as
an input.
The timer interrupt service routine determines what to
do next depending on the current state of the motor. The
four possible states are Accelerating, Decelerating,
Slewing, or Done. If the motor state is Done, the timer
ISR will disable further timer interrupts, turn off the LED,
Kommentare zu diesen Handbüchern