ADC timing from the electrical characteristics
Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time. A hard embedded C problem (Reading datasheets and…
hard · Reading datasheets and schematics · datasheets · adc · clocks · timing
The datasheet's ADC section says:
- f<sub>ADC</sub> must not exceed 14 MHz. The ADC clock is the bus clock divided by the prescaler (
ADC_DIV, provided). - t<sub>S</sub> (sampling time) is chosen per channel from the
SMPtable. With the sensor's source impedance, the electrical characteristics table requires t<sub>S</sub> of at leastmin_sample_ns. - t<sub>CONV</sub> = t<sub>S</sub> + 12.5 ADC clock cycles.
Write adc_plan(pclk_hz, min_sample_ns, out):
- Pick the smallest prescaler that keeps f<sub>ADC</sub> at or below 14 MHz (a faster ADC clock gives faster conversions).
- With that clock, pick the shortest sampling time that is at least
min_sample_ns. - Fill
outwith both codes and t<sub>CONV</sub> in nanoseconds, rounded up, and return 0. If either choice is impossible, return -1.
Do the timing comparisons exactly in integers: the half-cycle table avoids the .5s, and 64-bit products avoid overflow.
Starter code
int adc_plan(uint32_t pclk_hz, uint32_t min_sample_ns, adc_plan_t *out) {
(void)min_sample_ns;
out->adcpre = 0; // the fastest clock
out->smp = 0; // the shortest sampling time
out->conv_ns = 14000u / (pclk_hz / 2000000u); // 14 cycles
return 0;
}Example tests
72 MHz bus, a 1 µs sampling requirement
show(72000000, 1000);Expected output:
72 MHz, sample >= 1000 ns: /6, SMP 2 (13.5 cycles), 2167 nsTwo more boards
show(48000000, 0);
show(56000000, 5000);Expected output:
48 MHz, sample >= 0 ns: /4, SMP 0 (1.5 cycles), 1167 ns
56 MHz, sample >= 5000 ns: /4, SMP 6 (71.5 cycles), 6000 nsLearn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- A relay and a sensor's power switch, from the transistors · One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.