Subsection 6.2.5 Part 4b: Writing ADCPot.c
Turn the knob to each mechanical end stop. Do you read exactly 0 and exactly 4095? If not, is the explanation in the potentiometer, or in the converter?
Now we’ll put those five registers together. The program has three parts: a one-time initialization, a function that starts a conversion, and a function that waits for and returns the result.
Here is the skeleton. Each
// TODO is one of the clusters you have just seen, but for
channel 0 on PA0, not the channel 4 examples above.
/* ADCPot.c -- read a potentiometer on PA0 with the ADC
* ENGS 28
*/
#include <stdio.h>
#include "ES28.h"
#include "uart.h"
void pa0_adc_init(void);
void start_conversion(void);
uint16_t adc_read(void);
int main(void) {
uint16_t sensor_value = 0;
uart2_init();
printf("\n\rHello from STM32C0!\r\n");
printf("Single conversion ADC test\r\n");
pa0_adc_init();
while (1) {
start_conversion();
sensor_value = adc_read();
printf("Sensor value: %d\r\n", sensor_value);
delay_ms(1000);
}
return 0;
}
void pa0_adc_init(void) {
// TODO 1 -- configure PA0 as an analog input
// (enable the GPIOA clock, then change the mode field in one write)
// TODO 2 -- enable the clock to the ADC
// TODO 3 -- select the channel PA0 is connected to
// Enable the ADC module
ADC1->ISR = ADC_ISR_ADRDY; // clear the ready bit (we'll come back to this)
ADC1->CR |= ADC_CR_ADEN; // set the ADC enable bit
}
void start_conversion(void) {
// TODO 4 -- wait until the ADC reports that it is ready (ADRDY)
ADC1->CR |= ADC_CR_ADSTART; // start a single conversion
}
uint16_t adc_read(void) {
// TODO 5 -- wait until the conversion has finished (EOC)
return (uint16_t) ADC1->DR; // reading DR also clears EOC
}
Activity 6.2.4. Make the Knob Move the Number.
(a)
Fill in the five
// TODO blanks. Build, flash, and open your serial terminal. Turn the knob.
(b)
Compare what you see against the four counts you predicted when you measured the wiper with the DMM. Do they match? If a reading is off by more than a few counts, is the disagreement in your prediction, your DMM measurement, or the ADC?
Nothing printed? Each of these symptoms has a different cause, so work down the list in order.
-
The debugger will not connect, with a libusb error. Check that your power rail is on 3V3 and not NRST. The two are adjacent on the header, so a jumper one row off is easy to make and easy to miss, and it is a common wiring error in this class.
-
No output at all, but the board flashes and runs. Confirm you are building the project you think you are. A stray copy of blinky.c left in the project will build and run without error while printing nothing.
-
Output appears but never changes. The wiper is probably not connected to A0. Failing that, check that TODO 1 set the mode bits to 0b11 and not to something else. Note that the reset value for MODER for most GPIO pins is 3 (analog mode) so if you forgot this step entirely it’s not the cause for this error.
-
The value is always 0. The pin really is sitting at 0 V. Check that the wiper jumper shares a breadboard row with the pot’s middle leg, and that the two outer legs go to 3.3 V and GND, not both to GND.
-
The program hangs with no output after the greeting. One of the while loops is waiting for a bit that will never be set. All three likely causes look identical from outside: the ADC clock (TODO 2), the channel (TODO 3), or the ready wait (TODO 4). Check them in that order.
Finished early? Turn the knob to each mechanical end stop. Do you read exactly 0 and exactly 4095? If not, why not, and is the explanation in the potentiometer or in the converter?
The link below opens the same exercise in the simulator, for working on it away from your Nucleo. Click
+ Pot in the component bay, then click the Arduino header pin labeled
A0, and drag the knob: the wiper voltage appears under the knob, and it is the number your DMM would read. That one click wires the whole divider, so there is nothing to connect to 3.3 V or to ground. The register panel shows
ADC1->CHSELR,
ADC1->ISR and
ADC1->DR as soon as your program touches the converter, so you can watch
EOC come up and the count land in
DR. One warning: the reading in the simulator has very little noise, a lot less than a real converter and a real potentiometer will give you, and none of the sampling-time effects of
Subsection 6.3.2 appear here at all. The simulator does not save your code, so use the download button (the downward arrow) to keep a copy of your work.
Open in the board simulator (new tab)
-
Copy your TemplateProject and name the copy ADCPot.
-
Download the ADCPot.c skeleton from the book’s starter files into the project’s Src folder.
-
Then fill in the five blanks, using the five registers we just went through.
void pa0_adc_init(void) {
// TODO 1 -- PA0 as analog input (GPIOA clock, then change the mode field in one write)
// TODO 2 -- enable the clock to the ADC
// TODO 3 -- select the channel PA0 is connected to
ADC1->ISR = ADC_ISR_ADRDY; // clear the ready bit (we'll come back to this)
ADC1->CR |= ADC_CR_ADEN; // set the ADC enable bit
}
void start_conversion(void) {
// TODO 4 -- wait until the ADC reports ready (ADRDY)
ADC1->CR |= ADC_CR_ADSTART;
}
uint16_t adc_read(void) {
// TODO 5 -- wait until the conversion has finished (EOC)
return (uint16_t) ADC1->DR; // reading DR also clears EOC
}
-
libusb error, debugger won’t connect: power rail on NRST, not 3V3.
-
Runs, but no output: a stray blinky.c is what’s building.
-
Output never changes: the wiper is probably not on A0. (Most pins reset to analog mode, so a forgotten TODO 1 is not the cause.)
-
Always 0: the pin really is at 0 V. Check the wiper shares a row with the pot’s middle leg, and that the outer legs are not both on GND.
-
Hangs after the greeting: waiting on a bit that will never be set.