|
| 1 | +#include "adc16_utils.h" |
| 2 | +#include "hardware/gpio.h" |
| 3 | +#include "hardware/i2c.h" |
| 4 | + |
| 5 | + |
| 6 | +/* |
| 7 | +ADC16 uses the I2C bus to communicate with the ASD1115 chip. |
| 8 | +The I2C bus is connected to the following pins: |
| 9 | +SCL: GP5 |
| 10 | +SDA: GP4 |
| 11 | +
|
| 12 | + ADC strategy: |
| 13 | + The ADC is run in continuous-conversion mode. Currently only single-ended inputs are supported, |
| 14 | + enumerated as channels 0-3. The ADC is configured to measure +2.048V full-scale, at a rate of 32 SPS, |
| 15 | + i.e. a new result is available every 31.25ms. Note: when switching channels, the new result won't be |
| 16 | + available for about 80 msec (2 * 31.25ms, plus a small margin). |
| 17 | + In single-ended mode, the raw result is a 15-bit value, i.e. 0-32767. |
| 18 | + To convert to a voltage, the reported raw result can be multiplied by 2.048/32767. |
| 19 | +*/ |
| 20 | + |
| 21 | +/************** global vars ***************/ |
| 22 | +uint8_t adc16Installed = 0; |
| 23 | +uint32_t adc16Channel = 0; |
| 24 | +uint8_t confreg[2]; // config register |
| 25 | + |
| 26 | +/************** functions *****************/ |
| 27 | +// initialize the I2C bus |
| 28 | +void initAdc16I2C(void) { |
| 29 | + i2c_init(i2c_default, 10000); // I2C0 on GPIO 4[SDA],5[SCL] |
| 30 | + gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN, GPIO_FUNC_I2C); |
| 31 | + gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN, GPIO_FUNC_I2C); |
| 32 | + gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN); // weak pull-ups but enable them anyway |
| 33 | + gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN); |
| 34 | +} |
| 35 | + |
| 36 | +// initialize the ADS1115 registers |
| 37 | +void initAdc16Reg(void) { |
| 38 | + uint8_t buf[3]; |
| 39 | + // is the ADC chip installed? |
| 40 | + buf[0] = ADS1115_REG_CONFIG; |
| 41 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf, 1, false); |
| 42 | + i2c_read_blocking(i2c_default, ADS_ADDR, buf, 2, false); |
| 43 | + if (buf[0] != 0x85 || buf[1] != 0x83) { |
| 44 | + adc16Installed = 0; // no chip found |
| 45 | + return; |
| 46 | + } else { |
| 47 | + adc16Installed = 1; // chip found |
| 48 | + } |
| 49 | + // set config register to desired values |
| 50 | + confreg[0] = 0x44; // MUX set to AIN0, and PGA set to +-2.048V and continuous conversion mode |
| 51 | + confreg[1] = 0x43; // rate = 32 SPS |
| 52 | + buf[0] = ADS1115_REG_CONFIG; |
| 53 | + buf[1] = confreg[0]; |
| 54 | + buf[2] = confreg[1]; |
| 55 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf, 3, false); |
| 56 | +} |
| 57 | + |
| 58 | +// set the multiplexer to the desired input |
| 59 | +void setAdc16Mux(uint8_t mux) { |
| 60 | + uint8_t buf[3]; |
| 61 | + confreg[0] &= ~0x70; // clear the MUX bits |
| 62 | + confreg[0] |= (mux<<4); // set the MUX bits |
| 63 | + buf[0] = ADS1115_REG_CONFIG; |
| 64 | + buf[1] = confreg[0]; |
| 65 | + buf[2] = confreg[1]; |
| 66 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf, 3, false); |
| 67 | +} |
| 68 | + |
| 69 | +// start a conversion (this is only for single-shot mode) |
| 70 | +// will delete this function if it's not needed |
| 71 | +void startAdc16Conv(void) { |
| 72 | + uint8_t buf[3]; |
| 73 | + buf[0] = ADS1115_REG_CONFIG; |
| 74 | + buf[1] = confreg[0] | 0x80; // set 'OS' bit to start a conversion |
| 75 | + buf[2] = confreg[1]; |
| 76 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf, 3, false); |
| 77 | +} |
| 78 | + |
| 79 | +// check if the conversion is complete (this may only work for single-shot mode) |
| 80 | +// will delete this function if it's not needed |
| 81 | +uint8_t adc16ConvDone(void) { |
| 82 | + uint8_t buf[3]; |
| 83 | + buf[0] = ADS1115_REG_CONFIG; |
| 84 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf, 1, false); |
| 85 | + i2c_read_blocking(i2c_default, ADS_ADDR, buf, 2, false); |
| 86 | + if (buf[0] & 0x80) { |
| 87 | + return 0; // conversion not done |
| 88 | + } else { |
| 89 | + return 1; // conversion done |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// read the conversion register |
| 94 | +uint16_t readAdc16Meas(void) { |
| 95 | + uint16_t meas; |
| 96 | + uint16_t buf; |
| 97 | + uint8_t* buf8_ptr = (uint8_t*)&buf; |
| 98 | + *buf8_ptr = ADS1115_REG_CONVERSION; |
| 99 | + i2c_write_blocking(i2c_default, ADS_ADDR, buf8_ptr, 1, false); |
| 100 | + i2c_read_blocking(i2c_default, ADS_ADDR, buf8_ptr, 2, false); |
| 101 | + meas = buf>>8 | buf<<8; // swap bytes |
| 102 | + if (adc16Channel < 4) { |
| 103 | + // we are in single-ended mode. Any integer value less than 0 is invalid |
| 104 | + // and since we are dealing with unsigned values, that means that any |
| 105 | + // value greater than 32767 is invalid. |
| 106 | + if (meas > 32767) { |
| 107 | + meas = 0; |
| 108 | + } |
| 109 | + } |
| 110 | + return(meas); |
| 111 | +} |
| 112 | + |
| 113 | +// provide a pin count. returns 0 if the ADS1115 is not installed |
| 114 | +uint32_t adc16PinCount() { |
| 115 | + if (adc16Installed) { |
| 116 | + return(4); |
| 117 | + } else { |
| 118 | + return(0); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// get ADC result from the ADS1115 |
| 123 | +uint16_t getAdc16PinAt(uint32_t index) { |
| 124 | + uint16_t res; |
| 125 | + if (adc16Channel == index) { |
| 126 | + // already set to this channel |
| 127 | + } else { |
| 128 | + // switch channel, and then wait for a conversion to be done |
| 129 | + adc16Channel = index; |
| 130 | + setAdc16Mux(ADS1115_CH0 + index); |
| 131 | + // no need to start conversion, since we are in continuous conversion mode |
| 132 | + sleep_ms(80); // wait for conversion to complete. The value should be 2 * (1/SPS) + margin |
| 133 | + } |
| 134 | + res = readAdc16Meas(); |
| 135 | + // no need to start another conversion, since we are in continuous conversion mode |
| 136 | + return(res); |
| 137 | +} |
0 commit comments