Learn to setup your first microcontroller

Posted by Mohammed 15/10/2014 0 Comment(s)

Getting Started with Microcontrollers

Hello! Learn how to program your microcontrollers easily through our tutorial. In this tutorial, we are going to compile our first microcontroller program and then we will download it to the microcontroller

Why (do you need this)?

Familiarize with compiling and downloading new codes to the microcontroller

What (do you need)?

ItemQuantity
Pocket AVR Programmer1
Wires1
  
Download 
WinAVR (http://winavr.sourceforge.net/) 
Program Code (download here) 
USBTiny Driver (driver page here <ladyada>), (driver here) 

 

How (to make it)?

After downloading WinAVR and installing it, download the program code and extract it in your desktop or wherever you like, you will find two files, namely:

-main.c

-makefile

Makefile contains the main configurations which will be  used to compile and download the file, we’ll see how to play with these files later

Main.c is the actual c-language program, let us see what is inside it. Open the file using “Programmer’s Notepad” which should have got installed along with WinAVR

#include <avr/io.h>

#include <util/delay.h.>

int main(void)

{

            DDRB=0xFF; 

            for(;;)

            {

                        PORTB=0xFF;

                        _delay_ms(500);

                        PORTB=0x00;

                        _delay_ms(500);         

            }

            return 0;

}

We are not going to go through the details of programming here (it is going to be explained in another tutorial), if you are so excited, http://www.nongnu.org/avr-libc/user-manual/  is always your friend in AVR programming.

Let us modify the code a little bit to test that everything is working as it should, go ahead and replace the number “500” with “100”, so the code will be:

                        PORTB=0xFF;

                        _delay_ms(100);

                        PORTB=0x00;

                        _delay_ms(100);

Now, go to the “Tools” menu and select “[WinAVR] Make All”

winrar-archive

After a while, you should get the following message in the Output window:

------- end --------

> Process Exit Code: 0

> Time Taken: 00:33

Great, now the code is compiled, go back to the folder where you opened the main.c, you should see new files, one of them would be “main.hex”

main-hex

This is the file which will actually go to your microcontroller.

Now, let us go back to our breadboard, get your AVR Pocket programmer

Pocket-AVR-Programmer

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The cable that comes with the programmer has three connectors, at one end you have a 10-pin connector, followed by another 10-pin connector and lastly a 6-pin connector at the other end.

Connect the first 10-pin connector to the programmer, and connect the 6-pin connector as shown below

(Note, to identify pin 1 of the connector, find the small engraved arrow on the connector, see picture below)

pin-1-of-isp

We’ll connect five (5) more wires for what is called an ISP programmer:

ISP-Pinout

 

 

 

 

 

 

 

 

 

 

 

isp-pinout-connection

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Connect up a micro-USB cable to the programmer, now your PC is connected to the microcontroller.

programming-lines-connected-to-mcu

The programmer is connected now, let us download our new program, go back to your Programmer’s Notepad window and go to “Tools” then select “[WinAVR] Program”

download-the-program

You should now see the LED blinking faster than before.

With this, the doors of microcontrollers heaven have opened for you.

If you have any questions or comments, please feel free to post in the comment section below.

Leave a Comment