lf365 A digital thermometer or talk I2C to your atmel microcontroller

Not rated 9,721

Circuit Image

The Atmega8 microcontroller from Atmel features numerous digital and analog input/output lines, making it an ideal choice for developing various measurement equipment. It is essential to have the GCC AVR programming environment installed, as outlined in the article "Programming the AVR microcontroller with GCC, libc 1.0.4." For convenience, the AVR programming CD can also be utilized. When employing a microcontroller to measure analog or digital signals, interfaces are necessary to evaluate data or send commands. Previous articles have utilized RS232 communication with the UART included in the microcontroller. However, this requires an additional MAX232 chip and four extra capacitors, along with an external crystal oscillator for reliable UART communication. These additional components can be avoided, as the amount of data transferred between the PC and microcontroller is typically minimal (just a few bytes), making speed a non-issue. This situation renders the I2C bus/protocol suitable for the task. I2C (pronounced "eye-square-see") is a two-wire bidirectional communication interface invented by Philips, which has protected the name, leading other manufacturers to refer to it differently. Atmel designates I2C as "two-wire interface" (TWI). Many users may already be utilizing I2C on their PCs without awareness, as modern motherboards feature an I2C bus for reading temperatures, fan speeds, and various hardware information. Unfortunately, this I2C bus is not accessible externally (lacking a physical connector), necessitating the development of a new solution. The Atmega8 datasheet provides a detailed description starting on page 160, which will be summarized here for clarity. In the I2C bus configuration, there is always one master and one or more slave devices. The master initiates communication and controls the clock. The two wires are designated as SDA (data line) and SCL (clock line). Each device on the bus must be independently powered, similar to traditional RS232 communication. The bus lines are typically connected via 4.7kΩ pull-up resistors to maintain a logical "High" (+5V for 5V ICs), creating an electrical "or" connection among the devices. A device pulls a line to GND to transmit a 0 or leaves it "High" to send a 1. The master begins communication by sending a "start condition" and addressing the desired device, which possesses a unique 7-bit address. Following this, the master sends a bit indicating whether it wishes to read or write data, and the slave acknowledges receipt by sending an acknowledgment bit (ack-bit). This results in the transmission of 9 bits of data on the bus (7 address bits + read bit + ack-bit). Next, data can be received or transmitted in 8-bit multiples (1 byte), with each byte requiring acknowledgment. Therefore, 9-bit packets will consistently appear on the bus. Upon completion of communication, the master transmits a "stop condition." The master must know the expected amount of data when reading from a slave, which can be communicated within the user protocol (e.g., using a zero byte at the end of a string to indicate no further data).

The I2C protocol's functionality is enhanced by its simplicity and efficiency, allowing multiple devices to communicate over just two wires. The SDA line carries the data, while the SCL line provides the clock signal, enabling synchronized communication between the master and slave devices. Each device on the bus must have a unique address, allowing the master to select which slave to communicate with. The acknowledgment mechanism ensures data integrity, as the slave confirms receipt of the data sent by the master. This protocol is particularly advantageous in applications where space and component count must be minimized, as it eliminates the need for additional components typically required for RS232 communication. The design considerations for implementing I2C with the Atmega8 include ensuring proper pull-up resistor values, managing device addressing, and establishing a reliable power supply for each connected device. Overall, the I2C protocol is a robust solution for interfacing the Atmega8 microcontroller with various sensors and peripherals in measurement systems.The Atmega8 microcontroller from Atmel has plenty of digital and analog input/output lines. It is the ideal device to develop any kind of measurement equipment. 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 y ou 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