C2000 Piccolo One-Day Workshop Module 3
From Texas Instruments Embedded Processors Wiki
Control Peripherals
ADC Module
Pulse Width Modulation
ePWM
eCAP
eQEP
Lab 3: Control Peripherals
Lab Objective
The objective of this lab is to demonstrate and become familiar with the operation of the on-chip analog-to-digital converter and ePWM. ePWM1A will be setup to generate a 2 kHz, 25% duty cycle symmetric PWM waveform. The waveform will then be sampled with the on-chip analog-to-digital converter and displayed using the graphing feature of Code Composer Studio. The ADC has been setup to sample a single input channel at a 50 kHz sampling rate and store the conversion result in a buffer in the MCU memory. This buffer operates in a circular fashion, such that new conversion data continuously overwrites older results in the buffer.
Two ePWM modules have been configured for this lab exercise:
ePWM1A – PWM Generation
- Used to generate a 2 kHz, 25% duty cycle symmetric PWM waveform
ePWM2 – ADC Conversion Trigger
- Used as a timebase for triggering ADC samples (period match trigger SOC A)
The software in this exercise configures the ePWM modules and the ADC. It is entirely interrupt driven. The ADC end-of-conversion interrupt will be used to prompt the CPU to copy the results of the ADC conversion into a results buffer in memory. This buffer pointer will be managed in a circular fashion, such that new conversion results will continuously overwrite older conversion results in the buffer. The ADC interrupt service routine (ISR) will also toggle LED LD2 on the Piccolo™ ControlSTICK as a visual indication that the ISR is running.
Notes
- ePWM1A is used to generate a 2 kHz PWM waveform
- Program performs conversion on ADC channel A0 (ADCINA0 pin)
- ADC conversion is set at a 50 kHz sampling rate
- ePWM2 is triggering the ADC on period match using SOC A trigger
- Data is continuously stored in a circular buffer
- Data is displayed using the graphing feature of Code Composer Studio
- ADC ISR will also toggle the LED LD2 as a visual indication that it is running
Lab Procedure
Project File
1. A project named Lab3.pjt has been created for this lab. Open the project by clicking on Project → Open… and look in C:\C28x\LABS\LAB3. All Build Options have been configured. The files used in this lab are:
| Adc.c | Gpio.c |
| CodeStartBranch.asm | Lab_2_3.cmd |
| DefaultIsr_3_4.c | Main_3.c |
| DelayUs.asm | PieCtrl.c |
| DSP2802x_GlobalVariableDefs.c | PieVect.c |
| DSP2802x_Headers_nonBIOS.cmd | SysCtrl.c |
| EPwm.c | Watchdog.c |
|
2. Open and inspect Gpio.c by double clicking on the filename in the project window. Notice that the shared I/O pin in GPIO0 has been set for the ePWM1A function. Next, open and inspect EPwm.c and see that the ePWM1 has been setup to implement the PWM waveform as described in the objective for this lab. Notice the values used in the following registers: TBCTL (set clock prescales to divide-by-1, no software force, sync and phase disabled), TBPRD, CMPA, CMPCTL (load on 0 or PRD), and AQCTLA (set on up count and clear on down count for output A). Software force, deadband, PWM chopper and trip action has been disabled. (Note that the last steps enable the timer count mode and enable the clock to the ePWM module). See the global variable names and values that have been set using #define in the beginning of the Lab.h file. Notice that ePWM2 has been initialized earlier in the code for the ADC. Close the inspected files.
Build and Load
3. Click the "Build" button and watch the tools run in the build window. The output file should automatically load.
4. Under Debug on the menu bar click Reset CPU.
5. Under GEL on the menu bar click:
- EMU Boot Mode Select → EMU_BOOT_SARAM.
6. Under Debug on the menu bar click Go Main. You should now be at the start of Main().
Run the Code – PWM Waveform
7. Open a memory window to view some of the contents of the ADC results buffer. To open a memory window click: View → Memory… on the menu bar. The address label for the ADC results buffer is AdcBuf.
|
8. Using a connector wire provided, connect the PWM1A (pin # 17) to ADCINA0 (pin # 3) on the ControlSTICK.
9. Run your code for a few seconds by using the <F5> key, or using the Run button on the vertical toolbar, or using Debug → Run on the menu bar. After a few seconds halt your code by using Shift <F5>, or the Halt button on the vertical toolbar. Verify that the ADC result buffer contains the updated values.
10. Open and setup a graph to plot a 50-point window of the ADC results buffer.
- Click: View → Graph → Time/Frequency… and set the following values:
| Start Address | AdcBuf |
| Acquisition Buffer Size | 50 |
| Display Data Size | 50 |
| DSP Data Type | 16-bit unsigned integer |
| Sampling Rate (Hz) | 50000 |
| Time Display Unit | s |
Select OK to save the graph options.
11. The graphical display should show the generated 2 kHz, 25% duty cycle symmetric PWM waveform. The period of a 2 kHz signal is 500 s. You can confirm this by measuring the period of the waveform using the graph (you may want to enlarge the graph window using the mouse). The measurement is best done with the mouse. The lower left-hand corner of the graph window will display the X and Y-axis values. Subtract the X-axis values taken over a complete waveform period (you can use the PC calculator program found in Microsoft Windows to do this).
Frequency Domain Graphing Feature of Code Composer Studio
12. Code Composer Studio also has the ability to make frequency domain plots. It does this by using the PC to perform a Fast Fourier Transform (FFT) of the DSP data. Let’s make a frequency domain plot of the contents in the ADC results buffer (i.e. the PWM waveform).
- Click: View → Graph → Time/Frequency… and set the following values:
| Display Type | FFT Magnitude |
| Start Address | AdcBuf |
| Acquisition Buffer Size | 50 |
| FFT Framesize | 50 |
| DSP Data Type | 16-bit unsigned integer |
| Sampling Rate (Hz) | 50000 |
Select OK to save the graph options.
13. On the plot window, left-click the mouse to move the vertical marker line and observe the frequencies of the different magnitude peaks. Do the peaks occur at the expected frequencies?
Using Real-time Emulation
Real-time emulation is a special emulation feature that allows the windows within Code Composer Studio to be updated at up to a 10 Hz rate while the MCU is running. This not only allows graphs and watch windows to update, but also allows the user to change values in watch or memory windows, and have those changes affect the MCU behavior. This is very useful when tuning control law parameters on-the-fly, for example.
14. Reset the CPU and select the EMU_BOOT_SARAM boot mode.
15. Enable real-time mode by selecting:
- Debug → Real-time Mode
16. A message box may appear. Select YES to enable debug events. This will set bit 1 (DBGM bit) of status register 1 (ST1) to a "0". The DBGM is the debug enable mask bit. When the DBGM bit is set to "0", memory and register values can be passed to the host processor for updating the debugger windows.
17. The memory and graph windows displaying AdcBuf should still be open. The connector wire between PWM1A (pin # 17) and ADCINA0 (pin # 3) should still be connected. In real-time mode, we would like to have our window continuously refresh. Click:
- View → Real-time Refresh Options…
and check "Global Continuous Refresh". Use the default refresh rate of 100 ms and select OK. Alternately, we could have right clicked on each window individually and selected "Continuous Refresh".
Note: "Global Continuous Refresh" causes all open windows to refresh at the refresh rate. This can be problematic when a large number of windows are open, as bandwidth over the emulation link is limited. Updating too many windows can cause the refresh frequency to bog down. In that case, either close some windows, or disable global refresh and selectively enable "Continuous Refresh" for individual windows of interest instead.
18. Run the code and watch the windows update in real-time mode. Carefully remove and replace the connector wire from PWM1A (pin # 17). Are the values updating as expected?
19. Fully halting the CPU when in real-time mode is a two-step process. First, halt the processor with Debug → Halt. Then uncheck the "Real-time mode" to take the CPU out of real-time mode (Debug → Real-time Mode).
Real-time Mode using GEL Functions
20. Code Composer Studio includes GEL (General Extension Language) functions which automate entering and exiting real-time mode. Four functions are available:
- Run_Realtime_with_Reset (reset CPU, EMU boot mode, enter real-time mode, run CPU)
- Run_Realtime_with_Restart (restart CPU, EMU boot mode, enter real-time mode, run CPU)
- Full_Halt (exit real-time mode, halt CPU)
- Full_Halt_with_Reset (exit real-time mode, halt CPU, reset CPU)
These GEL functions can be executed by clicking:
- GEL → Realtime Emulation Control → GEL Function
If you would like, try repeating the previous step using the following GEL functions:
- GEL → Realtime Emulation Control → Run_Realtime_with_Reset
- GEL → Realtime Emulation Control → Full_Halt
Optional Exercise
You might want to experiment with this code by changing some of the values or just modify the code. Try generating another waveform of a different frequency and duty cycle. Also, try to generate complementary pair PWM outputs. Next, try to generate additional simultaneous waveforms by using other ePWM modules. Hint: don’t forget to setup the proper shared I/O pins, etc. (This optional exercise requires some further working knowledge of the ePWM. Additionally, it may require more time than is allocated for this lab. Therefore, the student may want to try this after the class).
End of Exercise
Lab Reference: Piccolo™ ControlSTICK Header Pin Diagram
| 1 ADC-A7 | 2 ADC-A2 COMP1 (+VE) | 3 ADC-A0 Vref-HI | 4 3V3 |
| 5 ADC-A4 COMP2 (+VE) | 6 ADC-B1 | 7 EPWM-4B GPIO-07 | 8 TZ1 GPIO-12 |
| 9 SCL GPIO-33 | 10 ADC-B6 | 11 EPWM-4A GPIO-06 | 12 ADC-A1 |
| 13 SDA GPIO-32 | 14 ADC-B7 | 15 EPWM-3B GPIO-05 | 16 5V0 |
| 17 EPWM-1A GPIO-00 | 18 ADC-B4 COMP2 (-VE) | 19 EPWM-3A GPIO-04 | 20 SPISOMI GPIO-17 |
| 21 EPWM-1B GPIO-01 | 22 ADC-B3 | 23 EPWM-2B GPIO-03 | 24 SPISIMO GPIO-16 |
| 25 SPISTE GPIO-19 | 26 ADC-B2 COMP1 (-VE) | 27 EPWM-2A GPIO-02 | 28 GND |
| 29 SPICLK GPIO-18 | 30 GPIO-34 (LED) | 31 PWM1A-DAC (Filtered) | 32 GND |
Links to C2000 Piccolo One-Day Workshop
C2000 Piccolo One-Day Workshop Home Page
C2000 Piccolo One-Day Workshop Module 1
C2000 Piccolo One-Day Workshop Module 2
C2000 Piccolo One-Day Workshop Module 3
C2000 Piccolo One-Day Workshop Module 4
Leave a Comment






































