Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
learn:microprocessor:tutorials:software-timing-delays:start [2017/12/21 19:25] – Marthalearn:microprocessor:tutorials:software-timing-delays:start [2022/10/04 11:38] (current) – external edit 127.0.0.1
Line 3: Line 3:
  
 The purpose of this project is to investigate methods of creating software time delays to pace processor operations. The timing methods that control when a process is to be executed can be implemented by using polling techniques that sample a free running clock or by executing code that requires a fixed amount of time. This project also demonstrates the use of time delays for signal conditioning. The purpose of this project is to investigate methods of creating software time delays to pace processor operations. The timing methods that control when a process is to be executed can be implemented by using polling techniques that sample a free running clock or by executing code that requires a fixed amount of time. This project also demonstrates the use of time delays for signal conditioning.
 +
 +\\
 +<html>
 +<a href="/learn/microprocessor/tutorials/pmodstep-parts-layout/start" class="btn btn-success" role="button">Pmod STEP Parts Layout</a>
 +</html>
 +\\
 +\\
 +<html>
 +<a href="/learn/microprocessor/tutorials/pic32-interrupts-appendices/start" class="btn btn-success" role="button">Determining Delay Constants</a>
 +</html>
 +\\
 +\\
 +<html>
 +<a href="/learn/microprocessor/tutorials/pic32-interrupts-appendices/start" class="btn btn-success" role="button">MPLAB X Stopwatch</a>
 +</html>
 +\\
 +\\
 +<html>
 +<a href="/learn/microprocessor/tutorials/pic32-interrupts-appendices/start" class="btn btn-success" role="button">Time Delay Application</a>
 +</html>
 +\\
 +\\
  
 ---- ----
Line 13: Line 35:
  
 ==== Inventory ==== ==== Inventory ====
-  * 1 [[http://store.digilentinc.com/digilent-pro-mx7-pic32-based-embedded-systems-trainer-board/| Digilent Pro MX7]]+  * 1 [[https://digilent.com/shop/digilent-pro-mx7-pic32-based-embedded-systems-trainer-board/| Digilent Pro MX7]]
   * [[http://www.microchip.com/mplab/mplab-x-ide| MPLAB X IDE]]   * [[http://www.microchip.com/mplab/mplab-x-ide| MPLAB X IDE]]
-  * 1 [[http://store.digilentinc.com/pmod-tph-6-pin-test-point-header/|Pmod TPH]], 1 [[http://store.digilentinc.com/pmod-tph2-12-pin-test-point-header/|Pmod TPH2]], 1 [[http://store.digilentinc.com/pmod-step-stepper-motor-driver/|Pmod STEP]] +  * 1 [[https://digilent.com/shop/pmod-tph-6-pin-test-point-header/|Pmod TPH]], 1 [[https://digilent.com/shop/pmod-tph2-12-pin-test-point-header/|Pmod TPH2]], 1 [[https://digilent.com/shop/pmod-step-stepper-motor-driver/|Pmod STEP]] 
-  * 1 Logic analyzer or oscilloscope (Digilent [[http://store.digilentinc.com/analog-discovery-100msps-usb-oscilloscope-logic-analyzer-limited-time/| Analog Discovery]] or [[http://store.digilentinc.com/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply/| Analog Discovery 2]] recommended)+  * 1 Logic analyzer or oscilloscope (Digilent [[https://digilent.com/reference/test-and-measurement/analog-discovery/start|Analog Discovery]] or [[https://digilent.com/shop/analog-discovery-2-100ms-s-usb-oscilloscope-logic-analyzer-and-variable-power-supply/|Analog Discovery 2]] recommended)
  
 ---- ----
Line 61: Line 83:
  
 It is sufficient to say that each C instruction can require multiple assembly language instructions to implement and each assembly instruction requires time to execute. The bottom line is that the execution of C code requires time and we can use this time to generate predictable time delays. An alternate method to generate a time delay is to use one of the timer resources of the processor. We will investigate both of these two methods of creating a delay function in this project. It is sufficient to say that each C instruction can require multiple assembly language instructions to implement and each assembly instruction requires time to execute. The bottom line is that the execution of C code requires time and we can use this time to generate predictable time delays. An alternate method to generate a time delay is to use one of the timer resources of the processor. We will investigate both of these two methods of creating a delay function in this project.
 +
 +----
  
 ==== Pacing the Computer ==== ==== Pacing the Computer ====
Line 66: Line 90:
  
 Two approaches for developing a delay function will be explored in this project. One of them requires no special hardware but instead relies strictly upon the execution time of a software loop. We will refer to this approach as the software delay method. The other uses one of the processor's built-in timers to generate a delay by counting clock cycles. We call this approach a hardware-assisted software delay method. Delay functions have two controlling interrelated elements: the resolution and the range. The resolution is defined by the smallest possible delay and the range by the longest possible delay. To some extent these parameters are determined by the method chosen to implement the delay. Two approaches for developing a delay function will be explored in this project. One of them requires no special hardware but instead relies strictly upon the execution time of a software loop. We will refer to this approach as the software delay method. The other uses one of the processor's built-in timers to generate a delay by counting clock cycles. We call this approach a hardware-assisted software delay method. Delay functions have two controlling interrelated elements: the resolution and the range. The resolution is defined by the smallest possible delay and the range by the longest possible delay. To some extent these parameters are determined by the method chosen to implement the delay.
 +
 +----
  
 ==== Pure Software Delays ==== ==== Pure Software Delays ====
Line 95: Line 121:
  
 Given that the “mS” variable that specifies the number of milliseconds to delay is an unsigned integer, the range, or longest possible delay, is 4,294,967,295 ms or 49.71 days. The disadvantage of the software delay loop is that the value assigned to COUNTS_PER_MS depends upon the processor speed. If the processor speed changes, then the delay function is no longer calibrated correctly. Given that the “mS” variable that specifies the number of milliseconds to delay is an unsigned integer, the range, or longest possible delay, is 4,294,967,295 ms or 49.71 days. The disadvantage of the software delay loop is that the value assigned to COUNTS_PER_MS depends upon the processor speed. If the processor speed changes, then the delay function is no longer calibrated correctly.
 +
 +----
  
 ==== Calibrating Time for Software Delays ==== ==== Calibrating Time for Software Delays ====
Line 102: Line 130:
  
 An effective alternative for measuring delay time is to use the MPLAB X Stopwatch tool described in the third tab on the right. An effective alternative for measuring delay time is to use the MPLAB X Stopwatch tool described in the third tab on the right.
 +
 +----
  
 ==== Hardware-Assisted Delays ==== ==== Hardware-Assisted Delays ====
Line 239: Line 269:
 Use the LATBINV = LEDA; and LATBINV = LEDB ; instructions to toggle the two LEDs. You need both instructions to implement the two different delay programs. Connecting an oscilloscope or logic analyzer to the test point for LEDB (RB3) will allow you to measure the total delay being called for from the main function. Connecting the oscilloscope / logic analyzer probe to the test point for LEDA (RB2) will measure the millisecond for loop delay but only for the sw_msDelay function. (Remember to connect the oscilloscope or logic analyzer ground to the PmodSTEP ground pin. See the first tab on the top right for the pin locations.) Use the LATBINV = LEDA; and LATBINV = LEDB ; instructions to toggle the two LEDs. You need both instructions to implement the two different delay programs. Connecting an oscilloscope or logic analyzer to the test point for LEDB (RB3) will allow you to measure the total delay being called for from the main function. Connecting the oscilloscope / logic analyzer probe to the test point for LEDA (RB2) will measure the millisecond for loop delay but only for the sw_msDelay function. (Remember to connect the oscilloscope or logic analyzer ground to the PmodSTEP ground pin. See the first tab on the top right for the pin locations.)
  
-Figure 1 is an example of the instrumentation to determine the timing for a 20 ms delay. The LEDA trace toggles each 1 ms resulting in a frequency of 500 Hz. The LEDB trace toggles each 20 ms resulting in a frequency of 25 H+Figure 1 is an example of the instrumentation to determine the timing for a 20 ms delay. The LEDA trace toggles each 1 ms resulting in a frequency of 500 Hz. The LEDB trace toggles each 20 ms resulting in a frequency of 25 Hz. 
 + 
 +{{ :learn:microprocessor:tutorials:software-timing-delays:logicanalyzer1.png?nolink&700 |Figure 1. Screen capture for 20 ms delay.}} 
 + 
 +Figure 1. Screen capture for 20 ms delay. 
 +Screenshot of Digilent WaveForms running on Microsoft Windows 7. 
 + 
 +Complete the data entries for Table 1 using the signal provided by instruments connected to the test point for LEDB and then the MPLAB X Stopwatch tool. When using the MPLAB X Stopwatch tool, place the Stopwatch Start for the first executable line of code in the delay function and set the Stopwatch Stop for the delay function closing brace as noted in the comments for Listing 3 and Listing 4. 
 + 
 +^ Design Total Delay — ms  ^ Software Delay — Measured — ms              |^ Hardware Delay — Measured — ms              || 
 +|                          ^ LEDB                            ^ Stopwatch  ^ LEDB                            ^ Stopwatch  ^ 
 +| 1                        | 1                               | 0.99775    | 1                               | 1.00066    | 
 +| 10                       | 10                              | 9.97491    | 10                              | 10.00359   | 
 +| 100                      | 100                             | 99.74654   | 100                             | 100.03284  | 
 +| 1000                     | 1000                            | Not valid  | 1000                            | Not valid  | 
 + 
 +(Solution values are in red and should be removed prior to distribution to students.) 
 + 
 + 
 +---- 
 + 
 +==== Try It on Your Own! ==== 
 +Now that you've completed this project, you should take a look at the following questions and problems: 
 + 
 +<html> 
 +<ol type="1"> 
 +<li>Report on your observations and conclusions based on the results recorded in Table 1.</li> 
 +</br> 
 +<font color="red">ANSWER: The MPLAB X Stopwatch tool appears to provide better resolution than using the logic analyzer. A high resolution measurement on long delays, at least compared to the degree provided by the Stopwatch, is difficult to obtain using oscilloscopes or logic analyzers. The Stopwatch has an upper limit on how long of a delay can be measured in which cases the logic or oscilloscope is needed.</font> 
 +</ol> 
 +</html> 
 + 
 +<html> 
 +<ol start="2"> 
 +<li>When and why you would use the software delay function written for this assignment and when and why you would implement a software delay function using the core timer?</li> 
 +</br> 
 +<font color="red">ANSWER: Reading the core timer has greater accuracy because the 32-bit core timer has greater resolution and is directly related to the oscillator, which is usually a crystal or resonator. The software loop is a simple delay that can be implemented on any processor. Both delays are dependent on the speed at which the processor is running. 
 + 
 +A couple of the things the software delay method has going for it is that it is easy to understand the concept, and it is easy to write the code to implement. However, beyond the scope of this project, software interrupts can also be preempted by an interrupt. This results in extending the delay period. The disadvantage is that the processor is completely tied up just incrementing a number unless interrupted. The bottom line is that they are simple by consume processor time.</font> 
 +</br></br> 
 + 
 +<li>When creating periodic delays, what determines the stability and accuracy of the delay period?</li> 
 +</br> 
 +<font color="red">ANSWER: When attempting to calibrate the software delay loop by measuring a periodic event (such as toggling a bit), one must consider the time required to execute code outside the delay loop. Longer delays will tend to be more accurate because the time spent in the software loop relative to the time needed to execute code outside the loop is greater, thus having less impact on the total period. 
 + 
 +Should the software delay loop be interrupted, as will be the case in Project 5, then the delay period for pure software delay loops will always be extended. However, the delay period may or may not be extended when the hardware-assisted software delay is used.</font> 
 +</br></br> 
 + 
 +<li>What is the delay reported by the Stopwatch tool in MPLAB X for a one ms delay using the sw_DelayMs function?</li> 
 +</br> 
 +<font color="red">ANSWER: Placing a breakpoint on the first line of code in the sw_msDelay function and the second breakpoint of the last brace of the sw_msDelay function, the stopwatch reports a cycle count of 79820. Based on the processor running at 80 MHz, this equates to 0.000998 ms.</font> 
 +</br></br> 
 + 
 +<li>What determines the resolution for the two methods of implementing polling-based time delay presented in this project?</li> 
 +</br> 
 +<font color="red">ANSWER: For the purely software method, the resolution is one part in the value determined for COUNTS_PER_MS. For the hardware-assisted delay method, it is one part out of the instructions cycle frequency (SYSTEM_FREQUENCY/2). One conclusion is that the lack of range for hardware-assisted delay is compensated for by the increased resolution.</font> 
 +</br></br> 
 + 
 +<li>Why do LEDA and LEDB toggle at the same rate when using the hw_msDelay function?</li> 
 +</br> 
 +<font color="red">ANSWER: The hw_msDelay function does not execute the delay as multiple one millisecond delays. The delay is always a total delay.</font> 
 +</br></br> 
 + 
 +<li>How can the limited range be extended for the hw_msDelay function shown in Listing 4?</li> 
 +</br> 
 +<font color="red">ANSWER: This approach extends the delay range to 4,294,967,295 ms or 49.71 days.</font> 
 +</br></br> 
 + 
 +</ol> 
 +</html> 
 + 
 +{{tag>learn microprocessors tutorial pro-mx7 chipkit-pro pic32 delays software-timing-delays }}