Sunday, December 09, 2012

EMBEDDED SYSTEMS TUTORIAL7- TIMERs IN AVR PART-1





Hiii…..friends as we discussed in previous articles that AVR MCU provides us facility of using some inbuilt advance features, TIMER is one of then and in this tutorial we are going to discuss about the TIMERs in AVR. Timer is a very useful and most widely used feature of AVR MCU it will be used in various applications and projects so one must have knowledge of TIMERs. Have a look….. 


INTRODUCTION

The TIMERs in AVR will be discussed in OVERFLOW MODE (TIMER0) and OUTPUT COMPARE MODE (TIMER1) in this tutorial we will discuss OVERFLOW MODE (TIMER0) only.

TIMER…??

A TIMER is nothing but a register . its size may bee 8 bit or 16 bit, which is called resolution of the timer. A 8 bit register can store numbers 0-255 , similarly a 16 bit register can store numbers 0-65535. The value of these registers can be increased or decreased without the CPU intervention, at a rate defined by the user. this rate can be can be equal to or less than the clock frequency of the CPU. The frequency at which timer register increases or decreases is known as Timer frequency.
We can use the TIMER registers to calculate the TIMER efficiently because the increment of register value by 1 means one clock has completed. So when the timer OVERFLOWS means the register value reaches to its maximum , we can inform CPU sending an interrupt. After reaching maximum value register again resets to 0. In overflow condition we must have to write a INTRUPPT SERVICE ROUTINE(ISR) to handle the event.
  



USING TIMER IN OVERFLOW MODE(TIMER0)

In ATMEGA 16 and 32 , there are three timers are available , of which simplest is TIMER0, with 8 bit resolution.

THE PRESCALER

The Prescaler is a mechanism for generating clock for timer by the CPU clock. As you know that CPU has a clock source such as a external crystal of internal oscillator. Normally these have the frequency like 1 MHz,8 MHz, 12 MHz or 16MHz(MAX). The Prescaler is used to divide this clock frequency and produce a clock for TIMER. The Prescaler can be used to get the following clock for timer. No Clock (Timer Stop). No Prescaling (Clock = FCPU) FCPU/8 FCPU/64 FCPU/256 FCPU/1024

TIMER0 REGISTERS.

Here is the description of some register of TIMER0 , which we are going to use-

TCCR0 – TIMER COUNTER CONTROL REGISTER

This register is used  configure the timer.


As you can see there are 8 Bits in this register each used for certain purpose. For this tutorial I will only focus on the last three bits CS02 CS01 CS00 They are the CLOCK SELECT bits. They are used to set up the Prescaler for timer.




TCNT0 – TIMER COUNTER 0

This register is the main counter in the TIMER0. The timer clock counts only this register as 1. It means tomer clock will increase the value of this 8 bit register by 1 every timer clock pulse.

TIMSK- TIMER INTRUPPT MASK REGISTER
   


This register is used to activate/deactivate interrupts related with timers. This register controls the interrupts of all the three timers. The last bit  BIT0 Controls the interrupts of TIMER0. 

TOIE0 : This bit when set to “1” enables the OVERFLOW interrupt.

PROGRAMMING


PRESCALER- 1024
CLOCK FREQUENCY- 16 MHz
Clock input of TIMER0 = 16MHz/1024 = 15625 Hz Frequency of Overflow = 15625 /256 = 61.0352 Hz if we increment a variable “count” every Overflow when “count reach 61” approx one second has elapse.  If count reaches 61 we will toggle PB0 which is connected to LED and reset “count= 0”.

SETTING UP THE TIMER0


  // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02)|(1<<CS00);
 //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);
 //Initialize Counter
   TCNT0=0;

OR

  // Prescaler = FCPU/1024
   TCCR0|=TCCR0|00000101;
 //Enable Overflow Interrupt Enable
   TIMSK|=TIMSK|00000001;
 //Initialize Counter
   TCNT0=0;



Now the timer is set and firing Overflow interrupts at 61.0352 Hz

The ISR


ISR(TIMER0_OVF_vect)
{
//This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
//CPU automatically call this when TIMER0 overflows.

//Increment our variable
  
   count++;
   if(count==61)
   {
   PORTC=~PORTC; //Invert the Value of PORTC
   count=0;
   }
}


COPMLETE SOURCE CODE


/* PROGRAM TO BLINK LE AT 0.5Hz CONNECTED AT PB0 */


#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint8_t count;

void main()
{
   // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02)|(1<<CS00);

   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

   //Initialize Counter
   TCNT0=0;

   //Initialize our varriable
   count=0;

   //PB0 as out put
   DDRB|=0x01;

   //Enable Global Interrupts
   sei();

   //Infinite loop
   while(1);
}

ISR(TIMER0_OVF_vect)
{
   //This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
   //CPU automatically call this when TIMER0 overflows.

   //Increment our variable
   count++;
   if(count==61)
   {
      PORTB=~PORTB; //Invert the Value of PORTB
      count=0;
   }
}




Now just compile the source code and burn the hex file generated in the ATMEGA 16, then implement the circuit as shown below on a breadboard or on your development board whatever you want to use, and you will find your LED blinking at a rate of 0.5 Hz.




 So we have studied the basic of TIMERs in AVR in next tutorial we will discuss about TIMER IN OUTPUT COMPARE MODE(TIMER1) .
I hope you found this article useful in case of any doubt, query, or critical view just leave a comment.



THNX……:)

Saturday, December 08, 2012

EMBEDDED SYSTEMS TUTORIAL6- INTERFACING 7 SEGMENT DISPLAY WITH AVR



Hii… friends its our next AVR tutorial this time we are going to discuss about interfacing of 7 segment display with a AVR. You must have seen 7 segment display in you house hold inverters, in a zerox machine or even on your microwave oven. So in this tutorial , We will discuss about 7 segment display  , its types and its interfacing with AVR along with source code so have a look…J

INTROUCTION
A seven segment display is the most basic electronic display device that can display digits from 0-9. They find wide application in devices that display numeric information like digital clocks, radio, microwave ovens, electronic meters etc. The most common configuration has an array of eight LEDs arranged in a special pattern to display these digits. They are laid out as a squared-off figure ‘8’. Every LED is assigned a name from 'a' to 'g'  and 'dp'and is identified by its name. Seven LEDs 'a' to 'g' are used to display the numerals while eighth LED 'dp' is used to display the dot/decimal. 
       

 PACKAGE AND TYPES

A seven segment is generally available in ten pin package. While eight pins correspond to the eight LEDs, the remaining two pins (at middle) are common and internally shorted. These segments come in two configurations, namely, Common cathode (CC) and Common anode (CA). In CC configuration, the negative terminals of all LEDs are connected to the common pins. The common is connected to ground and a particular LED glows when its corresponding pin is given high. In CA arrangement, the common pin is given a high logic and the LED pins are given low to display a number.



HOW IT WORKS

Now let us suppose a digit is to be displayed on the 7 segment. Suppose the digit is 1 in such case LEDS corresponding b and c should glow, so we can say foe each and every digit a specific set of LEDs has to glow . the LEDs to be golw with corresponding digits
Are as shown.

     

INTERFACING WITH AVR MICROCONTROLLER

Here is step wise process for inTERfacing a 7 segment display with AVR microcontroller( ATMEGA 16)

SETP 1
CICUIT CONNECTION
Here we are using a common anode 7 segment display.
Connect your 7 segment display with the atmega 16 as per the connections shown in the diagram below



STEP 2

SOURCE CODE

Create the new project in AVR STUDIO4 and compile the code written below-

Thursday, December 06, 2012

EMBEDDED SYSTEMS TUTORIAL 5- GPIO IN AVR



Hiii………friend this is next tutorial on AVR microcontroller, I know it took some time because I was trying to make it crystal clear for you all. In this tutorial we are going to discuss about basic GPIO( general purpose intput output) in AVRs.

All the interfacing of input and outputs of an AVR MCU is performed through PORTs. Before starting anything take a look of ATMEGA 16.





Here you can see there are 32 I/O pins grouped as A,B,C,D with 8 pins in a group. These groups are known as PORTs. You can see there are some special functions written in bracket  in front of these I/O pins, these are are the special function which our AVR MCU can perform with these specific pins other than I/O. we will discuss about all these functions in upcoming tutorials.


REGISTERS


All the processes in a MCU are performed through the help of registers. Register is a 8 bit location in the RAM. Here there are total 32x8 bit registers in ATMEGA16. Though the size of register is 8 bit that’s why it is called an 8 bit MCU.


HOW TO CONFIGURE INPUT /OUTPUT PROCESS IN MCU

Each PORT contain three type of registers.

DDRX- set weather a PORT is input or output.
PORTX- setting the value of output PORT.
PINX- reading the value of input PORT

DDRX( Data Direction Register)

With the help of this register we can make a port input PORT or output PORT.
To make a pin input or output we set its corresponding bit as

Set bit 1 to make it a output pin
Set bit 0 to make it a input pin.

So if we want to make all the pins of PORT A as input pins and all the pins of PORT B as output pins ,in our code we will write


DDRA=0x00;                          0x for hexadecimal
DDRB=0x FF;

OR

DDRA=0b00000000;                 0b for binary
DDRB=0b11111111;

Similarily if we want to make only first four pins of PORT C as input and last four as output, we will write.

DDRC=0XF0;
OR
DDRC=0b11110000;

PORTX (PORTX DATA REGISTER)

This register is used to set the value of a PORT, now a pin can be input or output let see both the cases

OUTPUT PIN

A pin be set to HIGH OUTPUT +5V  by setting it to 1, and can be set to LOW OUTPUT OV by setting it to 0.

For example we want high output on PC0,PC1 pins of PORTC and low output on rest of the pins of PORT C, then we can configure it as

PORTC=0x02;
OR
PORTC=0b00000011;

No the desired output sequence ia available on PORTC.

PINX (   Data read register)

This register is used to read the value of a PORT, If a pin is set as input then corresponding pin can be read as

0 for low input
1 for high output.

Now suppose PORT C is input port and we have connected a switch or sensor at PC0, then we can read the value of PC0 with the help of PINX register. You can read the state of key/switch by simply reading the PINC. It will be ‘1’ when  pressed and ‘0’ if  un pressed. 



CODING

...
if(PINC & 0b00000001)
{
 //Switch is pressed
 ...
}
else
{
 //Switch is not pressed
 ...
}
...
  
NOTE- all the input output functions needs the header file <avr/io.h> it must be added at the starting of the program.

In case you are not getting this code , I will suggest you to go through basics of C first. Download some tutorials from web or simply get any book on basics of c/c++.



Now its time to  use all this stuff in a code so we are going to implement a simple input output project in which a led will not  glow as output when switch will be pressed as input.
Here we are connecting the LED at PB0 and a push button at PC0. When we push the switch LED does not  glow otherwise it glows.

The source code is given below just compile this code get hex file and program your MCU with this hex file.


/*
 * IO_PROJECT.c
 *
 * Created: 10/16/2012 3:07:51 AM
 *  Author: ABHILASH
 */


#include<avr/io.h>
#include<util/delay.h>

int main(void)


{
 
                DDRB=0xFF;    // define PORT B as output PORT
                DDRC=0x00;   // define PORT C as input port
                  int s1=0;  // initializing and defining s1=0, acquire switch as s1
                while(1)      // create infinite loop
                {  
                                s1=PINC&0b00000001;  // acquire switch status connected at PC0
                                if(s1==0b00000001)   // check if switch is pressed
                                {
                                                PORTB=0b00000000;  // turn of led by setting  PB0 at low output
                                }
                                else
                                {
                                                PORTB=0b00000001;   // turn on LED by setting PB0 as high output
                                }
                                }
}



Now implement this circuit on the breadboard and you will we able to see the desired results…



Result will be like this.



.Switch pressed LED  not glowing.



 Switch not pressed LED glowing






some times there occurs the problem of switch debouncing, we will discuss about that in any upcoming tutorial.

I  hope this article was beneficial for beginners. in next tutorial we will discuss about some other basics of AVR. If you have any doubt, question or critical comment please post here.

THNX...:)