In embedded system development,ADC (Analog-to-Digital Converter) moduleIt is an essential component commonly used to convert external analog signals into digital signals. Nuvoton (Nuvoton Technology) offers the M031 series microcontrollers equipped with high-performance ADC modules and provides multiple complete example codes in their BSP (Board Support Package) to help developers get started quickly.
This article will take you on an in-depth exploration.ADC_SwTrg_TriggerExample: Explain its design purpose, program architecture, and key features to help you quickly grasp the software-triggered ADC technology on the M031.
https://github.com/OpenNuvoton/M031BSP/blob/master/SampleCode/StdDriver/ADC_SwTrg_Trigger/main.c
Example Description: ADC_SwTrg_Trigger
This example demonstrates how to useSoftware TriggerMethod, driving the ADC module of the M031 series MCU to perform analog conversion. This code utilizesSingle ModePerform the conversion operation on channel 2 and finally print the ADC conversion result to the UART terminal.
Development Environment and Hardware Requirements
- Target chipNew Tang M031 Series
- Development environmentKeil MDK / IAR / NuEclipse + GNU
Connection interfaceUSB Virtual COM (UART0)
Key explanation of program structure
System initialization
This code initializes the system clock and UART to ensure subsequent modules can operate correctly.
2. ADC module settings
You can choose between the following two modes via terminal input:
- UseSingle-end Mode
- Set toSingle Conversion Mode
- Enable Channel 2 (usually corresponding to the AIN2 pin).
- UseDifferential Input Mode
- Set toSingle Conversion Mode
Enable Channel 2 (usually corresponding to AIN2 and AIN3 as a pair)
3. Software-triggered start of conversion
Here, the ADC_START_CONV() function is used to issue a software trigger command, and polling is performed to wait for the interrupt flag indicating conversion completion. Once completed, the interrupt status is cleared.
4. Read and display the conversion results
Output the conversion results in both hexadecimal and decimal formats to the terminal, allowing direct observation of the numerical values corresponding to the input voltage.
Practical Tips and Suggestions
- If the development board you are using has variable voltage input components (such as adjustable resistors), you can actually change the input voltage and observe the changes in the ADC values.
- Can be paired with a timer or external interrupt to achieve functionality.Automatically or externally triggered ADC conversion mechanism。
- In the ADC settings, if higher precision or additional features (such as comparators or interrupt callbacks) need to be enabled, you can refer to other examples for further extension.