我正在尝试编写一些没有HAL的基本代码来初始化和读取ADC,但是我无法使其响应正确。我在蓝色药丸开发板上使用了STM32F103C8T6。功能如下:
引脚B1(ADC9)连接到0-3.3V信号(通过分压器提供12V电源电压)
当信号低于我称为SUPP_V_MIN_VAL的常数时,引脚B9被置位。如果信号高于该值,则清除引脚B9。
我将SUPP_V_MIN_VAL定义为2606.0。我将所有内容都强制转换为浮点数,因为在代码的单独部分中,我需要将当前ADC值显示为浮点数。
问题是我认为我没有正确初始化ADC或没有正确转换类型。我发现ADC读数始终为0。我的初始化如下:
RCC->APB2ENR |= 0x1UL << 3; //Initialize clock for GPIOB (bit 3), if it hasn't been initialized yet
RCC->APB2ENR |= 0x1UL << 9; //Initialize clock for ADC1 (bit 9), if it hasn't been initialized yet
GPIOB->CRL &= ~(0xF0UL); //SetBar pin PB1 to analog input
//ADC1->CR1 |= 0x0UL; //Default settings are correct - ignore
ADC1->CR2 |= 0x2UL; //Enable ADC1_CR2_CONT for continuous conversion
//ADC1->SMPR1; //ADC channels in SMPR1 are not used - ignore
ADC1->SMPR2 |= 0x38000000UL; // Set SMP9 to 239.5 cycles (set bits 27-29 to 1)
ADC1->CR2 |= 0x1UL; //Set CR2_ADON to wake up ADC from sleep mode
ADC1->CR2 |= 0x1UL << 3; //Initialize calibration register
while ((ADC1->CR2 >> 3) & 0x1UL); //Wait until calibration register is initialized
ADC1->CR2 |= 0x1UL << 2; //Enable calibration
while ((ADC1->CR2 >> 2) & 0x1UL); //Wait until calibration completed
ADC1->CR2 |= 0x1UL; //Set CR2_ADON again to turn on ADC and start converting
我正在用以下方式读取图钉:
void pollSUPP(void)
{
uint16_t ADC_Reading = ADC1->DR;
suppVolt.float_var = (float) ADC_Reading;
if( suppVolt.float_var < SUPP_V_MIN_VAL)
{
// digitalWrite(SUPP_LOW, HIGH); PB9
GPIOB->BSRR = 0X1 << 9;
}
else {
GPIOB->BRR = 0X1 << 9;
}
}
SuppVolt定义为
union {
float float_var;
uint8_t chars[4];
} suppVolt; //ADC reading for supply voltage
我可能还在做一些愚蠢的事情(我实际上尚未确认引脚B9在ADC1上,但我认为是因为蓝色药丸只有一个ADC ??)或引用了寄存器中的错误位。
有人可以帮助我发现我的错误吗?预先谢谢你!