This code example shows how the conversion results of the SAR ADC change depending on the various conversion output methods.
The device used in this code example (CE) is:
The board used for testing is:
- TRAVEO™ T2G evaluation kit (KIT_T2G-B-H_EVK, KIT_T2G-B-H_LITE)
- TRAVEO™ T2G Body Entry Lite evaluation kit (KIT_T2G-B-E_LITE)
In this example, the results of various ADC conversion output methods (Averaging, Alignment, Sign Extension) are executed. The KIT potentiometer is set as the ADC input and the conversion is performed. The results are sent to the terminal each time according to the user's input, which includes the average count and the output format.
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; SARADC core then performs analog-to-digital conversion.
A SAR sequencer is responsible for prioritizing the triggers requests, enable the appropriate analog channel, and control the sampling.A single-ended SAR ADC system is capable of scanning up to 40 analog inputs (32 GPIOs and eight internal signals).
More details can be found in:
- TRAVEO™ T2G CYT4BF Series
- TRAVEO™ T2G CYT2BL Series
This CE has been developed for:
-
TRAVEO™ T2G evaluation kit (KIT_T2G-B-H_EVK)
-
TRAVEO™ T2G Body High Lite evaluation kit (KIT_T2G-B-H_LITE)
-
TRAVEO™ T2G Body Entry Lite evaluation kit (KIT_T2G-B-E_LITE)
The example uses the default configuration of the board. Refer to the kit user guide to verify that the board is properly configured.
SAR ADC Configurations
In this example of code, A/D conversion is implemented with PDL(Peripheral Driver Library) APIs in configure_SAR_ADC().
- Cy_SAR2_Init() to initialize the ADC module and set the reference buffer with band gap voltage. The configurations are included in the structure CE_SAR2_config which is configured using Device Configurator:
- Note that the clock settings for A/D conversion are done in Device Configurator as well, and are reflected automatically in cybsp_init() call in advance
- Then Cy_SAR2_SetReferenceBufferMode() sets ePASS MMIO reference buffer mode
- After initializing channels, set the interrupt to occur at the end of the last channel conversion. Cy_SAR2_Channel_SetInterruptMask() is called to use the interrupt factor CY_SAR2_INT_GRP_DONE and the interrupt handler handle_SAR_ADC_IRQ() is registered by Cy_SysInt_Init(). Finally, calling NVIC_SetPriority() and NVIC_EnableIRQ() to enable the interrupt. For more details about interrupt handling, plese refer Handling Interrupts from pdl_api_reference_manual
- The A/D convertion is triggered by the software by calling Cy_SAR2_Channel_SoftwareTrigger()
The interruption generated when the conversion of the two channels terminates is managed by handle_SAR_ADC_IRQ().
- Firstly, the function obtains the conversion status via the Cy_SAR2_Channel_GetInterruptStatus() API. Then it clears the interrupt flags by Cy_SAR2_Channel_ClearInterrupt()
- This function specifies how the conversion results must be processed based on the different output formats in the current configuration. According to the calculations, it shows the value of the raw conversion result and the voltage value on the terminal
- In addition to the above, it reflects the new configuration specified by the user and the new configuration is performed by calling the configure_SAR_ADC() feature
Please refer here for detailed explanation of PDL API usage for SAR ADC.
Miscellaneous settings
-
STDIN / STDOUT setting
-
Calling cy_retarget_io_init() function to use UART as STDIN / STDOUT
- Initialize the port defined as CYBSP_DEBUG_UART_TX as UART TX and CYBSP_DEBUG_UART_RX as UART RX (these pins are connected to KitProg3 COM port)
- The serial port parameters are 8N1 and 115200 baudrate
-
cyhal_uart_getc() returns the user input from the terminal as received data
-
Before testing this code example:
- Connect the board to your PC using the provided USB cable through the KitProg3 USB connector
- Build the project using the dedicated Build button
or by right-clicking the project name and selecting "Build Project"
For this example, a terminal emulator is required to display outputs and receive keys pressed. You can install a terminal emulator if you do not have one. In this example, Tera Term was used as the terminal emulator.
After code compilation, perform the following steps to flash the device:
- 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
- Specify the output format and average count by pressing corresponded key as shown in below figure
Figure: Terminal Output
- Rotate the potentiometer to change the ADC input voltage. Based on user input, the results will be displayed in the terminal window
You can debug the example to step through the code. In the IDE, use the <Application 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.
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: