Skip to main content

Subsection 6.2.8 Part 7: Before Next Class

Tonight’s homework continues directly into Lab 4, so keep a record of what you find. Make one change at a time, and change it back before you make the next one.
Moving the pot to A3 and changing the resolution can both be done in the simulator as well as on your board. It takes its channel numbers from the same Table 12 of the STM32C031C6 datasheet, so the channel you look up is the one it will sample, and a write to ADC_CFGR1 while ADEN is set is ignored there just as it is on the chip. It does give you one of the three numbers: the port pin is printed beside every header pin on the simulated board, which your Nucleo does not print on its silkscreen. The channel is still yours to find. The link below opens the simulator with the ADCPot.c starter. Paste your own finished ADCPot.c in its place, then click + Pot in the component bay and click the Arduino header pin your program reads.
For checking the arithmetic both ways you need your own DMM and your own potentiometer. In the simulator the count and the wiper voltage under the knob are computed from the same number, so they are not two independent measurements, and agreement between them would tell you nothing. If your kit is not with you tonight, do the first two and keep this one for when it is.

Activity 6.2.8. “Homework”, to help you get a head start on the lab.

(a)

Move the pot to A3. Rewire the wiper to the header pin labeled A3 and make the program work again. You will need to find out which port pin A3 is, which the Nucleo pinout gives, and then which ADC channel that port pin reaches, which is datasheet Table 12 again. Do not assume any of the three numbers match. Work each translation out from the pinout and the datasheet, and keep a record of the chain you followed.
Everything that mentions the old pin has to change with it. Check each line of pa0_adc_init() in turn and ask whether it still names the right thing.

(b)

Change the resolution. Set your pot so the DMM reads as close to 1.000 V as you can manage, and record the count the ADC returns. Then find the RES field in ADC_CFGR1 in the reference manual and switch the converter to 10-bit. Read the field’s access rules while you are there: RM0490 §14.12.4 allows software to program ADC_CFGR1 only while ADEN is cleared, so where you put this line matters as much as what it says. Predict the new count before you run it, then check. Explain the factor you see.

(c)

Check the arithmetic both ways. Pick any three knob positions. For each, measure the wiper with the DMM, record the count your program reports, and compute the voltage from the count. How closely do the measured and computed voltages agree, and is the difference within the one-LSB rounding you would expect?