# 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

## 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 Onboard Switch (SW1) x 1 Character LCD Module x 1 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. ## Procedure

Before Lab, you must decide which ports and pins to be used 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. The on-board switch also will also be used in this lab to change the PWM frequency and duty cycle.

#### The System Clock

A function Setup_123G_80MHz() or Setup_1294_80MHz() must be called at the beginning of the main(), in order 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 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 to 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 calculates duty cycle and frequency of the PWM signal.

##### Period Measurement

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

• Configure the timer input capture event on rising-edge (or falling edge)
• Clean the bit of event status
• Capture the timer counts value on first rising edge (or falling edge)
• Clean the bit of event status
• Capture the timer counts value on second edge (same edge as the first captured)
• Calculate the period counts value (T) 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 counts value on the rising edge
• Configure the timer input capture event on falling edge
• Clean the bit of event status
• Capture the timer counts value on the falling edge
• Calculate the pulse with counter value (Δt) 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. Figure 11-3: Definition of Duty Cycle

#### Configurations

Write down the following configuration information into your lab report.

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 CSULA Moodle website.
3. Fill in the configuration tables above. Then, according to the configuration tables, use the register settings for PWM, Timer, and GPIO ports.

## Questions

1. Complete the Table 1 which shows the results from the experiments and calculations.
2. Compare your results with the values you calculated in the Table 1. Calculate the percent error about the frequency and duty cycle values for each PWM signal. 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 16-bit timer rolls 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?