Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Microcontroller Education for Mechanical Engineering - Example RPM-2 | EMCH 367, Study notes of Mechanical Engineering

Material Type: Notes; Professor: Giurgiutiu; Class: MICROCNTROLLRS MECH ENGR; Subject: Mechanical Engineering; University: University of South Carolina - Columbia; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 10/01/2009

koofers-user-1mv
koofers-user-1mv 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EMCH 367 Fundamentals of Microcontrollers Example RPM 2
EXAMPLE RPM_2
OBJECTIVE
This example has the following objectives:
Review the used of two time captures to calculate the time duration of an event
Illustrate the use of scaling factors (100s) to deal with numbers that are larger than the
microcontroller word length
Illustrate the handling of overflow counts, with special attention to the situation when the two time
captures are on one side and another of a timer overflow (time-change line)
Introduce the method of calculating multiplication through repeated additions and
decrementation of a counter
Introduce the use of a conversion subroutine to convert from hex to BCD
Illustrate rotation speed calculation from rotation period.
Illustrate the use of scaling factors (100s) to present the result on a display with limited number
of digits.
PROGRAM EX_RPM_2
This program calculates the rotation speed N_100 in 100s rpm and converts it to BCD format.
The program assumes that the times T1 and T2 (in cycles), as well as the number of overflows,
NOF, have been separately captured and are in memory.
To preserve sign information during computation, the program works in hundreds of cycles.
Hence, the variables T1_100 and T2_100 are calculated by dividing T1 and T2 by decimal 100.
Then, the program calculates the time taken by the overflows, TOVFL. This is performed by
repeated addition of the duration between 2 overflows, $10000/100, and decrementation of NOF
(Note that the duration between two overflows was also divide by decimal 100, since the
program works in hundreds of cycles.)
The total time, T_100, is calculated by adding T2_100+TOVFL and then subtracting T1_100.
The rotation speed N_100_HEX, is calculated by dividing the decimal number
2000000*60/100/100 by the total time, T_100. The result is in hex.
Finally, the subroutine HEX_BCD is invoked to obtain N_100_BCD, i.e., the rotation speed in
BCD format. Note that the subroutine is stored separately in the used root of the directory of your
floppy disk and is included at assembly time using the directive #INCLUDE 'A:\HEX_BCD.ASM'
To run this example, you have to enter some arbitrary values of T1, T2 and NOF in the appropriate
memory locations. Two numerical examples are given next:
Example 1: T2>T1
Data: NOF=1, T1=$0006, T2=$110c
Results: T1_100=$0000, T2_100=$002b, TOVFL=$028f, T_100=$02ba, N_100_HEX=$0011,
N_100_BCD=17
The rotation speed is 1700 rpm.
Example 2: T2<T1
This example gives the same time duration as Example1, but the individual times were shifted to
straddle the time-change line.
Dr. Victor Giurgiutiu Page 111/29/2020
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Microcontroller Education for Mechanical Engineering - Example RPM-2 | EMCH 367 and more Study notes Mechanical Engineering in PDF only on Docsity!

EXAMPLE RPM_

OBJECTIVE

This example has the following objectives:

 Review the used of two time captures to calculate the time duration of an event

 Illustrate the use of scaling factors (100s) to deal with numbers that are larger than the

microcontroller word length

 Illustrate the handling of overflow counts, with special attention to the situation when the two time

captures are on one side and another of a timer overflow (time-change line)

 Introduce the method of calculating multiplication through repeated additions and

decrementation of a counter

 Introduce the use of a conversion subroutine to convert from hex to BCD

 Illustrate rotation speed calculation from rotation period.

 Illustrate the use of scaling factors (100s) to present the result on a display with limited number

of digits.

PROGRAM EX_RPM_

This program calculates the rotation speed N_100 in 100s rpm and converts it to BCD format.

 The program assumes that the times T1 and T2 (in cycles), as well as the number of overflows,

NOF, have been separately captured and are in memory.

 To preserve sign information during computation, the program works in hundreds of cycles.

Hence, the variables T1_100 and T2_100 are calculated by dividing T1 and T2 by decimal 100.

 Then, the program calculates the time taken by the overflows, TOVFL. This is performed by

repeated addition of the duration between 2 overflows, $10000/100, and decrementation of NOF

(Note that the duration between two overflows was also divide by decimal 100, since the

program works in hundreds of cycles.)

 The total time, T_100, is calculated by adding T2_100+TOVFL and then subtracting T1_100.

 The rotation speed N_100_HEX, is calculated by dividing the decimal number

2000000*60/100/100 by the total time, T_100. The result is in hex.

 Finally, the subroutine HEX_BCD is invoked to obtain N_100_BCD, i.e., the rotation speed in

BCD format. Note that the subroutine is stored separately in the used root of the directory of your

floppy disk and is included at assembly time using the directive #INCLUDE 'A:\HEX_BCD.ASM'

To run this example, you have to enter some arbitrary values of T1, T2 and NOF in the appropriate

memory locations. Two numerical examples are given next:

Example 1: T2>T

Data: NOF=1, T1=$0006, T2=$110c

Results: T1_100=$0000, T2_100=$002b, TOVFL=$028f, T_100=$02ba, N_100_HEX=$0011,

N_100_BCD=

The rotation speed is 1700 rpm.

Example 2: T2<T

This example gives the same time duration as Example1, but the individual times were shifted to

straddle the time-change line.

Data: NOF=2, T1=$fffd, T2=$

Results: T1_100=$028f, T2_100=$002b, TOVFL=$051e, T_100=$02ba, N_100_HEX=$0011,

N_100_BCD=

The rotation speed is 1700 rpm.

FLOWCHART AND CODE

The program flowchart is show below. Two flowchart levels are presented: the big-picture and the

details. The big-picture is used to understand the overall architecture of the program. The details are

used to explain some of the blocks. (Details are given only for those blocks which are somehow new,

and have not been used in previous programs.) This flowchart was use to generate the code file

Ex_RPM_2.asm using the standard asm template.

Define variables START Divide T1 by 100 and store in T1_ Divide T2 by 100 and store in T2_ Use repeated addition to calculate TOVFL (time taken by overflows, in 100s of cycles) T_100=TOVFL+T2_100-T1_ SWI N_100_HEX=2000000*60/100/100/T_ Convert N_100_HEX to N_100_BCD  (^) Load lower part of N_100_HEX in accA  (^) JSR HEX_BCD  (^) STAA N_100_BCD Load $0000 in accD Store accD to TOVFL NOF=0? N LABEL5 Load TOVFL in accD Add ($10000)/ Store accD into TOVFL Decrement NOF Branch back if NOF> LABEL Y Load T1 in accD Load 100 in X IDIV Load T2 in accD Load 100 in X IDIV Overflow counter, NOF1 (1 byte)  1 st^ capture, T1 (2 bytes)  2 nd^ capture, T2 (2 bytes) T1_100=T1/100 (2 bytes) T2_100=T2/100 (2 bytes) T_100=(total time)/100 (2 bytes) N_100_HEX=N in 100 rpm (hex) (2 bytes) N_100_BCD=N in 100 rpm (BCD) (1 byte) Big-picture Flowchart Detailed Flowcharts

Please refer to the appendix for the explanation of sub routine HEX_BCD.

T1_100 = T1/100 = $0006/$64 = $0000 (1a)

T2_100 = T2/100 = $110c/$64 = $002b (1b)

TOVFL = NOF($10000)/100 = $01$028f = $028f (1c)

T_100= TOVFL + T2_100 – T1_100 = $028f + $002b - $0000 = $02ba (1d)

Note that the result of Equation (1d) is the same as that of equation (1). Hence, Equations (2) and (3)

still stand.

These arithmetic operations are illustrated, step-by-step next.

a) Put in memory the data from Example 1 above: NOF=1, T1=$0006, T2=$110c.Your memory list

should look like Table 1. Note the number of bytes (memory locations) that these variables take:

NOF=1 byte, T1=2 bytes, T2=2 bytes (highlighted).

Table 1 Memory list after Example 1 data has been entered NOF $0000 $ T1 $0001 $ $0002 $ T2 $0003 $ $0004 $0c T1_100 $0005 $ff $0006 $ff T2_100 $0007 $ff $0008 $ff TOVFL $0009 $ff $000a $ff T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

b) Step through the section of your program that divides the variables T1 and T2 by decimal 100 to

obtain T1_100 and T2_100. Use the hex pocket calculator to verify these results. The memory

list should look like Table 2. Note that T1_100 and T2_100 also take 2 bytes each.

Table 2 Memory list after the calculation of T1_100 and T2_ NOF $0000 $ T1 $0001 $ $0002 $ T2 $0003 $ $0004 $0c T1_100 $0005 $ $0006 $ T2_100 $0007 $ $0008 $2b TOVFL $0009 $ff $000a $ff T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

c) Step through the section of your program that calculates TOVFL, the time taken by overflows,

expressed in 100s of cycles. Note that, initially, TOVFL=$0000 is stored. Then, if NOF0, the

variable TOVFL is increased by ($10000)/100, while the variable NOF is decremented by one.

The process is repeated as long as NOF0. At the end of the process, the memory list should

look like Table 3. Note that TOVFL also takes 2 bytes. Also not that, at the end of this process,

NOF = 0.

Table 3 Memory list after the calculation of TOVFL NOF $0000 $ T1 $0001 $ $0002 $ T2 $0003 $ $0004 $0c T1_100 $0005 $ $0006 $ T2_100 $0007 $ $0008 $2b TOVFL $0009 $ $000a $8f T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

d) Step through the section of your program that calculates T_100, the total time in 100s of cycles.

Note how the formula

T_100= TOVFL + T2_100 – T1_100 = $028f + $002b - $0000 = $02ba (1d)

is applied in two steps, first the addition, then the subtraction. At the end of the process, the

memory list should look like Table 4.

Table 4 Memory list after the calculation of T_ NOF $0000 $ T1 $0001 $ $0002 $ T2 $0003 $ $0004 $0c T1_100 $0005 $ $0006 $ T2_100 $0007 $ $0008 $2b TOVFL $0009 $ $000a $8f T_100 $000b $ $000c $ba N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

EXAMPLE 2

Recall data from Example 2 as listed at the beginning of this document: NOF=2, T1=$fffd, T2=$1103.

Note that this data represents the same time interval as in Example 1, only that a backwards time shift

of 9 cycles took place. For this reason, the times T1 and T2 are on different sides of the time-change

line. T1 happens before the time change, T2 happens after. Therefore, between T1 and T2, the time

counter TCNT goes through a timer overflow. Hence, the overflow counter has the value NOF=2,

instead of 1 as in Example 1.

Use your pocket hex calculator to estimate the result to be expected at the end of the program:

T_100 = (NOF$10000+T2-T1)/100 = ($02$10000+$1103-$fffd)/$64 = $02ba (1)

N_100_HEX = (2000000*60/100/100/T_100) = $2ee0/$2ba = $0011 (2)

N_100_BCD = 17 (3)

Because the program needs to work with a finite word size, the variables in Equation (1) are first divided

by decimal100, such that Equation (1) is actually treated as:

T_100 = NOF*($10000)/100+T2/100-T1/

The terms of equation (1) are calculated separately, as TOVFL, T1_100, T2_100. Hence, the following

operations are performed:

T1_100 = T1/100 = $fffd/$64 = $028f (1a)

T2_100 = T2/100 = $1103/$64 = $002b (1b)

TOVFL = NOF($10000)/100 = $02$028f = $051e (1c)

T_100= TOVFL + T2_100 – T1_100 = $051e + $002b - $028f = $02ba (1d)

Note that the result of Equation (1d) is the same as that of equation (1). Hence, Equations (2) and (3)

still stand.

These arithmetic operations are illustrated, step-by-step, next.

a) Put in memory the data from Example 1 above: NOF=2, T1=$fffd, T2=$1103. Your memory list

should look like Table 7. Note the number of bytes (memory locations) that these variables take:

NOF=1 byte, T1=2 bytes, T2=2 bytes (highlighted).

Table 7 Memory list after Example 2 data has been entered NOF $0000 $ T1 $0001 $ff $0002 $fd T2 $0003 $ $0004 $ T1_100 $0005 $ff $0006 $ff T2_100 $0007 $ff $0008 $ff TOVFL $0009 $ff $000a $ff T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff

N_100_BCD $000f $ff

b) Step through the section of your program that divides the variables T1 and T2 by decimal 100 to

obtain T1_100 and T2_100. Use the hex pocket calculator to verify these results. The memory

list should look like Table 8. Note that T1_100 and T2_100 also take 2 bytes each.

Table 8 Memory list after the calculation of T1_100 and T2_ NOF $0000 $ T1 $0001 $ff $0002 $fd T2 $0003 $ $0004 $ T1_100 $0005 $ $0006 $8f T2_100 $0007 $ $0008 $2b TOVFL $0009 $ff $000a $ff T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

c) Step through the section of your program that calculates TOVFL, the time taken by overflows,

expressed in 100s of cycles. Note that, initially, TOVFL=$0000 is stored. Then, if NOF0, the

variable TOVFL is increased by ($10000)/100, while the variable NOF is decremented by one.

The process is repeated as long as NOF0. At the end of the process, the memory list should

look like Table 9. Note that TOVFL also takes 2 bytes. Also not that, at the end of this process,

NOF = 0.

Table 9 Memory list after the calculation of TOVFL NOF $0000 $ T1 $0001 $ff $0002 $fd T2 $0003 $ $0004 $ T1_100 $0005 $ $0006 $8f T2_100 $0007 $ $0008 $2b TOVFL $0009 $ $000a $1e T_100 $000b $ff $000c $ff N_100_HEX $000d $ff $000e $ff N_100_BCD $000f $ff

d) Step through the section of your program that calculates T_100, the total time in 100s of cycles.

Note how the formula

T_100= TOVFL + T2_100 – T1_100 = $051e + $002b - $028f = $02ba (1d)

is applied in two steps, first the addition, then the subtraction. At the end of the process, the

Table 12 Memory list after the calculation of N_100_HEX NOF $0000 $ T1 $0001 $ $0002 $ T2 $0003 $ $0004 $0c T1_100 $0005 $ $0006 $ T2_100 $0007 $ $0008 $2b TOVFL $0009 $ $000a $8f T_100 $000b $ $000c $ba N_100_HEX $000d $ $000e $ N_100_BCD $000f $ WHAT YOU HAVE LEARNED

In this example, you have learned:

 The use of two time captures to calculate the time duration of an event

 The use of scaling factors to deal with numbers that are larger than the microcontroller word

length. In the program, the scale factor decimal 100 was used, such that the cycle counts T1 and

T2 were converted to T1_100 and T2_100, which are measured in 100s of cycles.

 The handling of overflow-counts, with special attention to the situation when the two time

captures are on one side and another of a timer overflow (time-change line). In the first

numerical example, T1 and T2 were on the same side of the time-change line. In the second

numerical example, T1 and T2 were on opposite sides of the time-change line (T1 before, T

after). As a result, the number of overflows recorded in the second case was greater by 1 than in

the first case. However, with proper handling, the final result of the computation was the same.

 The calculation of multiplication through repeated additions and decrementation of a counter.

The time taken by overflows, TOVFL, was calculated through repeated addition of the length

between two consecutive overflows. Every time an addition was done, the overflow counter,

NOF, was decremented by one. The loop was exited when NOF=0. This method was necessary

since the multiplication opcode MUL available in set does not contain a.

 Introduce the use of a conversion subroutine to convert from hex to BCD

 Illustrate rotation speed calculation from rotation period.

 Illustrate the use of scaling factors (100s) to present the result on a display with limited number

of digits.