STM32CUBEMX(9)--ADC reads by polling, USART prints

Publisher:BlissfulDreamsLatest update time:2024-05-17 Source: elecfansKeywords:STM32CUBEMX Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

insert image description here

Select chip model

Use STM32CUBEMX to select the chip stm32f030r8 as follows:

insert image description here

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:

insert image description here

Configuring the Clock Tree

The maximum main frequency of STM32F0 is 48M, so configure 48:

insert image description here

Serial port configuration

This experiment uses serial port 1 for serial communication, and the baud rate is configured to 115200.

insert image description here

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:

insert image description here

Generate project settings

Please note that Chinese characters cannot appear in the production project settings, otherwise an error will be reported.

insert image description here

Generate code

insert image description here

Configure Keil

insert image description here

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.

insert image description here


Keywords:STM32CUBEMX Reference address:STM32CUBEMX(9)--ADC reads by polling, USART prints

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

Design of automotive laser detection and ranging system based on ADC technology
LIDAR (Light Detection and Ranging) is used for large-scale positioning, ranging and target profiling applications through radar. This system consists of a laser that can emit pulsed or continuous laser light within the required range and a high-speed, low-noise receiver for analysis of the reflected signal. The emi
[Embedded]
Design of automotive laser detection and ranging system based on ADC technology
[STM32] ADC library functions, general steps, detailed examples: internal temperature sensor
STM32F1xx official information: "STM32 Chinese Reference Manual V10" - Chapter 11 Analog/Digital Conversion (ADC) "STM32 Chinese Reference Manual V10" - Chapter 11 Section 11.10 Temperature Sensor ADC sampling value How to use the STM32 ADC module to get the actual voltage value connected to the ADC pin? What va
[Microcontroller]
[STM32] ADC library functions, general steps, detailed examples: internal temperature sensor
Wandering between bans and reality: Can domestic analog-to-digital conversion ADCs become stronger?
Praised as the jewel in the crown of chips, the importance of ADC (Analog-to-Digital Converter) has never been shaken in decades. Entering the era of AIoT and 5G, due to the increasing demand for interaction with the physical world, the status of ADC as the core of the signal chain is steadily improving. For the Chine
[Mobile phone portable]
Wandering between bans and reality: Can domestic analog-to-digital conversion ADCs become stronger?
ams-OSRAM launches new 256-channel ADC to help high-performance CT detectors reduce power consumption and simplify design
ams-OSRAM launches new 256-channel ADC to help high-performance CT detectors reduce power consumption and simplify design • The AS5911 in a 14mm x 14mm FBGA package is a system-in-package solution that performs current-to-digital conversion of 256 photodiode outputs; • 1.25mW per channel power consumption min
[Medical Electronics]
ams-OSRAM launches new 256-channel ADC to help high-performance CT detectors reduce power consumption and simplify design
STM8 ADC initialization + data collection
ADC initialization function: void Adc_Initialize(void) {     ADC2_Init(ADC2_CONVERSIONMODE_SINGLE,ADC2_CHANNEL_8,ADC2_PRESSEL_FCPU_D2,                         ADC2_EXTTRIG_TIM,DISABLE,ADC2_ALIGN_RIGHT,ADC2_SCHMITTTRIG_CHANNEL8,DISABLE);     ADC2_Cmd(ENABLE);      } in: ADC2_CONVERSIONMODE_SINGLE: ADC conversion
[Microcontroller]
STM8 ADC initialization + data collection
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号