当前位置:网站首页>Blue Bridge Cup embedded Hal library ADC
Blue Bridge Cup embedded Hal library ADC
2022-07-28 10:49:00 【[email protected]】
List of articles
The experiment purpose
adopt LCD Display the voltage value , And by rotating R37 Change the voltage value
ADC Related knowledge
STM32 HAL Library Learning Series No 1 piece ADC To configure And DAC To configure
Hardware design
Schematic diagram


STM32CubeMX To configure
1、 Pin selection PB15 Measure the voltage 
2、 Clock source selection HSE
3、 Set the clock frequency . stay HCLK Place input 80 enter 
4、 To configure ADC Corresponding GPIO Pin . Only aliases are added here R37
5、 choose ADC2 The passage of 15 Measure the voltage .
6、ADC Parameter configuration . The clock prescaler coefficient is set to “2 Asynchronous clock mode of frequency division ” , Others use the default configuration .

7、 Set the project name 、 Select compile software 
8、 Only reference the required library files ; Generate separate .c and .h file .
9、 The generated code 
Keil To configure
1、 add to LCD Related documents . Provide the match point resource package fonts.h and lcd.h Added to the new project Inc In the folder
route :D:\MX_project\ADC2\Core\Inc

Provide the match point resource package lcd.c Added to the new project Src In the folder
route :D:\MX_project\ADC2\Core\Src






2、 Downloader selection and related configuration 



software design
1、main.c Add header file to .( notes : Put all the code in BEGIN and END Between , Prevent overwriting the original code when regenerating the code )
#include "stdio.h"
#include "string.h"
#include "lcd.h"
2、 obtain ADC Function of voltage value 
uint16_t getADC(void)
{
uint16_t adc = 0;
HAL_ADC_Start(&hadc2); // Polling mode , It needs to be opened continuously in the cycle
adc = HAL_ADC_GetValue(&hadc2); // Read ADC Value
return adc;
}


3、 Define a character array to store voltage values 
char buf[20];
4、LCD Initialization and other processing ( Want to know more about LCD, Please have a look at LCD piece )
LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line0, (uint8_t *)" ");
LCD_DisplayStringLine(Line1, (uint8_t *)" ");
LCD_DisplayStringLine(Line2, (uint8_t *)" ADC Test ");
LCD_DisplayStringLine(Line3, (uint8_t *)" ");
LCD_DisplayStringLine(Line4, (uint8_t *)" ");
LCD_SetBackColor(White);
LCD_SetTextColor(Blue);
LCD_DisplayStringLine(Line5, (uint8_t *)" ");
LCD_DisplayStringLine(Line6, (uint8_t *)" ADC2 CH15 ");
LCD_DisplayStringLine(Line7, (uint8_t *)" ");
LCD_DisplayStringLine(Line8, (uint8_t *)" ");
LCD_DisplayStringLine(Line9, (uint8_t *)" ");
5、 Poll to read the voltage value , And display the voltage value to LCD On
ADC use 12 Bit resolution ,2^12=4096
Voltage max :3.3V
Obtained voltage value *3.3/4096 = Decimal voltage value 
LCD_SetTextColor(Red);
sprintf(buf, " VAL:%.2fV", getADC()*3.3/4096);
HAL_Delay(100);
LCD_DisplayStringLine(Line8, (uint8_t *)buf);
compile 、 erase 、 download
1、 Compiler Engineering 
Compile successfully 
2、 Erase program 
Erase succeeded 
3、 Download program 
perhaps 
Download successful 
Function position
ADC


HAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef *hadc);
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef *hadc);
ADC initialization
static void MX_ADC2_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {
0};
/** Common config */
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; // The clock frequency division //1 frequency division ,ADCCLK=PCLK2/1=
hadc2.Init.Resolution = ADC_RESOLUTION_12B; //ADC The resolution of the ,12 Bit mode
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; // Right alignment , low 12 It works
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE; // Turn off scan mode , That is, single channel is effective
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE; // Single conversion mode , Turn off continuous conversion
hadc2.Init.NbrOfConversion = 1; // The number of channels is 1
hadc2.Init.DiscontinuousConvMode = DISABLE; // Prohibit discontinuous sampling mode
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; // Software triggers
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; // Use software to trigger
hadc2.Init.DMAContinuousRequests = DISABLE; // close DMA request
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel */
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
LCD


void LCD_Init(void);
void LCD_SetTextColor(vu16 Color);
void LCD_SetBackColor(vu16 Color);
void LCD_Clear(u16 Color);
void LCD_DisplayStringLine(u8 Line, u8 *ptr);
Experimental phenomena
By spinning R37 The potentiometer can change the voltage 
problem
The maximum voltage is only 3.23V. I don't know whether it's an error or something . If you know the reason, please advise .
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281025238880.html
边栏推荐
- C language input string with spaces
- 蓝桥杯嵌入式-HAL库-USART_TX
- ACM winter vacation training 4
- 蓝桥杯嵌入式-HAL库-SYSTICK
- GKPerlinNoiseSource
- 20200229 training race L2 - 2 tree species Statistics (25 points)
- GKCheckerboardNoiseSource
- Inverse element & combinatorial number & fast power
- 20200229 training match L1 - 2 delete the substring in the string (20 points)
- GKNoiseMap
猜你喜欢

7、MapReduce自定义排序实现

GKCheckerboardNoiseSource

安装office自定义项 安装期间出错 解决办法

蓝桥杯嵌入式-HAL库-ADC

Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award

爱可可AI前沿推介(7.28)

粒子群实现最优解的求解

PyQt5快速开发与实战 4.13 菜单栏、工具栏与状态栏 and 4.14 QPrinter

ICML 2022 | graph represents the structure aware transformer model of learning

Excel word simple skills sorting (continuous update ~)
随机推荐
Andorid 开发三 (Intent)
Invalid ROM Table原因及解决办法
Using k-means clustering to classify tariff models of different industries
GKNoiseSource
SQL Server 2016 learning records - set query
Nodejs:mongodb 简单模糊+分页查询实例
PyQt5快速开发与实战 4.11 拖曳与剪贴板
ACM winter vacation training 5
Semeval 2022 | introducing knowledge into ner system, aridamo academy won the best paper award
蓝桥杯电子类嵌入式第十届省赛
5、Window端实现Mapreduce程序完成wordcount功能
Blue Bridge Cup embedded Hal library USART_ TX
机器人技术(RoboCup 2D)如何进行一场球赛
Select without the order by clause, the order of the returned results is not reliable
PyQt5快速开发与实战 4.13 菜单栏、工具栏与状态栏 and 4.14 QPrinter
Characteristics and installation of non relational database mongodb
机器人技术(RoboCup 2D)实验五十题及主要函数含义
生成对抗网络在DeepFake中的未来
GKPolygonObstacle
Uni app project directory, file function introduction and development specification