Simvik

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:

BitsFieldMeaning
15OSwrite 1: start a single conversion
14:12MUXinput: 100 = AIN0 vs GND, 101 = AIN1 vs GND, 110 = AIN2, 111 = AIN3
11:9PGArange: 000 = ±6.144 V, 001 = ±4.096 V, 010 = ±2.048 V, 011 = ±1.024 V
8MODE1 = single-shot, 0 = continuous
7:5DRdata rate: 100 = 128 samples/s, 111 = 860 samples/s
4:0comparatorkeep 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: 0xC383

AIN3, ±2.048 V, continuous, 860 SPS

show(3, 2, 0, 7);

Expected output:

AIN3 pga 2 continuous rate 7: 0x74E3

Learn it first

More Reading datasheets and schematics problems