Overview
This chapter uses an analog-to-digital converter (ADC) to sample multiple ADC channel voltages in a polling manner.
DMA sampling has been done before, please see:
https://blog.csdn.net/qq_24312945/article/details/106557538
Hardware Preparation
First you need to prepare a development board. Here I prepare the NUCLEO-F030R8 development board:
Select chip model
Use STM32CUBEMX to select the chip stm32f030r8 as follows:
Configuring the Clock Source
HSE and LSE are external high-speed clock and low-speed clock respectively. In this article, the built-in clock source is used, so the Disable option is selected for both, as shown below:
Configuring the Clock Tree
The maximum main frequency of STM32F0 is 48M, so configure 48:
Serial port configuration
This experiment uses serial port 1 for serial communication, and the baud rate is configured to 115200.
ADC Configuration
In STM32f030, there is an ADC (Analog/Digital Converter), each ADC has 12-bit, 10-bit, 8-bit and 6-bit options, and each ADC has 16 external channels, 2 internal channels and one VBAT channel signal.
This article will open three channels of ADC, IN0, IN1, and IN15, to read ADC respectively. Since serial port 2 is multiplexed with IN2 and IN3, IN2 and IN3 are not used. The configuration is as follows:
Generate project settings
Please note that Chinese characters cannot appear in the production project settings, otherwise an error will be reported.
Generate code
Configure Keil
Code
In main.c, add the header file. If you do not add it, an error message "identifier "FILE" is undefined" will appear.
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
Function declaration and serial port redirection:
/* USER CODE BEGIN PTD */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PTD */
Variable definition:
/* USER CODE BEGIN 0 */
uint8_t in;
uint16_t adcBuf[3]; //Store ADC
/* USER CODE END 0 */
Main loop:
copy
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
i=0;
while(i<3)
{
HAL_ADC_Start(&hadc);//Start ADC
HAL_ADC_PollForConversion(&hadc,0xffff);//Wait for the conversion to complete. The second parameter indicates the timeout in ms.
//HAL_ADC_GetState(&hadc1) is used to obtain the ADC status. HAL_ADC_STATE_REG_EOC indicates that the conversion is complete and the conversion data is available.
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc),HAL_ADC_STATE_REG_EOC))//It is to determine whether the conversion completion flag is set. HAL_ADC_STATE_REG_EOC indicates the conversion completion flag. The conversion data is available
{
//Read ADC conversion data, the data is 12 bits. According to the data sheet, the register stores conversion data in 16 bits, and the data is right-aligned, so the conversion data range is 0~2^12-1, that is, 0~4095.
adcBuf [ i ] = HAL_ADC_GetValue ( & hadc ) ;
printf("
adc%d=%4.0d,voltage=%1.4f",i,adcBuf[i],adcBuf[i]*3.3f/4096);
i++;
}
}
HAL_ADC_Stop(&hadc);
HAL_Delay(500);
}
/* USER CODE END 3 */
Demonstration effect
Set adc0 port to 3.3V, adc1 port to GND, adc15 port to 1.5V, the output is as follows.
Previous article:Detailed explanation of the application of SD NAND SDIO on STM32 (Part 2)
Next article:Strawberry picking robot control system based on Gizwits Cloud platform + STM32 + Raspberry Pi
Recommended ReadingLatest update time:2024-11-16 09:30
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- "Power amplifier experimental case" application of power amplifier in forward and reverse motion of ultrasonic motor
- Share: [Zhongke Blue News] AB32VG1 Review SDIO (File System)
- Design of audio products based on DSP
- 【Chuanglong Technology Allwinner A40i Development Board】Performance Comprehensive Test
- The idea of single chip microcomputer time-sharing control
- TI white paper "IQ: What is IQ and how to use it"
- Which manufacturers produce 16-bit microcontrollers with 251 cores?
- Totem pole in circuit,,,
- PA Test Solution
- Creative Modification Competition: The moderator gave me two booster boards, and I used both of them!