Timers2

14- AVR ATmega 16 Tutorials- Timers & Counters || Part 2 || Practical Demonstration Leave a comment

Hello everyone and welcome back to the Blog.

In the last post, we talked about timers, their types and utilisation in embedded systems. Therefore in this one, we are going to implement them on a practical project. We are going to connect three LEDs(Green, Red and Blue) with the microcontroller. We will then toggle the Green one at the rate of 500 milliseconds, the Red on at 250 milliseconds and the Blue one at 100 milliseconds. We are going to do this using timers and counters and not the delay statement that we had been using up until now. In fact, we will use all the three Timers present in the ATmega 16 controller in this one project, so that it will be easier for you all to understand. So, without further ado let’s get into it.

The first thing that we need to do is create the circuit for this project. Now, this is fairly simple for this case as all we are doing here are connecting three LEDs to the controller. I selected PORTA to do this but you can choose any other PORT of your choice. The timers and counters functionality is universal and works with everything. So I am going to connect three registers with PINs 0,1 and 2 of PORTA and connect the positive leads of the LED with these registers. The negative leads will then go to the ground rail. We can use 330-ohm registers for this scenario. Actually, the value of resistance required for each LED depends on the LED colour and size and there is a specific formula to get it but whenever in doubt 330 ohms is a good value to use with LEDs.

Circuit Diagram

circuit avr 14
Circuit Diagram

With the circuit out of the way let’s get into the programming part. I would ask you to go through the video below where I explain everything in details.

Video

Now you should have a clear idea of what’s happening in the code. I have also provided the entire code for your convenience below. Feel free to modify it as you see fit.

Code

/*
Title: External Pin Interrupt INT0
Author: Samiran Patra
Target: ATmega16 Microcontroller
MCU_SPEED: 1000000 Hz

Note: Use makefile for compilation.
*/

 

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

int count=0;

int main(void)
{
     DDRA = 0xFF;                           //Setting data direction for the LEDs
     PORTA = 0x00;                          //Setting default voltage for the LEDs at the start
     sei(); //Enabling Global Interrupts

     TCCR1B |= (1<<CS10)|(1<<CS12);         //1024-bit Prescaler mode selected and Timer 1 is ON
     TCCR0 |= (1<<CS00)|(1<<CS02);          //1024-bit Prescaler mode selected and Timer 0 is ON
     TIMSK |= (1<<TOIE0);                   //Enabling Overflow Interrupt for Timer 0
     TCNT0 = 12;
     TCCR2 |= (1<<CS20)|(1<<CS21)|(1<<CS22);//1024-bit Prescaler mode selected and Timer 2 is ON
     TCCR2 |= (1<<WGM21);                   //Selecting CTC mode from the Waveform Generation Table
     TIMSK |= (1<<OCIE2);                   //Enabling Compare Interrupt for Timer 2
          //(977/1000)*100 = 97.7 ~ 98
     OCR2 = 98;
     while(1)
     {
         /*
         if(TCNT1 >= 10000)
         {
              TCNT1 = 0;
              count++;
         }
         // 100 * 10000 = 1000000 (1 million)
         if(count >= 100)
         {
              PORTA ^= 1<<0; //Toggling the Green LED connected to PIN 0 of PORTA
              count = 0;
         }
         */

         if(TCNT1 >= 488)
         {
              PORTA ^= 1<<0; //Toggling the Green LED connected to PIN 0 of PORTA
              TCNT1 = 0;
         }   
     }
}

ISR(TIMER0_OVF_vect) //Interrupt Service Routine for Overflow Interrupt
{
    PORTA ^= 1<<1; //Toggling the Red LED connected to PIN 1 of PORTA
    //Interrupt is triggered when counter reaches to 255
    //977 / 4 = 244.25 ~244
    //256 - 244 = 12
    TCNT0 = 12;
}

ISR(TIMER2_COMP_vect) 
{
    PORTA ^= 1<<2; //Toggling the Blue LED connected to PIN 2 of PORTA
    TCNT2 = 0;
}

Download the Makefile needed for compilation and copy it into the folder where you have saved the source file. Modify the file as per your controller and C file before compilation.

Shop Smart with us for Best Prices:

If you want to continue on with this series I would ask you to visit our YouTube channel and subscribe to receive continuous notifications. You can also subscribe to our Newsletter below to get email notification of each post as and when they are posted.

You can also follow us on social media platforms such as  Facebook and Twitter.

Thanks a lot for reading through this post. Feel free to follow through this series and comment your queries in the comment box below. Also feel free to contact us regarding any out of stock product and we will try to get back to you as soon as possible.

Leave a Reply