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

Use fixed integer types to enhance portability

If you have programmed anything with C, you should be familiar with common data types like char, unsigned char, int, unsigned int, long int, long long int, etc. It is tough to tell by the looks of the type how many bytes this variable takes on memory and how it looks in a different system. For instance, in 8-bit AVR-GCC compiler, int is a 16-bit type, while in ARM-GCC, int is 32-bit. So int size is dependent on platform, compiler, and runtime libraries. And switching between systems may trick you if you are not careful enough. You can always check the size of the variable by using sizeof() function. What to do if you need your code to be portable between different systems. Some great libraries could work on any system, including 8-bit, 16-bit, 32-bit, or 64-bit. To avoid this annoying problem, the ISO C99 standard introduced portable data types that are defined in stdint.h header file. If you open this header file of your default compiler tool-set you will find how things are organized. First of all, it checks…

Continue reading

Using Volatile keyword in embedded code

Volatile is probably the least documented keyword in most tutorials and books. This is the primary cause of most misuses and bugs related to it. If you already are programming microcontrollers, you probably know that volatile is always used on global variables that are accessed from interrupt service routines. Otherwise, code won’t work. After few requests, I decided to drop few lines about volatile keywords. This keyword is commonly used to tag memory type. We hear “volatile memory” and “non-volatile memory” when discussing computer hardware. As a quick reminder – “non-volatile memory” is a type of memory that stores its contents even when power is off. Such type of memory is EEPROM, Flash, and FRAM. This is easy from a hardware perspective. But what volatile keyword means in C or C++ code? This is an indicator (called qualifier) to the compiler that tells that this variable may be changed during program flow even if it doesn’t look like it be. This means that compiler must treat this value seriously and keep optimizer away from it.

Continue reading

Interfacing GPS Module with AVR

GPS modem is a device which receives signals from satellites and provides information about latitude, longitude, altitude, time, etc. The GPS navigator is more famous on mobiles to track the road maps. The GPS modem has an antenna which receives the satellite signals and transfers them to the modem. The modem, in turn, converts the data into useful information and sends the output in serial RS232 logic-level format. The information about latitude, longitude, etc., is transmitted continuously and accompanied by an identifier string. The connection of GPS modem with AVR microcontrollers shown in the circuit diagram. The ground pin of max 232 and serial o/p of the GPS modem is made standard. Pin 2 of MAX232 is connected to pin 3 of GPS modem, and pin 3 of max 232 is connected to pin 2 of the modem. This type of connection is called a serial cross cable.

Continue reading

Software Debouncing of buttons

Connecting a button as an input to a microcontroller is a relatively easy task, but there are some problems. The main problem is that switches bounce, i.e., when you press (or release) a button, it will often change level a couple of times before it settles at the new level. So if you, for example, connect the switch to a pin with an external interrupt enabled, you will get several interrupts when you press the button once. This behavior usually is not wanted. Even if the buttons didn’t bounce (with filtering hardware, for example), we still want to capture the event of a pushed button and take some action once for every button press, so we need to keep track of the state of the switch as well. One technique used in this tutorial to handle this is to check (poll) the button(s) periodically and only decide that a button is pressed if it has been in the pressed state for a couple of subsequent polls.

Continue reading

Interfacing DC motor to Atmega32

In a past tutorial, we saw how to control a servo using AVR. This tutorial will aim at interfacing a DC-geared motor with the ever-popular ATMEGA series. For the sake of simplicity, we will learn a way to interface the DC motor and not control its speed. DC Motors are small, inexpensive, and powerful motors used widely in robotics for their small size and high energy out. A typical DC motor operates at speeds that are far too high speed to be valid and torque that is far too low. Gear reduction is the standard method by which a motor is made meaningful. Gear reduce the speed of the motor and increases the torque. Choosing a DC Motor depends upon the application.

Continue reading