header
 
menu_header
menu
home
about me
my work
tutorials
gallery
download
feedback
links
blog

Output Section

So this is the second part of the tutorial. Now we go to a simple program to see the power of the ATmega32. We want to write a simple program to blink a led. So at first look at the code.

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

int main(void)
{
	DDRC = 0xFF; //output mode portC

	while(1)
	{
		PORTC = 1; //light up the led
		_delay_ms(500); //wait for 500ms
		
		PORTC = 0; //light down the led
		_delay_ms(500); //wait for 500ms
	}
return 0;
}
						

At first I declare two header files. The first one(avr/io.h) is for input, output operation on the port. Early I said that there are four I/O ports. At the second line I define the frequency of the cpu(for ATmega32). Then for delay operation another header file is included.

Now dive into the main function. At first you have declare a PORT as an input or output. Every port has 8 pins(2^8 = 255). So to make a port as output, put all ones that is 0xFF. If you want to use a port as input puts all zero that is 0x0. You can use a port for both input and ouput(We will see it later). The meaning of DDR is Data Direction Register. The 'C' suffix is for PORTC. So DDRC means that we want to use PORTC as output mode.

To light a led +5V is given at the positive pin and the other pin is connected with the ground (a led has two legs, the longer one is positive). So we connect the positive pin with the ATmega32 and ground the other pin. The ATmega32 needs power to run it. +5V is enough and necessary for it. The tenth pin of ATmega32 is connected with the +5V voltage source and the pin 31 is connected with the ground. So that's all to light a led on and off.

ledbasic

 

So it is the diagram of our design. Compile the code which is given above it will generate a hex file under default derectory where you save your code. Burn the hex file with bunner/programmer and connect led with PORTC. Give power(+5V) at pin 10 and ground at pin 31. Now the led will blink after every 500ms.

In the next tutorial we will see how to take input from a port.

Back   Next



Name*
Email Address*
Comments
 

 

comment

error in connect