Saturday, 22 November 2014

Programming Atmega16 - LED Blink

Now that we have everything in place we will begin writing our first program.
In this program, we will blink an LED.

Circuit Diagram

As you can see in the circuit diagram, the anode of the LED has been connected to pin 22(that is PC0), through a resistor of 1K ohm. The cathode is connected to the ground. Therefore, to light up the LED, we need to send a HIGH signal on the PC0 pin. This will forward bias the LED and it will start glowing.

STEP 1 : Programmers Notepad:
Open programmers notepad. Write your code here. Start by including the header files and declaring input and output ports.
Below is an example code that you can use:

This code will turn the LED on and off in intervals of 1 second (1000 ms).

We need to include avr/io.h for all input and output operations.
util/delay.h is included to make use of the delay function.

All programs must contain a main function. Inside the main function, we first declare PORTC as output. This is done by use of the DDRx command. DDR(Data Direction Register) is used to define input or output. If the DDRx register is set to HIGH, it defines a port as output. If it is set as LOW, that port acts as input port.

PORTx command is used to write a logic level on the desired port. We write HIGH to PORTC to make all pins high. _delay_ms() is used to provide a delay. The parameter within this function determines the time delay. 1000 represents 1000 milli seconds. We then write LOW on PORTC for another 1000 ms. This will repeat indefinitely as these statements have been put inside an infinite while loop.

Once you have written the code, save it in a folder. Save the file with the extension '.c'. Minimize the programmers notepad.

STEP 2: M-FILE:
Click on start, and search for Mfile. Open this application. This application was installed along with programmers notepad, within the winavr package.
Mfile application
The Mfile application should be similar to the above picture. Now perform the following steps:
  1. Click on Makefile -> Main File name
    Type the same name as the file you saved in programmers notepad (without the extension)
  2. Click on Makefile -> MCU Type -> ATmega -> atmega16
  3. Click on Makefile -> Output format -> iHex
  4. Click on Makefile -> Programmer -> stk500v2
  5. Click on Makefile -> Port -> usb
  6. Click on File -> Save as
    Save it as the name Makefile in the same directory as the .c file that you saved earlier.
STEP 3 : Programmers Notepad:
Once you have saved the Makefile, you need to compile and build the program. Open programmers Notepad and click on Tool -> Make all.
This will compile and build your program. If any error is found, it will be displayed at the bottom of the screen. Incase of any errors, correct your code and compile and build the program again from the tools menu.

STEP 4 : AVRdude-GUI :
Now, connect the programmer to you circuit and your computer. 
Once your program has been successfully compiled and built, open the AVRdude-GUI application.
Under the 'Setup' tab, choose programmer as usbasp.
Under the 'Programming' tab, select target device as Atmega16. File format to use as Intel Hex.
Under the FLASH option, browse to the folder where you saved your code file. Here, you will notice many file have been generated after compilation. Select the file with the .hex extension.
Click on program.


You have now successfully programmed the Atmega16. You should be able to see a blinking LED.
You can now write different codes and have numerous LEDs connected to your microcontroller.

The above steps will be used for programming the atmega16 throughout this blog. We will be referring to this as burning the program in all future posts.

1 comment: