digital thermometer or talk I2C to your atmel microcontroller

Not rated 18,466

digital thermometer or talk I2C to your atmel microcontroller
digital thermometer or talk I2C to your atmel microcontroller

A prerequisite for this article is that the GCC AVR programming environment is installed as described in the article "Programming the AVR microcontroller with GCC, libc 1.0.4." To avoid installation issues, using the AVR programming CD is recommended. When utilizing advanced devices such as microcontrollers for measuring analog or digital signals, interfaces are necessary to evaluate data or send commands to the microcontroller. Previous articles have consistently employed RS232 communication via the UART integrated into the microcontroller. However, this approach necessitates an additional MAX232 chip and four extra capacitors. Atmel also recommends an external crystal oscillator for reliable UART communication, resulting in numerous additional components. Fortunately, this can be avoided. The volume of data exchanged between a PC and microcontroller is typically minimal (only a few bytes), making speed a non-issue. This characteristic renders the I2C bus/protocol suitable for the task at hand. I2C (pronounced "eye-square-see") is a two-wire bidirectional communication interface developed by Philips, which has protected the name; thus, other manufacturers refer to the same protocol differently. Atmel designates I2C as the "two-wire interface" (TWI). Many users may already be utilizing I2C on their PCs without awareness, as all modern motherboards feature an I2C bus for reading temperatures, fan speeds, and other hardware information. Unfortunately, this I2C bus is not externally accessible (lacking a physical connector), necessitating the development of an alternative solution. The Atmega8 datasheet provides a comprehensive description beginning on page 160; therefore, only an overview will be presented here for better understanding. In an I2C bus setup, there is always one master device and one or more slave devices. The master initiates communication and controls the clock. The two wires of the bus are designated as SDA (data line) and SCL (clock line). Each device on the bus must have independent power, similar to traditional RS232 communication. The two bus lines are typically connected through 4.7K pull-up resistors to a logical "high" (+5V for 5V ICs), creating an electrical "or" connection among all devices. A device pulls the line to ground when transmitting a '0' and leaves it "high" when sending a '1'. The master begins communication by sending a "start condition" followed by addressing the desired device. Each device on the bus has a unique 7-bit address. Subsequently, the master transmits a bit indicating whether it wishes to read or write data. The slave acknowledges receipt of this information by sending an acknowledgment bit (ack-bit). Thus, a total of 9 bits of data (7 address bits + read bit + ack-bit) are observed on the bus. Data is always transmitted in multiples of 8 bits (1 byte) and must also be acknowledged by an ack-bit, resulting in consistent 9-bit packets on the bus. Upon completion of communication, the master must transmit a "stop condition." The master must also be aware of the amount of data to be expected when reading from a slave; however, this can be managed within the user protocol by, for example, transmitting a zero byte at the end of a string to signify the absence of additional data.

The I2C bus operates using a simple protocol that allows for efficient communication between devices. The master device controls the timing and sequence of data transmission, ensuring that each slave device responds appropriately to requests. The two lines, SDA and SCL, are actively managed through pull-up resistors that maintain a high state when not being driven low by a device. This configuration allows for multiple devices to share the same bus without interference, as each device can be uniquely addressed by the master.

In practical applications, the I2C protocol is particularly advantageous in scenarios where minimal data transfer is required, such as sensor readings or configuration settings. The simplicity of the two-wire connection reduces the complexity and size of the circuit, making it an ideal choice for compact designs. Furthermore, the inherent flexibility of the I2C protocol allows for easy expansion, enabling additional slave devices to be added to the bus without significant redesign.

The implementation of I2C communication in microcontroller-based projects can significantly streamline the design process, reduce component count, and enhance reliability. By leveraging this efficient communication protocol, engineers can develop robust systems that meet the demands of modern electronic applications.A pre-requisite for this article is that you have the GCC AVR programming environment installed as described in my "Programming the AVR microcontroller with GCC, libc 1. 0. 4" article. If you want to avoid troubles with the installation you can of course use the AVR programming CD from When you use such an advanced device as a microcontroller

to measure analog or digital signals then you want of course interfaces to evaluate the data or send commands to the microcontroller. In all the articles presented here in the past we always used rs232 communication with the UART that is included in the microcontroller.

The problem is that this requires an additional MAX232 chip and 4 extra capacitors. Atmel suggests also that an external crystal osciallator is required for the UART communication to work reliably. In any case it is a lot of extra parts. and we can avoid them! The amount of data to transfer between PC and microcontroller is usually very small (just a few bytes).

Speed it therefore no issue at all. This makes the I2C bus/protocol suitable for this task. I2C (prounouce "eye-square-see") is a two-wire bidirectional communication interface. It was invented by Philips and they have protected this name. This is why other manufacturers use a different name for the same protocol. Atmel calls I2C "two wire interface" (TWI). Many of you might already be using I2C on their PCs without knowing it. All modern motherboards have an I2C bus to read temperatures, fan speed, information about available memory. all kind of hardware information. This I2C bus is unfortunately not available on the outside of the PC (there is no physical connector).

Therefore we will have to invent something new. The datasheet of the Atmega8 (see references) has actually a very detailed description starting on page 160. I will therefore present here just an overview. After this overview you will be able to understand the description in the datasheet. On the I2C bus you always have one master and one or several slave devices. The master is the device that initiates the communication and controls the clock. The two wires of this bus are called SDA (data line) and SCL (clock line). Each of the devices on the bus must be powered independently (same as with traditional rs232 communication).

The two lines of the bus are normally connected via 4. 7K pullup resistors to logically "High" (+5V for 5V ICs). This gives an electrical "or" connection between all the devices. A device just puls a line to GND when it wants to transmit a 0 or leaves it "High" when it sends a 1. The master starts a communication by sending a pattern called "start condition" and then addresses the device it wants to talk to.

Each device on the bus has a 7 bit unique address. After that the master sends a bit which indicates if it wants to read or write data. The slave will now acknowledge that it has understood the master by sending an ack-bit. In other words we have now seen 9 bits of data on the bus (7 address bits + read_bit + ack-bit): Next we can receive or transmit data. Data is always a multiple of 8 bits (1 byte) and must be acknowledged by an ack-bit. In other words we will always see 9-bit packets on the bus. When the communication is over then the master must transmit a "stop condition". In other words the master must know how much data will come when it reads data from a slave. This is however not a problem since you can transmit this information inside the user protocol. We will e. g use the zero byte at the end of a string to indicate that there is no more data. SDA H - /- /- /- L -/ -/ -/ -. SCL H - /- /- /- /- /- L -/ -/ -/ -/ -/ -. | START | 1 | 1 | 0 | 1 | 0 | One of the best things about this protocol is that you do not need a precise and

🔗 External reference