Category Archives: AVR Tutorials

RS232 Communication – The Basics

RS232 is a asynchronous serial communication protocol widely used in computers and digital systems. It is called asynchronous because there is no separate synchronizing clock signal as there are in other serial protocols like SPI and I2C. The protocol is such that it automatically synchronize itself. We can use RS232 to easily create a data link between our MCU based projects and standard PC. Excellent example is a commercial Serial PC mouse (not popular these days, I had got one with my old PC which I bought in year 2000 in those days these were famous). You can make a data loggers that reads analog value(such as temperatures or light using proper sensors) using the ADC and send them to PC where a special program written by you shows the data using nice graphs and charts etc.. actually your imagination is the limit! Basics of Serial Communication. In serial communication the whole data unit, say a byte is transmitted one bit at a time. While in parallel transmission the whole data unit, say a byte (8bits) are transmitted at once. Obviously serial transmission requires a single wire while parallel transfer requires as many wires as there are in our data unit. So parallel transfer is used to transfer data within short range (e.g. inside the computer between graphic card and […]

Timers in Compare Mode – Part II

Hello and welcome back to the discussion on the TIMERs in compare mode. In the last article we discussed the basics and the theory about using the timer in compare mode. Now its time to write some practical code and run it in real world. The project we are making is a simple time base which is very useful for other project requiring accurate computation of time like a digital clock or a timer that automatically switches devices at time set by user. You can use it for any project after understanding the basics. We will have three global variable which will hold the millisecond, second and minutes of time elapsed. These variables are automatically updated by the compare match ISR. Look at the figure below to get an idea how this is implemented. Fig – Using AVR Timer to generate 1ms Time base.     Complete Code #include <avr/io.h> #include <avr/interrupt.h> #include "lcd.h" //Global variable for the clock system volatile unsigned int clock_millisecond=0; volatile unsigned char clock_second=0; volatile unsigned char clock_minute=0; main() { //Initialize the LCD Subsystem InitLCD(LS_BLINK); //Clear the display LCDClear(); //Set up the timer1 as described in the //tutorial TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); OCR1A=250; //Enable the Output Compare A interrupt TIMSK|=(1<<OCIE1A); LCDWriteStringXY(0,0,"Time Base Demo"); LCDWriteStringXY(0,1," : (MM:SS)"); //Enable interrupts globally sei(); //Continuasly display the time while(1) { LCDWriteIntXY(0,1,clock_minute,2); LCDWriteIntXY(3,1,clock_second,2); _delay_loop_2(0); […]

Timers in Compare Mode – Part I

Hi Friends, In last tutorials we discussed about the basics of TIMERs of AVR. In this tutorial we will go a step further and use the timer in compare mode . In our first tutorial on timer we set the clock of the timer using a prescaler and then let the timer run and whenever it overflowed it informed us. This way we computed time. But this has its limitations we cannot compute time very accurately. To make it more accurate we can use the compare mode of the timer. In compare mode we load a register called Output Compare Register with a value of our choice and the timer will compare the current value of timer with that of Output Compare Register continuously and when they match the following things can be configured to happen. A related Output Compare Pin can be made to go high,low or toggle automatically. This mode is ideal for generating square waves of different frequency. It can be used to generate PWM signals used to implement a DAC digital to analog converter which can be used to control the speed of DC motors. Simply generate an interrupt and call our handler. On a compare match we can configure the timer to reset it self to 0. This is called CTC – Clear Timer on […]

Interfacing Temperature Sensor – LM35

By interfacing different types of sensors with our MCU we can sense the environment and take decisions, in this way we can create "smart" applications. There are wide variety of sensors available. In this tutorial we will learn about a popular sensor LM35 which is precision centigrade temperature sensor. It can be used to measure temperature with accuracy of 0.5 degree centigrade. We can interface it easily with AVR MCUs and can create thermometers, temperature controller, fire alarms etc. Things Required   S. No. Item Image 1 28 PIN AVR Dev Board Contains the core AVR circuit including 5v regulator, reset, ISP. 2 Seven Segment Display Module Four common anode displays multiplexed with driver transistors and current limiting resistors. 3 Single Pin Female to Female Burg Wires Used to interconnect the two boards. And the sensor.   4 USB AVR Programmer To upload the program to the development board. 5 LM35 Temperature Sensor   LM35 LM35 by National Semiconductor is a popular and low cost temperature sensor. It is also easily available. You can buy one from here online. It has three pins as follows. Fig – LM35 Pin Configuration     The Vcc can be from 4V to 20V as specified by the datasheet. To use the sensor simply connect the Vcc to 5V ,GND to Ground and the […]

Multiplexed Seven Segment Displays – Part II

Hi Friends, In last tutorial we discussed about Multiplexing Seven Segment Displays. So you must be very much familiar with the theory. Now let us write the code and design a small project that will make you expert in using these displays in your own projects. We will make a system that can display any number between 0-9999 using four of these displays. We will design a function Print() that we can use on latter projects to easily write integers onto displays. Once we have successfully tested this function we can add to to any project without any problem. This concept of code reuse will make bigger project both easy to make and far less painful. In this sample project we will test our function by using it in a loop to print all numbers from 0-9999. for(i=0;i<10000;i++) { Print(i); Wait(); } Things Required S. No. Item Image 1 28 PIN AVR Dev Board Contains the core AVR circuit including 5v regulator, reset, ISP. 2 Seven Segment Display Module Four common anode displays multiplexed with driver transistors and current limiting resistors. 3 Single Pin Female to Female Burg Wires Used to interconnect the two boards   4 USB AVR Programmer To upload the program to the development board. These are all the things required to get started with seven segment […]

Multiplexed Seven Segment Displays.

We have discussed the basics of seven segment displays on our tutorial “Using Seven Segment Displays with AVR MCUs”. So you should be familiar with them. In this tutorial we will discuss about multiplexing of seven segment displays.Multiplexing is required when we want to interface 3 or 4 or even more such displays with MCUs since it we go for normal way it will require lots of IO port. So the smart way is multiplexing. Multiplexing achieved by tricking our eyes. Only one display is active at a time but we see all of them active. For multiplexing all the displays are connected in parallel such that if you activate any segment, say ‘a’ the ‘a’ segment of all displays glows up. But the trick is that we can switch on and off the “common” line of the displays under MCU control. So if we wish to light up the ‘a’ segment of display 2 we simply switch on display 2 first by applying proper level at the base of its driving transistor as shown in figure. Fig – Multiplexed Seven Segment Displays. If we like to display the digit say “123” on three displays first we select disp-3 by applying a “low” level at the base of transistor Q1 and output the code of required digit at the data […]

AVR Timers – An Introduction

Timers are standard features of almost every microcontroller. So it is very important to learn their use. Since an AVR microcontroller has very powerful and multifunctional timers, the topic of timer is somewhat “vast”. Moreover there are many different timers on chip. So this section on timers will be multipart. I will be giving basic introduction first. What is a timer ? A timer in simplest term is a register. Timers generally have a resolution of 8 or 16 Bits. So a 8 bit timer is 8Bits wide so capable of holding value withing 0-255. But this register has a magical property ! Its value increases/decreases automatically at a predefined rate (supplied by user). This is the timer clock. And this operation does not need CPU’s intervention. Fig.: Basic Operation Of a Timer. Since Timer works independently of CPU it can be used to measure time accurately. Timer upon certain conditions take some action automatically or inform CPU. One of the basic condition is the situation when timer OVERFLOWS i.e. its counted upto its maximum value (255 for 8 BIT timers) and rolled back to 0. In this situation timer can issue an interrupt and you must write an Interrupt Service Routine (ISR) to handle the event. Fig.: Basic Operation Of a Timer. Using The 8 BIT Timer (TIMER0) The […]

ADC (Analog To Digital Converter) of AVR Microcontroller

Most of the physical quantities around us are continuous. By continuous we mean that the quantity can take any value between two extreme. For example the atmospheric temperature can take any value (within certain range). If an electrical quantity is made to vary directly in proportion to this value (temperature etc) then what we have is Analogue signal. Now we have we have brought a physical quantity into electrical domain. The electrical quantity in most case is voltage.To bring this quantity into digital domain we have to convert this into digital form. For this a ADC or analog to digital converter is needed. Most modern MCU including AVRs has an ADC on chip. An ADC converts an input voltage into a number. An ADC has a resolution. A 10 Bit ADC has a range of 0-1023. (2^10=1024) The ADC also has a Reference voltage(ARef). When input voltage is GND the output is 0 and when input voltage is equal to ARef the output is 1023. So the input range is 0-ARef and digital output is 0-1023. Fig: ADC Theory Inbuilt ADC of AVR Now you know the basics of ADC let us see how we can use the inbuilt ADC of AVR MCU. The ADC is multiplexed with PORTA that means the ADC channels are shared with PORTA. The ADC […]

Using Internal peripherals of AVR MCUs.

Each AVR MCU has several internal peripherals that give powerful abilities to your projects. For example internal ADC can be used to convert analog value (say voltage output of some sensor) to a digital value that you can use. And a USART(Universal Synchronous Asynchronous Receiver Transmitter) can be used to connect your MCU with PC. (Example use – A PC serial mouse or A PC controlled Toy Rocket Launcher). This tutorial gives you overview of the process used to interface with these peripherals. Internal peripherals. Now you know the basics of peripherals, lets see what peripherals are available in ATmega16 MCU. Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode Real Time Counter with Separate Oscillator Four PWM Channels 8-channel, 10-bit ADC 8 Single-ended Channels 7 Differential Channels in TQFP Package Only 2 Differential Channels with Programmable Gain at 1x, 10x, or 200x Byte-oriented Two-wire Serial Interface Programmable Serial USART Master/Slave SPI Serial Interface Programmable Watchdog Timer with Separate On-chip Oscillator On-chip Analog Comparator Interfacing Technique. Fig: Using internal peripherals of AVR MCUs. A peripheral is connected to MCU by some special registers. The different registers of any peripherals can be logically of two types- 1)Data registers – which usually contains some data, say byte received from USART or […]

Using LCD Module with AVRs

When you start working with LCD modules you will start feeling the real power of MCU and your imaginations will be touching sky you will wonder how many exciting a powerful gadgets you can create and that’s so very easily. LCD Modules can present textual information to user. It’s like a cheap “monitor” that you can hook in all of your gadgets. They come in various types. The most popular one can display 2 lines of 16 characters. These can be easily interfaced to MCU’s, thanks to the API( Functions used to easily access the modules) we provide. LCD interfacing is just fun ! Fig: A 16×2 LCD Module Buy LCD Modules Online In India   PIN Configurations. The lcd modules has 16 PINs for interfacing. The details are given below. LCD Module Pin Configuration 1 VSS (GND Supply) 2 VCC (+5V) 3 VEE (Contrast Adjust) 4 RS 5 R/W 6 E 7 DB0 8 DB1 9 DB2 10 DB3 11 DB4 12 DB5 13 DB6 14 DB7 15 LED + 16 LED –   Connection with ATmega8/ATmega168 etc. The lcd module can be easily connected to the any 28 pin AVR MCU like ATmega8/ATmega168/ATmega328 etc. The diagram below shows the LCD connection with AVR MCUs port pins. Fig: Connection with 28 PIN AVR MCUs Connect the required pins of […]

DC Motor Control using AVR MCUs

Motor gives power to your MCU. Ya power to do physical works, for example move your robot. So it is essential to know how to control a DC motor effectively with a MCU. We can control a DC motor easily with microcontrollers. We can start it, stop it or make it go either in clockwise or anti clock wise direction. We can also control its speed but it will be covered in latter tutorials. A Geared DC Motor. DC Motor A DC motor is electromechanical device that converts electrical energy into mechanical energy that can be used to do many useful works. It can produce mechanical movement like moving the tray of CD/DVD drive in and out (you may like to try it out Go to My Computer, right click the drive icon and click “Eject”). This shows how software controls a motor. DC motors comes in various ratings like 6V and 12V. It has two wires or pins. When connected with power supply the shaft rotates. You can reverse the direction of rotation by reversing the polarity of input. Control with AVR MCUs As the MCUs PORT are not powerful enough to drive DC motors directly so we need some kind of drivers. A very easy and safe is to use popular L293D chips. It is a 16 PIN […]