This code example demonstrates the ADC range detection feature of the SAR ADC module. This feature is useful to monitor whether the converted result of ADC voltage is within the required range.
The device used in this code example (CE) is:
The board used for testing is:
- TRAVEO™ T2G Cluster 6M Lite Kit (KIT_T2G_C-2D-6M_LITE)
In this example, the ADC range detection feature is configured to trigger an interrupt whenever the ADC output voltage goes out of the range marked by user-selected threshold voltages. The onboard potentiometer is used to provide input voltage level to the selected ADC channel. The onboard User LED1 is switched on whenever the ADC input voltage goes out of range.
SAR ADC
TRAVEO™ T2G features a successive approximation register analog-to-digital converter (SAR ADC), The SAR ADC is designed for applications that require a moderate resolution and high data rate. It consists of the following blocks:
- SARADC Core
- SARMUX
- SAR sequencer
- Diagnostic reference
- Reference buffer
SARMUX is an analog multiplexer to connect the signal sources to the ADC input. The SARADC core then perform the analog-to-digital conversion. A SAR sequencer is responsible for prioritizing the trigger requests, enabling the appropriate analog channel, and controlling the sampling.
TRAVEO™ T2G platform supports the following ADC features:
- Three SAR A/D converters with up to 96 external channels
- Each ADC supports 12-bit resolution and sampling rates of up to 1 Msps
- Each ADC also supports six internal analog inputs like
- Bandgap reference to establish absolute voltage levels
- Calibrated diode for junction temperature calculations
- Two AMUXBUS inputs and two direct connections to monitor supply levels
- Each ADC has a sequencer supporting the autonomous scanning of configured channels
- Synchronized sampling of all ADCs for motor-sense applications
ADC Range Detection
The SAR sequencer supports an optional range detection feature. Range detection enables a check against up to two programmable threshold values without CPU involvement.
Range detection is defined by two 16-bit threshold values and a mode field selecting one of four possible modes.
Both the mode PASSx_SARy_CHz_POST_CTL.RANGE_MODE and the two thresholds PASSx_SARy_CHz_RANGE_CTL.RANGE_LO and PASSx_SARy_CHz_RANGE_CTL.RANGE_HI are configured per channel.
The available range detection modes are:
- BELOW_LO (RESULT < RANGE_LO)
- INSIDE_RANGE (RANGE_LO ≤ RESULT < RANGE_HI)
- ABOVE_HI (RANGE_HI ≤ RESULT)
- OUTSIDE_RANGE (RESULT < RANGE_LO) || (RANGE_HI ≤ RESULT)
Range detection uses the 16-bit PASSx_SARy_CHz_RESULT.RESULT from the "left/right align" step. This means that the threshold values need to be in the same format as the PASSx_SARy_CHz_RESULT.RESULT after all the preceding post-processing steps (including averaging). The event flag will be set when the range mode condition evaluates to true.
When the event flag is set, the PASSx_SARy_CHz_INTR.CH_RANGE interrupt will be set and a pulse is output on the range violation trigger.
More details can be found in:
- TRAVEO™ T2G CYT4DN
This CE has been developed for:
- TRAVEO™ T2G Cluster 6M Lite Kit (KIT_T2G_C-2D-6M_LITE)
Figure 1. KIT_T2G_C-2D-6M_LITE (Top View)
The onboard potentiometer can be used to define a voltage level connected to an ADC input channel.
Please refer to the board schematic for more details on how the potentiometer is connected to the ADC.
A UART serial connection should be made with the computer via the serial port (KitProg3 connector). Use a terminal emulator to see the output on the screen. In the terminal emulator settings, select the KitProg3 COM port and set the baud rate to 115200.
In this design, an onboard potentiometer is used to provide input to the SAR ADC. The input voltage ranges from 0V to 5V. The main function displays the available options for the threshold values and the range detection mode on the terminal program. The user can then enter the corresponding keys to make a selection.
When the voltage is out of the range chosen by the user, an interrupt is triggered and the onboard User LED1 is turned on. Alternatively, the signal can also be measured on P7[5] pin.
STDOUT/STDIN setting
Initialization of the GPIO for UART is done in the cy_retarget_io_init() function.
- Initializes the pin specified by CYBSP_DEBUG_UART_TX as UART TX and the pin specified by CYBSP_DEBUG_UART_RX as UART RX (these pins are connected to KitProg3 COM port)
- The serial port parameters are 8N1 and 115200 baud
GPIO pin configuration
The User LED1 is configured to output mode. This LED is later turned on to indicate voltage out of range.
The POT port pin which is connected to the onboard potentiometer is configured as analog input and connected to the configured ADC channel.
SAR ADC initialization
SAR ADC instance and channel are configured by calling Cy_SAR2_Init() with appropriate arguments using macros and configuration structure cy_stc_sar2_config_t configured by Device Configurator:
ModusToolbox™ Device configurator
This code example project can be created or imported using ModusToolbox™ IDE. To configure the project in the Quick Panel, click on Device Configurator
Figure 2. Select Device Configurator
User input
Using UART serial communication, a list of available reference voltages is displayed on the terminal emulator. The user can enter the corresponding key to choose a pair of defined upper and lower threshold voltage levels for ADC range detection. In addition, the different ADC range detection modes can be selected.
Code example main loop
The endless loop of the code example in main() always checks whether a character has been received from the user via UART (terminal) using Cy_SCB_UART_Get(). The user interface supports numeric values ranging from 0 - 5 and alphabetic characters a, b, i and o. Any other key input will be ignored.
The character received is then used to set the ADC range parameters such as the detection mode, the upper and the lower threshold values by calling the function configureAdcRangeDetectionMode(). In this function the ADC is temporarily disabled by calling Cy_SAR2_Disable(), then the PASSx_SARy_CHz_RANGE_CTL.RANGE_HI and PASSx_SARy_CHz_RANGE_CTL.RANGE_LO and PASSx_SARy_CHz_POST_CTL.RANGE_MODE fields are written by the PDL driver API Cy_SAR2_Init() for the configured ADC instance and channel. The SAR ADC block is then re-enabled using the API Cy_SAR2_Enable(). Next, the handleAdcIrq() function is setup as an interrupt handler and the system and CPU interrupts are enabled by calling Cy_SysInt_Init() and NVIC_EnableIRQ(). As a last step, the channel interrupt is configured using Cy_SAR2_Channel_SetInterruptMask() and the A/D conversion is triggered by calling the API Cy_SAR2_Channel_SoftwareTrigger(). The ADC will be re-triggered by hardware in continuous mode.
A/D conversion
The ADC is configured in continuous mode, which means that, since it is triggered once the SAR ADC is retriggering the following conversions autonomously. The clock and conversion timing is set with parameters that every 1 microsecond a new ADC conversion is done.
When an ADC range event is detected the interrupt callback function handleAdcIrq() is called where a flag variable will be set. The main loop will set the User LED1 port pin to high when an event has been detected or otherwise to low. This means the User LED1 is illustrating whether there is an ADC range detection event. The signal can also be measured on P7[5] pin.
Before testing this code example:
- Power the board through the dedicated power connector
- Connect the board to the PC through the USB interface
- Build the project using the dedicated Build button
or by right-clicking the project name and selecting "Build Project"
- To program the board, in the Quick Panel, scroll down, and click [Project Name] Program (KitProg3_MiniProg4)
For this example, a terminal emulator is required for displaying outputs and to change settings during runtime. Install a terminal emulator if you do not have one. Instructions in this document use Tera Term.
After code compilation, perform the following steps for flashing the device:
-
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
-
Open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
-
Program the board using one of the following:
- Select the code example project in the Project Explorer.
- In the Quick Panel, scroll down, and click [Project Name] Program (KitProg3_MiniProg4).
-
After programming, the code example starts automatically. Confirm that the messages are displayed on the UART terminal:
A list of upper and lower threshold voltage levels is displayed. Also, different range detection modes, outside range, inside range, above upper threshold and below lower threshold. The user can select an option by entering the corresponding character.
-
You can debug the example to step through the code. In the IDE, use the [Project Name] Debug (KitProg3_MiniProg4) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
Note: (Only while debugging) On the CM7 CPU, some code in main() may execute before the debugger halts at the beginning of main(). This means that some code executes twice: once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main(). See KBA231071 to learn about this and for the workaround.
Expected behavior
For illustration purposes, different ADC range detection configurations are selected. For each test, a triangle waveform signal (0 - 5V) was applied to the ADC input (ADCin in oscilloscope pictures below). However, similar results can also be achieved by using the potentiometer. The implementation ensures that the User LED1 is set to high each time the range detection event has been triggered or to low if the event stops to occur.
-
Test 1 - Outside Range Detection: Upper threshold = 3.5V, lower threshold = 1.5V
Whenever ADCin is above 3.5V OR below 1.5V the OuterRangeDetection signal (User LED1) pin is set to high. -
Test 2 - Inside Range Detection: Upper threshold = 3.5V, lower threshold = 1.5V
Whenever ADCin is between 1.5V AND 3.5V the InnerRangeDetection signal (User LED1) pin is set to high. -
Test 3 - Above Upper Threshold Detection: Upper threshold = 3.5V
Whenever ADCin is above 3.5V the AboveUpperThDetection signal (User LED1) pin is set to high. -
Test 4 - Below Lower Threshold Detection: Lower threshold = 1.5V
Whenever ADCin is below 1.5V the BelowLowerThDetection signal (User LED1) pin is set to high.
Relevant Application notes are:
- AN235305 - Getting started with TRAVEO™ T2G family MCUs in ModusToolbox™
- AN219755 - USING A SAR ADC IN TRAVEO™ T2G AUTOMOTIVE MICROCONTROLLERS
ModusToolbox™ is available online:
Associated TRAVEO™ T2G MCUs can be found on:
More code examples can be found on the GIT repository:
For additional trainings, visit our webpage:
For questions and support, use the TRAVEO™ T2G Forum: