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

We will measure the frequency and duty cycle values for several PWM signals, and 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 PWM signal — the input to the timer input capture pin.

measureFreqDutyonLCD s

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.

Fills the following table and write into the report.

Pin Connection and Configuration

Pin Connection and Configuration

    • FIll the Pin Configuration table based on the circuit diagram:
      Device.
      Connector
      Port.Pin  Signal Type PCTL Direction: Drive Mode Interrupt 
      Enable Trigger Mode

Add EzTivaLIB into Your Project

The results will be showed on the LCD screen, therefore, you have to include EzTivaLIB into your project. Please follow the article "Lesson 09: Add ezTiva Library into Your Project" to add the library into your project.

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 code that generates two PWM signals (both using the same 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 timer delay functions — timer_waitMicros() and timer_waitMillis().

Write code that use the Input Capture (Edge Time) feature of the Timer to find the period, duty cycle and frequency of a 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)

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 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)

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

Duty Cycle Measurement

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

dutyCycleMeasurement

Figure 11-3: Definition of Duty Cycle

GPIO Configurations

Fills the following tables, and then develop code to configure the GPIO ports.

GPIO Initialization and Configuration

GPIO Initialization Configuration

Next, we need to configure all the GPIO ports and pins that are used in the designd.

According to the pin connections, complete the following GPIO configurations for each port. Fills the pin field by the value below:

  • 0: Clean the bit
  • 1: Set the bit
  • x: Do not change the bit
  • d: Do not care

For both TM4C123GXL and TM4C1294XL LaunchPads, the Port C [3:0] are used for JTAG/SWD. Therefore, when you configure the Port C, you have to use bitwise operators to make sure your new configuration settings do not affect the JTAG/SWD function (PC3 ~ PC0).

Most of GPIO pins are configured as GPIOs and tri-stated by default (GPIOPCTL = 0, CPIOAFSEL = 0, GPIODIR = 0, GPIOPUR = 0, GPIOPDR = 0, GPIOODR = 0)

    1. Enable Clock to the GPIO Modules (RCGCGPIO register)
      TM4C123G: SYSCTL->RCGCGPIO binary =  hex
      8 4 2 1   8 4 2 1  
      7 6 5 4   3 2 1 0 bit
      Port F Port E Port D Port C Port B Port A port
      0 0 -  

      TM4C1294: SYSCTL->RCGCGPIO =  binary =  hex
      8 4 2 1   8 4 2 1   8 4 2 1   8 4 2 1  
      15 14 13 12   11 10 9 8   7 6 5 4   3 2 1 0 bit
      Port Q Port P Port N   Port M Port L Port K Port J   Port H Port G Port F Port E   Port D Port C Port B Port A port
      0 - - -  
      After enable clock signal, check the PRGPIO register until the correspoding bit set to 1.
      In Assembly:
                  LDR     R0, =SYSCTL_PRGPIO_R
      Wait4GPIO   LDR     R1, [R0]
                  TST     R1, #(____)
                  BEQ     Wait4GPIO
      In c:
      while ( (SYSCTL->PRGPIO & ____ ) != ____ ) {};
    2. Unlock Port
      TM4C123GPD7 and PF0 are locked after reset.
      TM4C1294: PD7 and PE7 are locked after reset
      If those pins are used in the design, it must be unlocked first. To unlock the port, 0x4C4F434B must be written into GPIOLOCK register and uncommit it by setting the GPIOCR register.
          8 4 2 1   8 4 2 1            
          7 6 5 4   3 2 1 0 bit          
      Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
      -           -         =   GPIO->LOCK = 0x4C4F434B
      - - = GPIO->CR
      -           -         =   GPIO->LOCK = 0x4C4F434B
      - - = GPIO->CR

     

    Convert above configuration into registers

     

      1. GPIO Analog Mode Select
        If any pin is used as an Analog signal (check Signal Type field on the table 1), the appropriate bit in AMSEL must be set.
        • 0: Digital signal
        • 1: Analog signal
            8 4 2 1   8 4 2 1            
            7 6 5 4   3 2 1 0 bit          
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
        - - = GPIO->AMSEL
      2. GPIO Port Control (PCTL)
        The PCTL register is used to select the specific periheral signal for each GPIO pin when using the alternate function mode.
        • 0: GPIO
        • 1~0xF: Check the GPIO Pins and Alternate Function table
        Check the PMCx bit field in the PCTL register from "Lesson 11: GPIO Port Control Register (GPIOPCTL) ".
            8421 8421 8421 8421   8421 8421 8421 8421            
            31~28 27~24 23~20 19~16   15~12 11~8 7~4 3~0 bit           
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin  Value in Hex   Register   Value to Register
        - - = GPIO->PCTL
        - - = GPIO->PCTL
        - - = GPIO->PCTL
        - - = GPIO->PCTL
        - - = GPIO->PCTL
        - - = GPIO->PCTL
        - - = GPIO->PCTL
      3. GPIO Alternate Function Select (AFSEL)
        Setting a bit in AFSEL register configures the corresponding GPIO pin to be controlled by PCTL peripheral function.
        • 0: General I/O
        • 1: Pin connected to the digital function that defined in PCTL register

            8 4 2 1   8 4 2 1            
            7 6 5 4   3 2 1 0 bit          
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
        - - = GPIO->AFSEL
      4. GPIO Pin Direction (DIR)
        Set pin direction
        • 0: Input pin
        • 1: Output pin

            8 4 2 1   8 4 2 1            
            7 6 5 4   3 2 1 0 bit          
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
        - - = GPIO->DIR
        - - = GPIO->DIR
        - - = GPIO->DIR
        - - = GPIO->DIR
        - - = GPIO->DIR
        - - = GPIO->DIR
        - - = GPIO->DIR
      5. Internal Pull-Up Resistor (PUR), Pull-Down Resistor (PDR), and Open-Drain (ODR)
        PUR: The pull-up control register
        PDR: The pull-down control register
        ODR: The open-drain control register
        • 0: Disable
        • 1: Enable

            8 4 2 1   8 4 2 1            
            7 6 5 4   3 2 1 0 bit          
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
        - - = GPIO->
        - - = GPIO->
        - - = GPIO->
        - - = GPIO->
        - - = GPIO->
        - - = GPIO->
        - - = GPIO->
      6. GPIO Digital Enable
        Enables all the pins that are used in the design, including GPIO pins and alternate function pins.
        • 0: Pin undriven
        • 1: Enable pin

            8 4 2 1   8 4 2 1            
            7 6 5 4   3 2 1 0 bit          
        Port   Pin 7 Pin 6 Pin 5 Pin 4 - Pin 3 Pin 2 Pin 1 Pin 0 pin Value in Hex   Register   Value to Register
        - - = GPIO->DEN 
        - - = GPIO->DEN 
        - - = GPIO->DEN 
        - - = GPIO->DEN 
        - - = GPIO->DEN 
        - - = GPIO->DEN 
        - - = GPIO->DEN 

       

      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.

      Lab Experiments

      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, presses "RESET" button to start your code.
      3. The LCD screen will show the signal frequency and duty cycle through the timer input capture. Records the values in the table.
      4. Press and hold the on-board switch (for 1 sec) to re-configure the PWM signal, and then repeat step 3 and 4 for each PWM signal.
      # PWM Signal Output The Results show 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. Compare your results with the values you calculated. Calculate the percent error about the frequency and duty cycle values for each PWM signal.
        \% error = \left| {\frac{{({\rm{actual value}}) - ({\rm{experimental value}})}}{{{\rm{actual value}}}}} \right| \times 100\%
      2. Can we configure the timer input capture event on the both edges mode to measure the pulse-width of input PWM signal ? Please explain why, or why not.
      3. Briefly describe the "Input Capture" function.
      4. How often will 16-bit timer rolls over if it uses the default system clock of 16MHz and a prescaler factor of 0x0F?
      5. What register is used  to select the type of events that will trigger a TimerA Input Capture?
      6. What register stores the current counter value for Timer A?

       


      Exercises

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

      Write a program that uses the Edge Count and Periodic modes of the Timer to count the number of rising or falling edges in the pulse train from the PWM signals in 10 seconds. Configure another pin that is already connected to the onboard LED, At time, t = 0, the LED should be turn on and the Edge Count system should be enabled to count the rising or falling edges in the pulse train from the PWM signal. After time t = 10 sec., the LED should be off and the pulse accumulator should stop counting the number of specified edges. Display the counter value on the LCD screen at the end of the program. Press onboard switch to chnage the PWM signal and start a new counting.

      Question:

      1. Is it possible to generate a square wave (50% duty cycle) of period 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.

       

       

      Go to top