使用 STM32L ADC 单独读取不同的输入
目标是通过轮询读取多个 ADC 通道。它不需要太快——其想法是读取所连接的不同电池的电压。我有一个 STM32L071 微控制器。与STM32F0模型相比,编程有点不同。我正在使用 platformio。
我在这里发现了非常有用的信息:
- 使用STM32F0 ADC单独读取不同的输入
- https://controllerstech.com/stm32-adc-multiple-channels/
- https://deepbluembedded.com/stm32-adc-tutorial-complete-guide-with-examples/
但是,不幸的是我不能读出多个通道。这些问题可能与 HAL_ADC_Init
和 HAL_ADC_ConfigChannel
有关。
这是一个最小的代码示例:
#include <Arduino.h>
#include <STM32IntRef.h>
uint32_t a1=0, a2=0;
#define HAL_ADC_MODULE_ENABLED
ADC_HandleTypeDef hadc1;
void displaying(){
Serial.println("values:");
Serial.println("-------");
Serial.print("ch1 - ");
Serial.println(a1);
Serial.print("ch2 - ");
Serial.println(a2);
Serial.println("");
}
void config_ext_channel_ADC(uint32_t channel, bool val) {
hadc1.Instance = ADC1;
hadc1.Init.SamplingTime = ADC_SAMPLETIME_79CYCLES_5;
HAL_ADC_Init(&hadc1);
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = channel;
if (val == true) {
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
} else {
sConfig.Rank = ADC_RANK_NONE;
}
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
Serial.println("Error ADC Config Channel");
//Error_Handler();
}
}
uint32_t r_single_ext_channel_ADC(uint32_t channel) {
/* read the ADC and output result */
uint32_t digital_result;
config_ext_channel_ADC(channel, true);
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 1000);
digital_result = HAL_ADC_GetValue(&hadc1);
HAL_ADC_Stop(&hadc1);
config_ext_channel_ADC(channel, false);
return digital_result;
}
void readBat() {
/* read voltages */
a1 = r_single_ext_channel_ADC(1);
a2 = r_single_ext_channel_ADC(PA2);
}
void setup() {
// put your setup code here, to run once:
// Serial monitor
Serial.begin(9600);
Serial.println(F("Starting now"));
// initialize pins for ADC
analogReadResolution(ADC_RESOLUTION);
pinMode(PA1, INPUT);
//pinMode(BATTERY_SENSE_PIN2, INPUT);
pinMode(PA2, INPUT_ANALOG);
}
void loop() {
// put your main code here, to run repeatedly:
readBat();
displaying();
delay(2000);
}
输出为:
values:
-------
ch1 - 0
ch2 - 140
听起来很合理,但在引脚上施加一些电压不会改变值。
有人可以给我一些建议、想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
分别读取不同 ADC 通道(此处为 2 个通道的 ADC2)的代码(由 CubeMX 生成):
注意:我们必须使用
ScanConvMode
加上DiscontinuousConvMode
和 NOContinouslyConvMode< /代码>。
现在,阅读:
Code to read different ADC channels (here ADC2 with 2 channels) separetly (Generated By CubeMX):
Note: We have to use
ScanConvMode
plusDiscontinuousConvMode
and NOContinuousConvMode
.Now, to read:
好吧,同时我发现了问题。软件很好;我采用了 Arduino 框架并使用了analogRead()。
我需要清除 PCB 上的所有助焊剂,这些助焊剂会导致引脚之间产生一些接触。
此外,如果想直接使用 HAL 寻址,则应设置 ClockDivider,使 ADC 采样频率低于 1 MHz。
Okay, in the meantime I found the problem. The software is fine; I took the Arduino framework and used
analogRead()
.I needed to clean all the solder flux from the PCB which caused some contacts between the pins.
Moreover, if one wants to use address the HAL directly, one should set the ClockDivider such that the ADC samples below 1 MHz.
您是否错过了将 io 设置为模拟模式?它应该在某个地方是这样的(通常在你的 hal msp 文件中,adc_init func)。
编辑=>你错过了 ch1
它应该与 PA2 相同,输入是“数字模式”。
Did you miss to set the io to analog in mode? It should be something like this somewhere (usually in your hal msp file, adc_init func).
edit => you missed it for ch1
It should be the same as PA2, input is "digital mode".