Introduction to I2C: Example
This video provides an example of I2C communication. The video shows how to configure the address in hardware. The example steps through a communications example where a pointer register is used to select different registers and perform read write operations on the device. Each step of the communications pattern is described as it would be shown on an oscilloscope.
Medios
Hello, and welcome to our in-depth look at communications with precision data converters. In this video, we describe I2C communication using a precision data converter as an example. We'll use an example to show how to write to and read from an ADC using I2C. For this example, we will look at the ADS1115, which is a 16-bit precision ADC. It uses it I2C interface and is capable of standard, fast, and high speed modes. It has several different settings that can be set through a configuration register, including the input range set by a Programmable Gain Amplifier or PGA, a variety of data rates, and an input multiplexer that can be set to make differential measurements or single-ended measurements with respect to ground.
The ADS1115 has an address pin labeled ADDR. This pin can be used to select one of four I2C addresses, meaning that four of these devices could be used on the same bus as long as the devices are programmed to different addresses. The I2C address used for the device depends on the ADDR pin connection. With the ADDR pin connected to ground, the device has an I2C address of 48 in hexadecimal. With the ADDR pin connected to VDD, SDA, or SEL, the device can be set to other addresses shown in the table.
For this example, we'll use the ADDR set to ground, so the address is 48 and hexadecimal. The ADS115 has four internal registers, each addressed by an internal pointer register. The pointer register scheme in this example is a common method used in I2C devices with multiple registers. The first register is the conversion data register. When the ADC completes a conversion, the ADC data is placed in this register, and the controller device reads it out from here.
The second register is the configuration register. The controller device will write to this register to program the device and start a conversion. This register sets different aspects of the ADC conversion. It sets the programmable gain amplifier, the input channel, the data rate, and other modes of operation for the device. Later, we'll cover this register in-depth, showing how to write to this register and to configure the device.
The last two registers are the lo_threshold register and the hi_threshold register. These two registers are used to set thresholds for a digital comparator in the device. Once the conversion data goes beyond these thresholds, they can set an alert in the ALERT/RDY pin. For this example, we won't configure the comparator and the thresholds. For this example, we'll with looking at the settings for the configuration register and using the I2C protocol to program the device.
The ADS115 has a 16-bit configuration register. Writing to this register will program the configuration of the device and start a conversion. We'll discuss the settings for this configuration register and determine what to program into the device. Figure 36 from the data sheet shows the configuration register data fields. This shows the bit positions in the configuration register and what they are used for. Once we determine all of the settings for the configuration register, we can use an I2C write to set the register.
Table 8 in the data sheet shows the configuration register field descriptions, giving a detailed description of the bit settings. Starting with the most significant bit, bit 15 is the single shot conversion start bit. Setting bit 15 to 1 will start a single conversion.
Bits 14 to 12 set the multiplexer setting of the device. For this example, we we'll set the device to do a single-ended measurement from AIN0 with respect to ground. Using table 8, we would set AINT to AIN0 and AINN to GNd. To do this, set bits 14 to 12 to be 100 in binary.
Bits 11 t0 9 set the PGA setting of the device. This is the setting for the programmable gain amplifier. This sets the full scale range of the input measurement, setting how large of an input signal can be measured by the ADC. Here we set the ADC to measure a signal as large as plus and minus 4.096 volts. Set bits 11 to 9 to be 001 in binary.
Bit 8 sets the operating mode for the device. For this operation, we want to set the device to be in a single-shot conversion mode. Set bit 8 to 1.
Bit 7 to 5 set the data rate for the ADC of the device. We can set this to the highest data rate of 860 samples per second. Set but 7 to 5 to 111.
The last five bits, from 4 down to 0, are all used for the digital comparator for this device. We won't use the digital comparator here, so we'll disable the setting with the last two bits of the register and set the remaining bits to their default setting. Set bits 4 to 0 to be 00011. One
Now, we have the complete configuration registers setting for the ADS115. We'll use these bits for the write to the register. This register can also be represented in hexadecimal as C3E3.
Figure 31 in the ADS115 data sheet shows a generic diagram for writing to the device. The figure has been altered to show the exact bit transaction sent to the device. We'll use the settings that we've previously discussed to show exactly what we need to write to the configuration register. For reference, we'll put up the data we need on the right side of the slide.
First, the I2C write starts with a start condition. SDA is pulled low and then SEL is pulled low. Then we write the I2C address. With the ADDR pin connected to ground, we use an address of 100 1000 or 48 in hex. The read/write bit is then set low, indicating that we want to write to the device.
After the completion of the address frame, the ADS115 should acknowledge the address by pulling down the SDA for the last bit of the address frame. The controller sends out the address and read/write information to all of the targets on the bus. If a target has a matching address, they will acknowledge to let the controller know that this is valid address and they are ready to receive information. This acknowledge lets the controller know that the target device is ready for communication.
After we've indicated that we want to write to the ADS115, we need to tell the device that we want to write to the configuration register. The second byte is the register pointer for the configuration register. Here we send 0000 0001 to the ADS115. As a response, the ADS115 pulls down on SDA for an acknowledge. Again, the target devices is letting the comptroller know it has received the address pointer data.
Now, we start to send in the configuration register data one byte at a time. For this byte, we send in the first byte of the configuration register. Here we send 1100 0011 to the ADS115. The ADS115 acknowledges the first byte and pulls down on SDA for the last bit.
Finally, we said in the last byte of the configuration register. Here we send 1110 0011 to the ADS115. The ADS115 acknowledges the first byte and pulls down on SDA for the last bit. At the end, the controller releases the bus by issuing a stop condition. SEL is released high, and then SDA is released by.
Putting it all together, figure 31 now looks like this-- figure 31 on the data sheet is very useful when debugging communications. You could plot the I2C communication with an oscilloscope and compare this figure with the plot to debug any issues. The ADS115 has a 16-bit ADC and therefore puts out 16-bit data conversions. To get the ADC conversion data, you need to read from a conversion register. The conversion register address pointer is 0000 0000.
Figure 35 from the data sheet shows the conversion register data field, and table 8 in the data sheet shows the configuration register field description. Conversion data appears as a 16-bit result in binary 2's complement. A positive fullscale input produces an output code of 7FFF in hexadecimal, and a negative fullscale input produces an output code of 8000 in hexadecimal.
Figure 30 in the ADS115 data sheet shows a generic diagram for reading from the device. Again, we'll use the settings that we've previously discussed to show exactly what we need to read from the conversion register. For reference, we'll put up the address and the register pointer that we need on the right side of the slide.
First, the I2C write starts with the start condition. SDA is pulled low, and then SEL is pulled low. Then we write to the I2C address. With the ADDR pin connected to ground, we can use an address of 100 1000 or 48 in hexadecimal. We need to tell the device which register we need to read from. For this, we need a write to the device so that we can set up the read from the ADS115. At this point, the read/write bit is then set low, indicating that we want to write to the device. After the completion of the address frame, the ADS115 acknowledges the address by pulling down on the SDA for the last bit of the address frame.
After we've indicated that we want to write to the ADS115, we need to tell the device that we want to access the configuration register. The second byte is the register pointer for the configuration register. Here we send to 0000 0000 to the ADS115. As a response, the ADS115 pulls down on SDA for an acknowledge. Finally, the controller issues a stop to release the bus.
Now that we've told the device we want to access the conversion register, we follow up with the read from the conversion register. We write the I2C address again. Now, we need a read from the device from the conversion register of the ADS115. At this point, the read/write bit is then set high, indicating that we now want to read from the device. Again, after the completion of the address frame, the ADS115 should acknowledge the address.
Now, we can read the conversion register one byte at a time. First, you read the most significant byte. And as a response for reading the data, the controller device pulls down on SDA for an acknowledge. Because the target device is sending data, it is now the controller device that pulls down SDA for an acknowledge. This tells the target device that the data has been received by the controller device. Then follows the read of the least significant byte and then another acknowledge from the controller. Finally, the controller sends a stop to end the I2C communication.
Just to follow up with the conversion register result, let's use one last example with reading data and making the conversion. Let's say you read out the data from the conversion register and it reads 44C0 four in hexadecimal or 17600 in decimal. This is the ADC output based on the input voltage from the measurement. Using this value, you can convert the conversion register to a voltage for the ADC measurement. With a positive fullscale range of 4.096 volts, you can convert this to a measured voltage. Here, the ADC is reporting 2.2 volts.
That concludes this video. Thank you for watching. Please try the quiz to check your understanding of this video's content.
Question 1, True/False, a write of the configuration register can be written with 16 consecutive bits instead of breaking up the register into two 1-byte transfers? The correct answer is b, False. I2C transmissions come in a single byte transmission frame. This is standardized, and both controller devices and target devices know that the transmission will come in a set format.
Question 2, what is the I2C protocol by order of a read from the ADS115? The correct answer is d, I2C address write, pointer address, I2C address read, data MSB, data LSB. While the objective of this sequence is to read the conversion register, we still need to tell the target device what register the controller wants to read from. Notice that we first write to the pointer register before we can read that register.
This video is part of a series
-
Serie Precision labs: Convertidores analógico-digitales (ADC)
video-playlist (85 videos)