STM32F3开发板ADC通道选择裸机编程技术求助
Hey there! I see you're making the jump from AVR to STM32 bare-metal development—great choice, though I totally get the frustration with the extra configuration layers. Let's tackle your ADC channel selection issue first, then point you to some solid resources tailored for your setup.
Fixing Your ADC Channel Selection Code
Looking at your code, there are a few key issues preventing proper ADC channel configuration (especially for the internal temperature sensor you mentioned). Here's a breakdown of the fixes and a revised code snippet:
1. Clock Configuration Mistakes
- You tried setting the ADC clock mode via
ADC1->CR, but the clock mode is controlled in the shared ADC12 control register (ADC12->CCR), not the individual ADC1 CR register. - The
ADRDflag in your wait loop is a typo—it should beADRDY(ADC Ready flag).
2. Channel & Sensor Enable
- To use the internal temperature sensor, you need to enable it in
ADC12->CCRwith theTSENbit. - Channel selection is done via the
SQR(Sequence Register) family. For a single conversion of channel 16 (the internal temp sensor on STM32F3), you'll configureSQR3to set the first sequence channel.
3. Sampling Time Correction
- The
SMP0bits inSMPR1correspond to channel 10, but the temp sensor is channel 16—you need to targetSMP16instead.
Revised Code
/* * ADC in ARM * * Author : Easwaran * */ #include "stm32f30x_conf.h" int main (void) { // ADC calibration ADC1->CR &= ~ADC_CR_ADEN; // Disable ADC ADC1->CR |= ADC_CR_ADCAL; // Start calibration while ((ADC1->CR & ADC_CR_ADCAL) != 0); // Wait for calibration to finish // Enable ADC12 clock RCC->AHBENR |= RCC_AHBENR_ADC12EN; // Configure HSE clock (optional but useful for stable ADC timing) RCC->CR |= RCC_CR_HSEON; while((RCC->CR & RCC_CR_HSERDY) == 0); // Wait for HSE to stabilize // Set ADC clock mode (HSE divided by 1 here—adjust per your needs) ADC12->CCR |= ADC12_CCR_CKMODE_0; // Enable internal temperature sensor ADC12->CCR |= ADC12_CCR_TSEN; // Enable ADC1 and wait for ready ADC1->CR |= ADC_CR_ADEN; while((ADC1->ISR & ADC_ISR_ADRDY) == 0); // Wait for ADC ready flag // Set sampling time for channel 16 (temp sensor) to maximum (239.5 cycles) ADC1->SMPR1 |= ADC_SMPR1_SMP16_0 | ADC_SMPR1_SMP16_1 | ADC_SMPR1_SMP16_2; // Configure conversion sequence: 1 conversion, channel 16 as first (and only) channel ADC1->SQR1 = 0; // Sequence length = 1 (no bits set) ADC1->SQR3 = ADC_SQR3_SQ1_4; // Channel 16 (binary 10000) uint16_t adc_result; while(1) { // Start conversion ADC1->CR |= ADC_CR_ADSTART; // Wait for conversion to complete while((ADC1->ISR & ADC_ISR_EOC) == 0); // Read conversion result adc_result = ADC1->DR; // Reset EOC flag (optional, but good practice) ADC1->ISR |= ADC_ISR_EOC; } }
Bare-Metal STM32 Learning Resources
Since you're working on Linux with GNU ARM tools and Eclipse, here are some resources that will help you avoid the "not STM32F3-specific" problem you ran into:
STM32 Official Documentation:
- The STM32F303 Reference Manual is your go-to for register-level details—don't skip this, it's the definitive source for F3 specifics.
- The Cortex-M4 Programming Manual covers core-level operations that apply to all Cortex-M4 MCUs like the STM32F3.
STM32F3-Specific Tutorials:
- Look for "STM32F3 Discovery Bare-Metal Programming" video or blog series—many creators focus on register-level code for this board, avoiding HAL/LL libraries which can hide the details you want to learn.
- Embedded Artistry's tutorials have great general bare-metal tips, plus F3-specific code examples that work with GNU tools.
Books:
- Mastering STM32 is a fantastic resource that covers bare-metal development across multiple STM32 families, including the F3. It includes examples for GNU toolchains and explains register-level configuration clearly.
Community Support:
- The STM32 tag on Stack Overflow is full of F3-specific questions and answers—search for "STM32F3 ADC channel selection" to see similar issues other developers have solved.
- The official STM32 Community Forums have a dedicated bare-metal section where you can ask F3-specific questions and get advice from seasoned developers.
内容的提问来源于stack exchange,提问作者easwaran

