当前位置:网站首页>STM32 single channel and multi-channel sampling of non DMA polling ADC based on Hal Library
STM32 single channel and multi-channel sampling of non DMA polling ADC based on Hal Library
2022-07-22 16:27:00 【Haoyuehua】
Catalog
single channel ( Continuous conversion mode )
single channel ( Single conversion mode )
Not DMA Multi channel sampling
single channel ( Continuous conversion mode )
Scan mode disabled , Intermittent mode deactivation , Continuous conversion mode enable
Rule mode enable
about F1 Series of chips
HAL_ADCEx_Calibration_Start(&hadc1); //AD calibration
F4 You don't need to , It has been calibrated automatically
stay while Only need to start once before ADC
HAL_ADC_Start(&hadc1); // start-up ADC transformation
Timed read adc
if(adct>1000){
adct=0;
HAL_ADC_PollForConversion(&hadc1, 50); // Wait for the conversion to complete ,50 For maximum waiting time , Unit is ms
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
{
adcval[0] = HAL_ADC_GetValue(&hadc1); // obtain AD value
printf("ADC1 Reading : %d \r\n",adcval[0]);
printf("PA1 True Voltage value : %.4f \r\n",adcval[0]*3.3f/4096);
}
}
Serial port read
[2022-07-20_18:10:59:740]ADC1 Reading : 2017
[2022-07-20_18:10:59:740]PA1 True Voltage value : 1.6250
single channel ( Single conversion mode )
And a single conversion, each conversion must start adc
HAL_ADC_Start(&hadc1); // start-up ADC transformation
HAL_ADC_PollForConversion(&hadc1, 50); // Wait for the conversion to complete ,50 For maximum waiting time , Unit is ms
Not DMA Multi channel sampling
This question has puzzled me for a long time , Polling mode ( Single plus intermittent ) I've tried , The data is inaccurate during multi-channel testing , For example, the pins of two channels are externally connected 0 or 3.3v The voltage of , Or both 0 Or both 3.3
But after testing, it is read that the voltage values of the two channels are different
Reference resources
contrast cubeMX basis hal Library generated adc Initialization code
As long as cubeMX Configure multi-channel , So many channels will enable you .
There are also these two sentences
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
In this way, it is obvious that the channel set by the latter covers the former , It belongs to redundant code .
This is the structure parameter of the channel configuration , That is to say sConfig
Enter again HAL_ADC_ConfigChannel( Assign values to registers )
#define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
Or I didn't look at the configuration process carefully before , Use the library simply for the sake of using the Library , So follow many tutorials ( Not DMA Many channels of sampling failed .)
Finally, I see the code on the punctual atomic document .( It uses hal library , But it continues the style of the standard library , So it's all about writing by yourself , of no avail cubeMX To automatically generate code )
The final solution : To put it simply, we need to turn multi-channel into single channel , Just change the configuration , The changed configuration is the channel ! because cubeMX Only generated once , So we define a configuration ....
rewrite adc The initialization : That is, it becomes a single channel
void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
}
Other pin GPIO Initialize to analog input with cubeMX Generated can .
Every time according to what you want to read adc Channel to read , That is to change adc Handle configuration , With adc1 Two examples
u16 get_adc(u32 ch){
ADC_ChannelConfTypeDef _adc;
_adc.Channel=ch;
_adc.Rank=1;
_adc.SamplingTime=ADC_SAMPLETIME_239CYCLES_5;
HAL_ADC_ConfigChannel(&hadc1,&_adc);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,10);
return (u16)HAL_ADC_GetValue(&hadc1);
}
effect ( The pin test voltage is correct )
Conclusion : Multi channel is configured as single channel . When I test, both single conversion and continuous conversion are ok .
边栏推荐
- Android interview: 2022 please keep this experience of Netease Android development and Tiktok e-commerce Android engineers
- Android面试:2022请收好这份网易Android开发和抖音电商Android工程师的面经
- Word: insert vector diagram with specified color
- Simplified writing of not like in MySQL
- How does the red team play
- Online RPC timeout troubleshooting and subsequent GC tuning ideas
- 字符编码问题
- 硬件工程师有没有35岁危机?
- Soc之按键控制LED
- mysql中not like的简化写法
猜你喜欢
随机推荐
Remember a composer dependency problem requires composer runtime API ^2.0.0 - > no matching package found
SoC之Hello World
MP query criteria
EasyCVR平台V2.5.0版本及以上如何配置WebRTC协议实现低延迟播放?
【Unity项目实践】游戏架构
EasyCVR平台设备分组新增以及编辑操作异常的问题修复
数据平台数据管理实践
智齿提供的横向撕咬功能
Typora免费下载压缩包(最新可用)
计算机网络之DNS面试题
Session共享问题
Why does the system we developed have concurrent bugs? What is the root cause of concurrent bugs?
C# ftp检测目录是否存在和创建文件夹
Window startup add / close
Soc之按键控制LED
The appearance sequence of question 38 in C language. Three methods (traversal method, recursive method and wolf killing method)
Word: insert vector diagram with specified color
STM32基于HAL库的非DMA的轮询ADC单通道与多通道的采样
A 15-year-old ABAP veteran's suggestion: understanding these basic knowledge is beneficial to ABAP development
shell语法个人运用中问题小结