AVR Tutorials Part VI - My First Program on AVR to Light Up an LED

Now we will see our first program to light up an LED. So assemble the circuit as shown.



The output from MUC is 5V. So to limit the current we are using a 330 ohm resistor in the circuit.Before going to the first example, let us get familiarized with the three types of basic port operating registers for AVR:
  • DDRx (x = A / B / C / D ): It is called Data Direction Register. It is used to set whether a particular bit in a port is being used for input or output. If we give a high value for a bit then that bit will be used for out putting something in the later programs. Eg: DDRA = 0b00001111;
  • Here the first four bits (ie. bit 0 to bit 3) of port A is being used for outputting something and the last four(bit 4 to bit7) is being used for inputting something.
  • We could also write similarly in hexadecimal as: DDRB = 0xf6;
  • PORTx (x = A / B / C / D ): It is the Register which we use to output something into a port. As an example if we have set DDRA to have the bit 0 as output and if we want to set that to high we write as: PORTB = 0x01;

Next we are going to write the program.

  • Goto start-> Programmers note pad
  • Select C/C++ as shown below




Write the program as shown below.

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

DDRB = 0b00000001;// Setting Pin0 of PORTB as output
PORTB = 0b00000001; // assign a high voltage to pin 0 of PORTB
while(1)
{

}

}

  • Now create a folder to save your project any where in your drive and save this file as main.c. Next we have to configure what is called Make file.
  • So goto Start->Win AVR-> Mfile
  • Click on Makefile in tab and do the following
  • Select MCU type as Atmega 16 (if you are using another MUC select that)
  • Select any programmer from the list. Click on enable editing of make file. Now change Programmer to UsbTiny

  • Select Port as USB
  • Select main file name from makefile tab. By default it is main.c (if you have saved your program in another write your file name their instead on main.c. So it is convenient to put main.c as file name always. This is really important )
  • Now save your make file in the folder where you have saved your program file main.c (location is important)

Now we are going to transfer our program to MUC.

  • Open your program file in programmers notepad
  • Goto Tools-> [Win AVR] Make all. Check whether any error is indicated in the status bar if not
  • Goto Tools -> [Win AVR ] Program

If everything is ok the LED will lit up. You are done !

In coming tutorials we will see more about Programming in Atmega 16. Speak your questions , doubts, opinions etc. about this tutorials as comments below!

Thank you ! :-)

Post a Comment Blogger

  1. Mohammed fasil17.9.12

    when is your next tutorials coming ? this only basic na ??

    ReplyDelete
  2. philip mathew21.9.12

    this is just basics

    ReplyDelete
    Replies
    1. yes ! More tutorials will be coming soon :-)

      Delete
  3. Where can I get the full documentation on functions provided in C for programming ATmega ?

    You used PORTB and DDRB. More info on these ?

    Thsnks

    ReplyDelete
    Replies
    1. I didn't find any C Instruction Set Rahul. But you check this
      http://support.atmel.no/knowledgebase/
      avrstudiohelp/mergedprojects/avrasm/Html/
      ATmega32_ATmega16_instructions.html

      Delete

 
Top