Docsity

Investigating Software Time Delays: Polling Techniques & Delay Functions, Lab Reports of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE 341 Lab 2
ECE 341 Lab 2
Software Timing
Revised : September 5. 2008
Purpose
The purpose of this lab is to investigate methods of creating software time delays using
polling techniques.
Concepts
How long does it take?
Each instruction that the processor executes requires finite time. Take for example the program
statement written in C to write to an I/O port:
WrPortI(PBDDR, &PBDDRShadow, PBDDRShadow|0x82);
This instruction is implemented as a sequence of assembler language instructions:
Listing 1. Assembler code generated for WrPortI C instruction.
WrPortI(PBDDR, &PBDDRShadow, PBDDRShadow|0x82);
1aae 2AC3C6 ld hl, (0xC6C3) 11
1ab1 2600 ld h, 0x00 4
1ab3 EB ex de, hl 2
1ab4 218200 ld hl, 0x0082 6
1ab7 EC or hl, de 2
1ab8 7D ld a, l 2
1ab9 ED56 ipset 1 4
1abb D3324700 ioi ld (0x0047), a 11
1abf 32C3C6 ld (0xC6C3), a 10
1ac2 ED5D ipres 4
1ac4 EF rst 0x28 10
The last number listed for each assembler instruction lists the number of CPU instruction clock cycles
required for execution. For this particular write port instruction the total number of clock cycles is 66.
There are two crystal cycles for each instruction clock cycle and for the RCM 3000 processors, the period
of the clock is 68ns. Hence, the write port instruction requires a total of approximately 4.5 s to execute.
The execution time required any particular C instruction is determined by the complexity of the C
statement, the assembler code that the C instruction is translated into, and the speed that the processor is
running.
Pacing the computer
Generally, microprocessor base systems are designed to operate with time to spare. Code must be
written that allows the rest of the world to catch up. This is done using time delay routines. Say, for
1
pf3
pf4
pf5

Partial preview of the text

Download Investigating Software Time Delays: Polling Techniques & Delay Functions and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

ECE 341 Lab 2

Software Timing

Revised : September 5. 2008

Purpose

The purpose of this lab is to investigate methods of creating software time delays using

polling techniques.

Concepts

How long does it take?

Each instruction that the processor executes requires finite time. Take for example the program statement written in C to write to an I/O port: WrPortI(PBDDR, &PBDDRShadow, PBDDRShadow|0x82); This instruction is implemented as a sequence of assembler language instructions: Listing 1. Assembler code generated for WrPortI C instruction. WrPortI(PBDDR, &PBDDRShadow, PBDDRShadow|0x82); 1aae 2AC3C6 ld hl, (0xC6C3) 11 1ab1 2600 ld h, 0x00 4 1ab3 EB ex de, hl 2 1ab4 218200 ld hl, 0x0082 6 1ab7 EC or hl, de 2 1ab8 7D ld a, l 2 1ab9 ED56 ipset 1 4 1abb D3324700 ioi ld (0x0047), a 11 1abf 32C3C6 ld (0xC6C3), a 10 1ac2 ED5D ipres 4 1ac4 EF rst 0x28 10 The last number listed for each assembler instruction lists the number of CPU instruction clock cycles required for execution. For this particular write port instruction the total number of clock cycles is 66. There are two crystal cycles for each instruction clock cycle and for the RCM 3000 processors, the period of the clock is 68ns. Hence, the write port instruction requires a total of approximately 4.5 s to execute. The execution time required any particular C instruction is determined by the complexity of the C statement, the assembler code that the C instruction is translated into, and the speed that the processor is running.

Pacing the computer

Generally, microprocessor base systems are designed to operate with time to spare. Code must be written that allows the rest of the world to catch up. This is done using time delay routines. Say, for

example, I need to delay one second. How can I do this knowing that the microprocessor always wants to be executing the next line of code? One way is to call a function that does nothing but kill time. A delay routine has two attributes: how many units of time are to be delayed and what constitutes a unit of time. Common units of time are related to cycles of the CPU crystal or milliseconds. The RCM3000 unit uses a 14.7456MHz crystal with a processor clock doubler. As stated above, the state clock frequency is the one half crystal frequency. Hence the clock frequency is the same as the crystal frequency. Dynamic has three built in timing global variables: MS_TIMER, SEC_TIMER, and TICK_TIMER. (See text section 6.6.3, pg 204). A delay function call also be written based upon the expected time to execute code. This is frequently accomplished by executing a loop that simply decrements the value stored in a variable. Such a loop would look like: for (i = 0; i< NUM; i++); where NUM is a constant value. A delay function of length 65535 ms or less (including zero) can be implemented as: void delay_ms(unsigned int ms) { #define NUM_MS ???? // Value to be determined by trial and error int i; while(ms--) { for (i = 0; i< NUM_MS; i++); } } Delay functions of longer durations can be generated by calling the “delay_ms” function more than once. Calibrating Time Delay How accurate is the program listed on page 204 of the text? How can this be determined? If you use the “delay_ms” function listed above, what value should be used for NUM_MS and how accurate is the delay? For the “delay_ms” function, you can look at the assembler code and add up the execution times. You can also add software code to make the delay time observable by toggling a pin on the microprocessor. This requires that external instrumentation be connected to the pin of the board connected to that I/O port for the bit being toggled. Suitable instrumentation for this test include frequency counters and oscilloscopes. Now one must consider the appropriate place to insert the code to appropriately set the IO pin high or low realizing the inserting of such code may affect the accuracy of the delay function. It is advisable to place the instrumentation code where it will be executed the minimum number of times. Assignment You are to write two separate programs that implements the two different software delay functions described above. One program uses the delay listed on page 204 of the text and the other program uses the software delay function listed above. You must determine an appropriate value for the NUM_MS constant in the program listed above. (See Appendix I.) Add the bit toggling code to instrument the two different delay programs such that PG7 will measure the total delay being called for from the main function and PG6 will measure the millisecond delay. No other bits can be altered when toggling bits 6 and 7 of port G for timing purposes. This means that you must use

1. Explain why and when you would use the software delay function written for this

assignment and when you would implement a software delay function using MS_TIMER

described in the text.

2. When creating periodic delays, what determines the stability and accuracy of the delay

period?

Appendix I Equation for determining the Delay Constant NUM_MS: Constant that determines the number of for loop iterations to generate a one millisecond delay. NUM_MS_previous: Previous estimate or computed value of NUM_MS T_actual: Actual delay time measure by monitoring Port G pin 6 T_desired: 1 ms – the desired loop delay Step 1: Pick an initial value for NUM_MS and inset this value into the “#define NUM_MS” statement of the delay_ms function. (A good number to try is 100.) Step 2: Compile and execute the Dynamic C program Step 3: Measure the high or low period of the square wave observed with an oscilloscope that is connected to Port G pin 6. (Remember to connect the oscilloscope common to the project board ground pin.) T_actual equals the period measured. Step 3 alternate: Measure the frequency of the square wave observed with a frequency counter that is connected to Port G pin 6. (Remember to connect the oscilloscope common to the project board ground pin.) T_actual equals 1/(2 * F_maeasured) Step 4. Using the T_actual found in Step 3 or Step 3 alternate and NUM_MS_previous equal to the value on NUM_NS used in the program, apply Eq. 1 or Eq. 2 to compute a new NUM_MS NUM_MS = (1 ms/ T_actual) * NUM_MS_previous (Eq. 1) NUM_MS = (2*F_measured/1000) * NUM_MS_previous (Eq. 2) Step 5: Repeat Steps 2 though 4 until T_actual is 1 ms or NUM_MS equals NUM_MS_previous Appendix II Read-Modify-Write Instructions Read-modify-write instructions are used whenever the value of some but not all of the bits of a byte, integer, or long variable is to be changed while the remaining bits are to be unchanged. The “BitWrPortI” command provided by Dynamic C provides the means to modify single bits. For example, to set Port G, bit 4 to zero, the following statement can be used.

BitWrPortI(PGDR, &PGDRShadow,1, 4); (C1) The state of the bit is the third argument in the argument list and the bit number is the fourth argument in the list. Remember, bits are numbered from 0 (least significant bit) to 7 (most significant bit). When the processor executes the code in statement C1, a read-modify-write operation is implicitly executed. The second argument in the argument list of C1 is a global variable assigned by the C compiler and is passed by reference. The variable contains an exact copy of the data written to the output port. You cannot directly read the data previously written to an output port using the “RdPortI” instruction. The statement C2 is functionally equivalent to C1. WrPortI(PGDR, &PGDRShadow, (PGDRShadow | 0x10)); (C2) Notice that the hexadecimal value for a bit in the fourth position is 0x10. In order to set a bit to zero, one can use statement C3 or C4. Notice that the bit wise “and” operator is used in C4 instead of the bitwise logic “or” operator used in C2. Also note that a one’s complement operation (“~”) is performed on the constant 0x10 that results in a hexadecimal value of 0xEF. BitWrPortI(PGDR, &PGDRShadow,0, 4); (C3) WrPortI(PGDR, &PGDRShadow, (PGDRShadow & ~0x10)); (C4) Statement C5 can be used to be toggle a bit regardless of its original value. WrPortI(PGDR, &PGDRShadow, (PGDRShadow ^ 0x10)); (C6) Statement C6 is logically equivalent to the following sequence of instructions. if(PGDRShadow & 0x10 == TRUE) (C7) { BitWrPortI(PGDR, &PGDRShadow,0, 4); (C8) } else (C9) { BitWrPortI(PGDR, &PGDRShadow,1, 4); (C10) } If there are more than one of the bits that need to be modified and the new states of the bits are a combination of ones and zeros, then the two operations are required. Initially, one must know the field of bits that are to be modified. We call the collection of bits a mask that will either hide bits we don’t want to change or will enable us to modify the bits we do want to change. For example, if we want to set bits 5, 6, and 7 low and bit 4 high, the mask value is 0xF0. Using the a constant MASK set to 0xF0 to modify the four most significant bits to the binary value 0001XXXX, the following lines of code are used: PGDRShadow &= ~MASK; (C11) WrPortI(PGDR, &PGDRShadow, (PGDRShadow | 0x10)); (C12) Statement C11 sets the four most significant bits in the shadow register to zero. Statement C12 sets the appropriate bits to one for both the shadow register and the output port. The output pins change only