Docsity

Example Long Delay - Microcontrollers in Mechanical Engineering | EMCH 367, Study notes of Mechanical Engineering

Toggle sidebar
bg1
EMCH 367 Fundamentals of Microcontrollers Example LONG DELAY
EXAMPLE LONG DELAY
OBJECTIVE
This example has the following objectives:
Present a subroutine that can implement a long delay between stepper motor steps
Introduce the concept of how to achieve double precision delays (4-hex) using single precision
(2-hex) variables
PROGRAM EX_LONG_DELAY
This program illustrates the use of subroutine DLAY_SR. This subroutine performs a long delay action
using TOC1. Although the delay is in double precision (4-hex), the controlling variable, DELAY, is
simple precision. This is one of the special features of this subroutine. DELAY controls the higher 2-hex
digits. This is how this works:
First, clear the OC1F flag, which is bit 7 in TFLG1. When doing this, we use the OR operation in
preserve the other bits in TFLG1.
We load DELAY in accA, which is the higher part of accD. Then, we fill accB, i.e., the lower part
of accD, with $00. If DELAY=$XX, then the value in accD is $XX00.
Next, add TCNT to accD and store the result in TOC1. This is the new time
Step in place until OC1F is set. When the number of cycles $XX00 have elapsed, you are done.
The subroutine return control to the main program.
A stepper motor is controlled by sending binary patterns (step sequences) to its controller board. Eight
distinct binary patterns (S0 – S7) are recognized by the stepper motor controller board in your lab.
When these patterns are hit in increasing order, one after the other, the motion is called ‘half speed
forward’. If only the odd sequences are hit (i.e., every second pattern), the motion is ‘full speed
forward’. If the patterns are hit in decreasing order, the motion is, respectively, ‘half-speed backward’
and ‘full speed backward’. The corresponding step sizes are, respectively, +1, +2, -1, -2 (Table 1).
Using 2’s complement 8-bit signed numbers convention, these values correspond to the variable STEP
taking hex values $01, $02, $ff, $fe.
Table 1 Stepper motor speed definitions
Step size STEP
Half speed forward +1 $01
Full speed forward’ +2 $02
Half-speed backward -1 $ff
Full speed backward’ -2 $fe
The program is intended for stepper motor control. The stepper-motor speed is controlled through the
delay inserted between steps. A short delay ensures a high speed, while a long delay produces a slow
speed. The shortest delay that can be physically accommodated by the stepper motor dynamics is 5
ms. For a 2 MHz processor (0.5 s/cycle), this corresponds to 10,000 ($2710) cycles. The maximum
delay is, obviously, $ffff. Since we use a single precision variable, DELAY, we need to round the
numbers $2710 and $ffff to 2 significant hex numbers. We get $2800 and $ff00 (To maintain delay
Dr. Victor Giurgiutiu Page 112/1/2020
pf3
pf4
pf5

Partial preview of the text

Download Example Long Delay - Microcontrollers in Mechanical Engineering | EMCH 367 and more Study notes Mechanical Engineering in PDF only on Docsity!

EXAMPLE LONG DELAY

OBJECTIVE

This example has the following objectives:  Present a subroutine that can implement a long delay between stepper motor steps  Introduce the concept of how to achieve double precision delays (4-hex) using single precision (2-hex) variables

PROGRAM EX_LONG_DELAY

This program illustrates the use of subroutine DLAY_SR. This subroutine performs a long delay action using TOC1. Although the delay is in double precision (4-hex), the controlling variable, DELAY, is simple precision. This is one of the special features of this subroutine. DELAY controls the higher 2-hex digits. This is how this works:  First, clear the OC1F flag, which is bit 7 in TFLG1. When doing this, we use the OR operation in preserve the other bits in TFLG1.  We load DELAY in accA, which is the higher part of accD. Then, we fill accB, i.e., the lower part of accD, with $00. If DELAY=$XX, then the value in accD is $XX00.  Next, add TCNT to accD and store the result in TOC1. This is the new time  Step in place until OC1F is set. When the number of cycles $XX00 have elapsed, you are done. The subroutine return control to the main program. A stepper motor is controlled by sending binary patterns (step sequences) to its controller board. Eight distinct binary patterns (S0 – S7) are recognized by the stepper motor controller board in your lab. When these patterns are hit in increasing order, one after the other, the motion is called ‘half speed forward’. If only the odd sequences are hit (i.e., every second pattern), the motion is ‘full speed forward’. If the patterns are hit in decreasing order, the motion is, respectively, ‘half-speed backward’ and ‘full speed backward’. The corresponding step sizes are, respectively, +1, +2, -1, -2 (Table 1). Using 2’s complement 8-bit signed numbers convention, these values correspond to the variable STEP taking hex values $01, $02, $ff, $fe. Table 1 Stepper motor speed definitions Step size STEP Half speed forward +1 $ Full speed forward’ +2 $ Half-speed backward -1 $ff Full speed backward’ -2 $fe The program is intended for stepper motor control. The stepper-motor speed is controlled through the delay inserted between steps. A short delay ensures a high speed, while a long delay produces a slow speed. The shortest delay that can be physically accommodated by the stepper motor dynamics is 5 ms. For a 2 MHz processor (0.5 s/cycle), this corresponds to 10,000 ($2710) cycles. The maximum delay is, obviously, $ffff. Since we use a single precision variable, DELAY, we need to round the numbers $2710 and $ffff to 2 significant hex numbers. We get $2800 and $ff00 (To maintain delay

integrity, the first number was rounded up, the second number was rounded down.) Hence, the single precision variable, DELAY, can take values between $28 and $ff.

FLOWCHART AND CODE

The program flowchart is show below. The essential code for this program is shown to the right of the flowchart. The essential code was incorporated into the standard template asm to generate the code file Ex_Long_Delay.asm. Please refer to this file for more coding details, and coding style. Flowchart START Define variables:  (^) DELAY 1 byte Initialize: X=REGBAS  (^) Clear OC1F  (^) Add DELAY to TCNT and store in TOC Jump to subroutine DELAY_SR Step in place until OC1F is set START DLAY_SR SWI RTS Code ORG DATA DELAY RMB 1 ORG PROGRAM START LDX #REGBAS JSR DLAY_SR SWI DLAY_SR LDAA #% ORAA TFLG1,X STAA TFLG1,X LDAA DELAY LDAB #$ ADDD TCNT,X STD TOC1,X LABEL5 LDAA TFLG1,X ANDA #% BEQ LABEL RTS

EXECUTION

Open THRSim11. Close the Commands window. Open and assemble Ex_Long_Delay.asm. View CPU registers, timer registers, memory list for variable DELAY. Arrange windows for maximum benefit. Reset registers. Set standard labels (Label/Set Standard Labels). Press the RESET button. Set breakpoints at SWI and RTS. Put $28 in DELAY. Your screen should look like this:

d) Let the program run. It should loop continuously on LABEL5 while checking for OC1F. When TCNT goes over the value of TOC1, the flag OC1F (bit 7 in TFLG1) gets set and the code is ready to return to the main program. Your screen looks like this: You are done. e) Repeat the above procedure for other values in DELAY: $30, $40, $80 and $ff. Make sure that you understand the way this subroutine works.

WHAT YOU HAVE LEARNED

In this example, you have learned:  A subroutine that can implement a long delay between stepper motor steps  How to achieve double precision delays (4-hex) using single precision (2-hex) variables. The single precision variable DELAY was used. The variable was loaded into the higher part of double accD. The higher part of accD is accA. Hence, the variable was loaded in accA using single precision arithmetic. The lower part of the double precision accumulator accD, which is the single precision accumulator accB, was loaded with $00. Hence, if DELAY=$XX, at the end of the process accD=$XX00.