Programming AVR I2C interface

I2C interface (also referred to as IIC or TWI) is a widely used interface in embedded applications. A two-wire bus was initially used by Philips and become a standard among chip vendors. I2C bus consists of Serial Data Line (SDA) and Serial Clock Line (SCL). Communication is relatively fast, and short distances are mainly used to communicate between sensors, RTC, EEPROM, LCD. I2C protocol allows up to 128 devices connected to those two lines, where each of them has a unique address. Communication between devices is master and slave-based. Master generates a clock signal, initiates, and terminates data transfer. From an electrical point of view, I2C devices use open drain (open collector) pins. For correct operation, SDA and SCL lines require pull-up resistors. Typically 4.7kΩ resistors are used. The START signal initiates each communication and is finished by STOP. The master always generates these signals. START and STOP signals are generated by pulling the SDA line low while the SCL line is high. In other cases, when data is transferred, the data line must be stable during clock high and…

Continue reading

ADC on Atmega328. Part 2

After we’ve learned how to perform simple ADC conversions on AVR microcontroller we can move forward with more complex tasks. AVR ADC module has many valuable features that make conversions more robust without occupying MCU resources. Imagine that we need to sample analog waveform or audio signal. It has to be done precisely at defined sampling frequency like 20kHz. The only way to do this correct is to use auto-triggering with exact time intervals. Why not pass counting task to a timer? Let’s write Timer0 auto-triggered ADC conversions with ADC complete interrupt service routine.

Continue reading