Pages

Wednesday, July 15, 2020

LED Blinking with AVR ATmega16

Hardware:-

8 LED are connected on group B ( i.e PB0 to PB7).

Theory:-
In AVR, in order to blink led we need to do the following things:-

1)      Declare those pins as an output on which LED are connected
2)      Keep changing state of output ( change logic 0 to 1 & vice versa) with some delay.

Declare pin as output:-
In order to declare pin as a general-purpose output pin, 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 an 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 the pin will act as output pin & if DDRx bit is defined as logic ‘0’ then the pin will act as an input.
          In our case, as all led are connected with group B, so, we have to declare pins of the group be as output pins:-
DDRB = 0b11111111;

Define output value:-
In order to define the output value of pins, AVR have 'register' with name PORTx, where x can be A, B, C, or D depending on what pin group we are using.  PORTx is an 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 PORTx bit is defined as logic ‘1’ then pin will have logic high on the pin & if PORTx bit is defined as logic ‘0’ then pin will have logic low on the pin.

Let’s define logic high on all LED pins:-
PORTB=0b11111111;

Also, let’s define logic low on all led pins:-
PORTB=0b00000000;

Providing delay in the program:-
To provide a delay in the program, there is an inbuilt library “delay.h”. We need to declare this library as follows:-
#include <util/delay.h>

Now, to declare a delay, we have following function:-
_delay_ms(milliseconds);
So, to define 1 second, the value of “milliseconds” will be “1000”:-
_delay_ms(1000);


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 the 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 as well as delay library:-
#include <avr/io.h>
#include <util/delay.h>

Step3:- In main() function, define output pins:-
DDRB=0b11111111;

Step4:- In an infinite loop, toggle the state of output with some delay:-
PORTB=0b11111111;
_delay_ms(1000);
PORTB=0b00000000;
_delay_ms(1000);

Final program:-

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
    DDRB=0b11111111;
    while(1)
    {
        PORTB=0b11111111;
        _delay_ms(1000);
        PORTB=0b00000000;
        _delay_ms(1000);
    }
}


Click Here To Download the code.

No comments: