Hello
In this Tutorial you know about how to turn on a led on avr Microcontroller i use AVR atmega32 but you can use different microcontroller

There are many way to write this program this is  One way to do this is to write
First you have to need Port Selection

DDRB = 0b00000001;

Lest go to explain
"DDRB" refers to the Data Direction Register for port B; "0b" is to tell the compiler that what follows is the binary expression of a number; and the "1" on the end denotes the pin 0 position. Recall that there are 8 pins for port B; pins 0 though 7. There are also 8 digits in our line of code. So each digit represents a pin on the port, and we can use the individual digits to specifically refer to any one of the pins in port B. So the '1' at the end of our code statement refers to the first pin in port B, which in this case is pin 0.We really don't need to get any more complex at this point, as this will be covered in much more detail in future tutorials.
AVR pin set
Now you have to set Pin Selection
PORTB = 0b00000001;
The only difference between this and the previous statement is that we are now using the PORT register. This register knows the pins of that specific port, and gives us access to specify the actual data value (logical 0 or 1) for these pins.

now you hove to need create a main function

Now we need to talk a bit about the overall structure of our program. All programs need a specified place to start the execution. It's like giving someone a set of instructions on how to make a cake without telling them which step to start on. The "main" function is the place where all C/C++ programs start executionint

 main(void)
{
}

In Main (Void) program to understand the DDR and PORT register information and how these work within the microcontroller, 

Now you have to need include statement to  be added that contains all of the information about the AVR microcontrollers. 
So write this program before  the main(void)
#include <avr/io.h>

When your  compilation process starts, the pre-processor portion of the compiler looks in the "avr" directory for the "io.h" file. The ".h" extension here indicates that this is a header file.


#include <avr/io.h>
int main(void)
{

DDRB = 0b00000001;
//Data Direction Register setting pin0 to output and the remaining pins as inputPORTB = 0b00000001; //Set pin0 to 5 volts

}

Now the direction of the pin0 is set to output, with a value set at 5v. But we are still not finished. We need to keep the microcontroller running indefinitely, so we need a routine to do this. This is called an endless (or infinite) loop. The infinite loop makes sure that the microcontroller does not stop performing its operations. I will explain this in more detail when we have stuff to do within this loop. There are several types of loops we can use for this purpose, but for this demonstration I will use the while loop. 

#include <avr/io.h>
int main(void)
{

DDRB = 0b00000001;
//Data Direction Register setting pin0 to output and the remaining pins as input
PORTB = 0b00000001; //Set pin0 to 5 volts
while(1)
{
//Code would be in here if it needed to execute over and over and over ... endlessly
}

}

Proteus simulation view

Proteus simulation video



Complete File Free download Click Here of click Here

Proteus Software free download  with Crick  Click Here





No comments

Theme images by Dizzo. Powered by Blogger.