Build a register from its datasheet table
Assemble the ADS1115 ADC's 16-bit config register from its fields. A medium embedded C problem (Reading datasheets and schematics), run and graded in the…
medium · Reading datasheets and schematics · datasheets · registers · bits
The ADS1115 ADC's datasheet describes its 16-bit config register like this:
| Bits | Field | Meaning |
|---|---|---|
| 15 | OS | write 1: start a single conversion |
| 14:12 | MUX | input: 100 = AIN0 vs GND, 101 = AIN1 vs GND, 110 = AIN2, 111 = AIN3 |
| 11:9 | PGA | range: 000 = ±6.144 V, 001 = ±4.096 V, 010 = ±2.048 V, 011 = ±1.024 V |
| 8 | MODE | 1 = single-shot, 0 = continuous |
| 7:5 | DR | data rate: 100 = 128 samples/s, 111 = 860 samples/s |
| 4:0 | comparator | keep at 00011 (comparator disabled) |
Write ads_config(channel, pga, single, rate): channel is 0 to 3, pga and rate are the field codes from the table, single is 1 or 0; always set OS when single is 1, and the comparator bits to 00011.
Starter code
uint16_t ads_config(int channel, int pga, int single, int rate) {
uint16_t r = 0;
r |= (uint16_t)(channel << 12);
r |= (uint16_t)(pga << 9);
r |= (uint16_t)(single << 8);
r |= (uint16_t)(rate << 5);
return r;
}Example tests
AIN0, ±4.096 V, single, 128 SPS
show(0, 1, 1, 4);Expected output:
AIN0 pga 1 single rate 4: 0xC383AIN3, ±2.048 V, continuous, 860 SPS
show(3, 2, 0, 7);Expected output:
AIN3 pga 2 continuous rate 7: 0x74E3Learn 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
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- 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.