Pages

Wednesday, July 15, 2020

Button with AVR ATmega16


Hardware:-

An LED is connected on PB0 pin & and a button is connected on PA0 pin such that when button is pressed, input value will be logic 0 else it will be logic 1

Theory:-

In this tutorial, we will light up the led when button pressed & keep led off when button is free.
In order to achieve the aim, we need to do following things:-
1)      Read the state of the button
2)      Update the state of LED according to button.

Read the state of the button:-
To read the button, first of all microcontroller pin on which button is connected needs to be declare as input. AVR have register with name DDRx ( Data Direction Register), where x can be A,B,C or D depending on what pin group we are using.  DDRx is a 8 bit register in which each bit is associated with one particular pin of group x ( LSB bit associated with pin0 of the group & MSB bit associated with pin7 of the group). If DDRx bit is defined as logic ‘1’ then pin will act as output pin & if DDRx bit is defined as logic ‘0’ then pin will act as an input.
In our case, as button is connected with PA0 pin, so, we have to declare as input pin:-
DDRA = 0b11111110;
As we are not going to use pins PA7 to PA1, so we can declare them as input or output, It doesn’t matter. I have declared them as output so that you can easily differentiate it with PA0 ( button pin ).

Till now we have only declare input pin, but we didn’t read the input value yet. In order to read input value of pins, AVR have register with name PINx, where x can be A,B,C or D depending on what pin group we are using.  PINx is a 8 bit register in which each bit is associated with one particular pin of group x ( LSB bit associated with pin0 of the group & MSB bit associated with pin7 of the group). If PINx bit is logic ‘1’ then pin is receiving 5V on it & if PINx bit is logic ‘0’ then pin is receiving 0V on it. Let’s take a variable to store the PINx value.
char input;
Note:- The reason I have taken char type input is that, both char & PINx are of 8-bit (1 byte).
Now lets the PINx value. As button is connected at PA0, so x = A:-
input  = PINA;
We are not done with reading the input value yet. The variable input is reading all bit of PINA, but we are interested only in PA0. To hide remaining bits value, let’s apply ‘&’ operation such that the result is only having PINA0 value:-
input = PINA & 0b00000001;

Update the state of LED according to button:-
To declare the output value of LED, first of all we need to declare LED pin as output:-
DDRB=0b00000001;
As we are not going to use pins PB7 to PB1, so we can declare them as input or output, I doesn’t matter. I have declared them as input so that you can easily differentiate it with PB0 ( LED pin ).
Now, in an infinite loop, keep LED ON when button is pressed & keep LED OFF when button is released.

while(1)
{
    if(input == 0b00000000)   // button is pressed
    {
        PORTB = 0b00000001;
    }
    else                                     // button is released
    {
        PORTB = 0b00000000;
    }
}

Now, we have discussed the different aspects of the program. It’s time to integrate all of them & right down a final program. But before that, lets simplify each step of the program:-



Step1:- Define frequency on which we are going to work:-

#define F_CPU 1000000UL
Actually, ATmega16 can work up to 20MHz. Also, this microcontroller can generate 1,4 & 8 MHz of frequency internally but if you want more frequency then you have to connect crystal oscillator externally ( on pin 12 & 13).

Step2:- Declare input-output library:-
#include <avr/io.h>

Step3:- In main() function, define output & input pins:-
DDRB = 0b00000001;
DDRA = 0b11111110;

Step4:- Declare a variable to store input (button) value:-
char input;

Step5:- In an infinite loop, check input value & set LED state accordingly:-
while(1)
{
    input = PINA;
    If(input == 0b00000000)   // button is pressed
    {
        PORTB = 0b00000001;
    }
    Else                                       // button is released
    {
        PORTB = 0b00000000;
    }
}

Final program:-

#define F_CPU 1000000UL

#include <avr/io.h>

int main(void)

{

    DDRB = 0b00000001;

    DDRA = 0b11111110;

    char input;

    while(1)

    {

        input = PINA;

        if(input == 0b00000000)   // button is pressed

        {

            PORTB = 0b00000001;

        }

        else                                      // button is released

        {

            PORTB = 0b00000000;

        }

    }

}



Click Here to download the code.

No comments: