Tiva Lab 11: Measure Pulse Width of PWM Signals using Timer

 

Objective

  • To learn about timer input capture to measure period and pulse width
  • To calculate the frequency and duty cycle

Required Reading Material

Background Information

The timer system in the TI Tiva microcontroller can be used for a variety of purposes. This lab will use the "Input Capture" ("Edge-Time") functionality, which is used to measure the length of a pulse. Timer input capture can be used for many features, such as:

  1. Measuring the width of a pulse
  2. Measuring the duty cycle of a signal
  3. Measuring the frequency of a signal

In this lab, you have to develop a code using the timer capture to measure the frequency and duty cycle values for several PWM signals and then display them on the LCD screen.

Fundamentals of Frequency Measurement

There are two methods of estimating the frequency of the square wave. The number of signal transitions (rising, falling, or both) in a fixed time interval can be counted, or the time interval between two consecutive signal transitions can be measured using a timer.

  • For a high-frequency signal, measuring frequency by counting transitions over short intervals is more accurate for signals.
  • Measuring the period is generally more precise fo relatively low-frequency signals. The resolution is provided by the number of timer counts between input signal transitions. For very low-frequency signals, may result in excessively long delays between measurement updates.

In this lab, you will learn how to use a timer to capture the rising and falling edges of a PWM signal, the difference between the capture values drives the pulse width time duration.

Required Components List

 

Button Small 64 Onboard Switch (SW1) x 1
CharacterLcdDisplay s Character LCD Module x 1
LED Red 64 Onboard LED x 1

 

Circuit Diagram

The following diagram shows the schematic of the design that uses the timer input capture to capture the rising and falling edges. The verification uses the PWM to generate the PWM signal — the input to the timer input capture pin.

measureFreqDutyonLCD s

 

Procedure

Before the Lab, you must decide which ports and pins to use in this lab. Some pins are used to connect the LCD module, and one is an input pin for timer input capture; two are used to generate PWM output signals — one is connected to the Timer Input Capture pin, and the other is connected to the on-board LED. This lab will also use the onboard switch to change the PWM frequency and duty cycle.

 

Add EzTivaLIB to Your Project

The results will be shown on the LCD screen. Therefore, you have to include EzTivaLIB in your project. Please follow the article "Lesson 05: Branches" to add the library to your project.

 

The System Clock

A function Setup_123G_80MHz() or Setup_1294_80MHz() must be called at the beginning of the main() to configure the system clock to 80 MHz.

PWM signals

Develop a code that generates two PWM signals (both use the same PWM configuration):

  • The frequency of the PWM timer is 10 MHz
  • Generates Right-Aligned PWM signal
  • The LOAD and CMP values have been defined in the template code

Timer Input Capture

Configure a general-purpose timer to operate as a 16-bit timer input capture mode with the pre-scale factor of 0xFF (255). This timer will be used to capture the time when the input signal transitions from low to high (rising edge), or high to low (falling edge).

Do not use the Timer5 (T5CCP0); it is used for the hardware timer delay functions — timer_waitMicros() and timer_waitMillis().

Write a C code that uses the Input Capture (Edge Time) feature of the Timer to find the period, then calculate the duty cycle and frequency of the PWM signal.

Period Measurement

Develop code to capture the main timer values corresponding to the period of one cycle:

  • Configure the timer input capture event on the rising edge (or falling edge)
  • Clean the bit of event status
  • Capture the timer count value on the first rising edge (or falling edge)
  • Clean the bit of event status
  • Capture the timer counts value on the second edge (same edge as the first captured)
  • Calculate the period counts value (T)

periodMeasurement
Figure 11-1: Period measurement by capturing two consecutive edges

Pulse Width Measurement

Develop code to capture the rising and falling edges

  • Configure the timer input capture event on the rising edge
  • Clean the bit of event status
  • Capture the timer count value on the rising edge
  • Configure the timer input capture event on the falling edge
  • Clean the bit of event status
  • Capture the timer count value on the falling edge
  • Calculate the pulse with counter value (Δt)

pulseWidthMeasurement
Figure 11-2: Pulse-width measurement using input capture

Duty Cycle Calculation

Then, you can get the value for the duty cycle and the frequency.

dutyCycleMeasurement

Figure 11-3: Definition of Duty Cycle

 

Configurations

Pin Configurations

DevicePort.Pin Signal TypePCTLDirectionDrive Mode

 

  1. Connect a wire from the PWM output pin to the timer input capture pin.
  2. Start a new project (or re-use the project from the previous parts), and load the template code from the CSULA Canvas website.
  3. Fill in the configuration tables above. Then, according to the configuration tables, use the register settings for PWM, Timer, and GPIO ports.

 

Lab Experiment

Experiment 1:

Fill in the table below with the results from the calculation and the LCD screen displayed: (The output signals are Right-Aligned PWM)

  1. Calculates the PWM output frequency and duty cycle values.
  2. Download your code to the Tiva LaunchPad, and press the "RESET" button to start your code.
  3. The LCD screen will show the signal frequency and duty cycle through the timer input capture. Record the values in the table.
  4. Press and hold the onboard switch (for 1 sec) to re-configure the PWM signal, and then repeat steps 3 and 4 for each PWM signal.

Table 1: The Measurement Result

PWM Signal Output The Results are shown on the LCD Screen
LOAD Value CMP Value Frequency (Hz)
= (10M / LOAD)
Duty (%)
= (CMP / LOAD) x 100%)
Frequency (Hz) Duty (%)
0 18150 4628
1 6665 3032
2 12490 8118
3 49996 37497
4 8328 2914

 

  

Questions

  1. Complete Table 1, which shows the results from the experiments and calculations.
  2. Compare your results with the values you calculated in Table 1. Calculate the percent error for each PWM signal's frequency and duty cycle values.
    \(\% error = \left| {\frac{{({\rm{actual value}}) - ({\rm{experimental value}})}}{{{\rm{actual value}}}}} \right| \times 100\% \)
  3. Can we configure the timer input capture event on both edges mode to measure the pulse width of the input PWM signal? Please explain why or why not.
  4. Briefly describe the "Input Capture" function.
  5. How often will the 16-bit timer roll over if it uses the default system clock of 16MHz and a Prescaler factor of 0x0F?
  6. What register is used to select the type of events that will trigger a TimerA Input Capture?
  7. What register stores the current counter value for Timer A?

 


Exercises

 

Ex1: Edge-Count to Measure the Frequency of PWM signals

Develop an embedded system program that utilizes the Edge Count and Periodic modes of the Timer to count rising or falling edges in a PWM signal pulse train for a duration of 10 seconds. Employ an additional pin connected to the onboard LED. Initially, at t = 0, activate the LED and enable the Edge Count system to track rising or falling edges in the PWM signal pulse train. Once t = 10 seconds is reached, disable the LED and cease counting the specified edges. Upon completion, display the counter value on the LCD screen. Press the onboard switch to initiate a new counting cycle and modify the PWM signal.

 

Question:

  1. Is it possible to generate a square wave (50% duty cycle) over a period of 10 hours using the Periodic mode of the 16-bit Timer? If yes, explain how, and if no, explain why not. You do not need to write a program or flow chart to justify your answer.

Submit your completed project report, including your working code.