Subsection 6.2.4 Part 4a: The Registers, One at a Time
Reading one voltage takes five steps: set the pinβs mode, enable the ADCβs clock, select the channel, start a conversion, and wait for it to finish. Today weβll do each of those five steps ourselves. Because you write all five, you can change any of them later, for a different pin, a different resolution or a faster sample, and you can see where a measurement went wrong when it does. Weβll look at each step on its own before you write any of them.
The pin. An analog input is a fourth GPIO mode, alongside input, output, and alternate function. Its
MODER encoding is 0b11. Like every peripheral, a GPIO port needs its own clock enabled before its registers respond, and note that this is the portβs clock, in RCC_IOPENR, which is a different register from the ADCβs own clock enable that follows. Then, as with any multi-bit field, we clear the field and OR in the new value inside a single assignment, as the UART driver does, so the register is written once. For analog mode (0b11) OR alone would happen to work, but clearing first is what makes the same statement correct for every mode, since OR cannot turn a 1 back into a 0. The example below uses PA4, not the PA0 you actually need, so that you have to work out which parts change rather than copying it across:
RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // clock to port A
GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE4_Msk)
| (GPIO_ANALOG << GPIO_MODER_MODE4_Pos); // PA4 to analog (0b11), in one write
GPIO_ANALOG is the constant 3 that ES28.h defines for this mode, the same list of mode constants you used on Day 6 (GPIO_INPUT 0, GPIO_OUTPUT 1, GPIO_ALTERNATE 2, GPIO_ANALOG 3), and 3 is 0b11 in binary, which is what the two MODER bits must hold. GPIO_MODER_MODE4_Pos is the name that CMSIS, the Common Microcontroller Software Interface Standard that ships with the projectβs headers, gives to the position where that two-bit field starts. Using the named constants rather than counting bit positions by hand is what keeps this readable.
The ADC clock. Like every peripheral, the ADC does nothing until its clock is enabled. The ADC is on the APB, the Advanced Peripheral Bus, and its enable bit is in
RCC_APBENR2:
RCC->APBENR2 |= RCC_APBENR2_ADCEN; // enable the clock to the ADC
The converter also has its own conversion clock, asynchronous to the APB bus clock that this line enables. We leave it at its default source and prescaler, so there is nothing further to configure today, but remember that this line is where the ADC becomes reachable at all.
The channel. Set the bit for the channel you want in
ADC_CHSELR. To sample channel 4, which is where PA4 appears:
ADC1->CHSELR |= ADC_CHSELR_CHSEL4; // select channel 4
The peripheral is called
ADC1 even though this chip has only one converter: other members of the STM32 family have several, and the name is kept consistent so code ports between them.
The converter. Two registers govern the ADC itself:
ADC_CR and ADC_ISR. Of these two, ADC_CR is the control register. In this register, ADEN enables the ADC and ADSTART begins a conversion.
ADC_CR. Bit 0 (ADEN) enables the converter; bit 2 (ADSTART) starts a conversion and is cleared by hardware when that conversion completes. You set these bits; the hardware manages the rest.Enabling the ADC does not make it usable immediately, since the analog circuitry needs time to stabilize. The three steps to help accomplish this are: clear
ADRDY, set ADEN, and then wait for ADRDY to come back. That flag is how the hardware tells you the converter is ready to accept work. Note that the first two happen once, in our initialization routine pa0_adc_init(), while the wait belongs in start_conversion(), which we call each time we want to take a sample.
ADC_ISR is the interrupt and status register, where the hardware reports what it has done. Two of its bits matter today: ADRDY, set when the converter has finished starting up and is ready to begin a conversion, and EOC, set when a conversion has finished and the result is available.
ADC_ISR. ADRDY (bit 0) means the converter is ready to be used; EOC (bit 2) means a conversion has completed and its result is waiting in the data register. Both are set by hardware. Your codeβs job is to wait for them.Waiting for a status bit is a pattern you have used before. On Day 5 you waited for the UART to be ready to accept a character:
while (!(USART2->ISR & USART_ISR_TXE_TXFNF)) {} // Day 5: wait for the UART
The ADC wait has the same shape: same
while, same !(register & bit), a different register and a different bit. Written out for the end-of-conversion flag, it is:
while (!(ADC1->ISR & ADC_ISR_EOC)) {} // wait until the conversion is done
Keep looping for as long as it is not true that the flag bit is set.
Finally, when a conversion completes, the 12-bit result appears in the data register
ADC_DR, right-aligned in bits 11:0. Reading ADC_DR also clears EOC, so each result is collected exactly once.
ADC_DR is a 32-bit register, but the result inside it is only 12 bits wide and is at the bottom, so it fits comfortably in a uint16_t, which is why adc_read() returns one rather than a full uint32_t. The cast (uint16_t) discards the upper 16 bits of the register, and that is safe here precisely because those bits are reserved and always read as zero. Narrowing a value you have not checked is a good way to lose data silently; this one you have checked.
