AVR microcontroller basic-2.

By IKTech May 29, 2017

The AVR microcontroller basics are in today’s episode. How to write a basic basic program and it can be loaded through simulation to see the effectiveness of the program.
How to Write a Basic Basic Program

 

In the past we have seen how to open a new project. Open a new project by following the direction of the past. Please choose atmega8 as the device when you open it.

Then you will get a code window like the picture below.

Enter the code below
#include <avr / io.h>
#include <use / delay.h> // for _delay_ms ()

Int main (void)
{
DDRB = 255; // initialize port B
While (1)
{

PORTB = 0b11111111; // Al the pins of PORTB will be high
_delay_ms (3000); // wait 3000 milliseconds

PORTB = 0b00000000; // Al the pins of PORTB will be low
_delay_ms (3000); // wait 3000 milliseconds

}
}

Click Build Solution from the Build menu. Then compile the .hex file to be created.

The .hex file can be found in C: \ Users \ my \ Documents \ AVRStudio 5.1 \ avrtutorial2 \ avrtutorial2 \ Debug

We will load this .hex file in microcontroller.
How to design simulation hardware at PROTEUS, take a look

Open the PROTEUS ISIS and click on Pick from Libraries as shown below.

Follow the instructions below. Search by keywords at atmega8, then select, finally click on the OK button.

The mouse pointer will hold the pencil size. Bring one click to the atmega8 design window.

Now double click on the microcontroller and show the location of the .hex file.

Click Play button. Follow the pictures below.

Then the code written by us will run.

In the above animation, we see that the pins that have PB0 to PB7 on the left are logical, then waiting for three seconds, then waiting for three seconds for logic load.
Review the program
#include <avr / io.h>
#include <use / delay.h> // for _delay_ms ()

Int main (void)
{
DDRB = 255; // initialize port B
While (1)
{

PORTB = 0b11111111; // Al the pins of PORTB will be high
_delay_ms (3000); // wait 3000 milliseconds

PORTB = 0b00000000; // Al the pins of PORTB will be low
_delay_ms (3000); // wait 3000 milliseconds

}
}
A library file has been called through #include <avr / io.h>, which is used in the program to work using the microcontroller’s input output port.
A library file has been called via #include <ut / delay.h>, which is used in the program to create time intervals with the _delay_ms () function.
Int main (void) {} ​​Since we have written a program using Programming C, so in order to write programs using traditional C, all code has to be included in the main () function. We did the same.

DDRB = 255; Here is the DDRB register for data port for B port. DDRB registers an 8-bit. 255 is given as the value of the DDRB registrar ie 1111 1111 in Binary, in this situation, all the pins output of Port B will be done. If port bit is to be used as some pin input, then all the places should be written as 0, in 1111 0000, pins from PA0 to PA3 will act as input and pins from PA4 to PA7 will act as output.
An infinite loop has been created through while (1) {}, which is conditioned in 1, ie this condition of the loop will remain true as long as our microcontroller works and what is written in this loop will be repeatedly re-cycled.

PORTB = 0b11111111; PORTB is a register used for B port, indicating the output status of that port. PORTB = 0b11111111 If all the ports of PORTB will be logic high, and the pins will be ignited when adding LEDs.

PORTB = 0b00000000; PORTB has all the logic logos of PORTB

_delay_ms (3000); It has been created in 3 seconds time.

 

Leave a Reply

Your email address will not be published.