How to: My First Micro Controller Project Part 2

continued from previous post, please read that for instructions on how to make the project. This post will continue with instructions for programming the 2313 and uploading the program to the 2313, as well as how to debug the code.

Section III: Programming:

1. First, make sure you have everything you need to begin programming, this includes an attiny2313(the one your using for this project), a cradle for the 2313, and a programmer to connect the cradle to the computer. Also make sure you have all the necessary software(see last post for links on where to buy/make/download these).

2. Once you have everything set up and running(see last post for links to drivers for usbtinyISP), place the 2313 into the cradle, connect the 6-slot ribbon cable into the cradle, and the USB connector into the computer. Now we can begin programming.

3. Open AVRStudio, and start a new project, make sure its an AVR GCC project, and name it whatever you want, though I recommend blinky or LED blink. Make sure AVR simulator is selected, and under device select ATtiny2313, the micro-controller we are using. After this you should have a few things open.

4. Now we can begin writing the program. At this point, if you don’t already know much about C or C++, I suggest you learn more about it before continuing this tutorial as I will not go over each part of the code in depth. A good place to learn about it is this book here(link), however if you aren’t interested in how the code works, skip to the end or the tutorial, I have placed all the code there.

5. First, place the include files avr/io.h and util/delay.h (should look something like #include <avr/io.h> and on the next line #include <util/delay.h>

6. Next we will begin the main loop, start by declaring int temp and setting DDRB equal to temp(should look like int main(){ and on the next line int temp; and on the next line temp = DDRB;). This will set all the pins of Port B as outputs, meaning that 5 volts will come out of them if and when PORTB is also set to temp.

7. Now begin a while loop, and within that set PORTB equal to temp(should look like while(1){ and on the next line PORTB = temp;)

8. Now for the main part of the program. Begin an if statement, set the condition to temp = 0xFF, then as a statement put temp = 0x00. Now place an else statement and then an if statement with 0x00 being the condition, the statement for this should be temp = 0xFF. (should look like if(temp == 0xFF) and on the next line place temp = 0x00; then on the next line place else if(temp==0x00) and on the next line place temp = 0xFF;)

9. Now add a millisecond delay, and in the parenthesis place how many milliseconds you want before it switches.(should look like _delay_ms(#ofmilliseconds you want to delay);)

10. Now finish off the program with a return 0; and a closing bracket.(should look like return 0; and on the next line place a }   )

ALL THE CODE:

in case you messed up or didn’t understand me earlier, here is all the code from the program:

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
int temp = 0xFF;
DDRB = temp;
while(1){
PORTB = temp;
if(temp == 0xFF)
temp = 0x00;
else if(temp == 0x00)
temp = 0xFF;

_delay_ms(100);

}
return 0;
}
Section IV: Debugging the Program:

1. Now that the program is all done, its time to upload, I’m going to assume that you are using the cradle I linked to as well as the usbtinyISP I linked to in Section I.

2. Now, in AVRStudio, click build+run(or just press Ctrl and F7). This will compile the program and begin to debug it.

3. Now you can use the I/O view(usually on the right side of the screen) to see what is happening in the program. I recommend looking at the register named PORTB by clicking on it.

4. Now to advance through your program, step by step, press F11. keep pressing and you will notice that the squares keep turning from black to white, this displays weather or not the pins of port B are outputting 5 volts or not.

5. once you are finished with debugging you can close the AVRStudio window, you can always return by pressing CTRL + F7 whenever you are back in your program.

Section V: Uploading the program:

1. The first step here is to again make sure that everything is connected and working, see section IV for more info.

2. Now open a command prompt(click on start -> run and type in cmd.exe). Don’t be scared of this, I will offer help in the comments if you need it.

3. Now you will need to navigate to wherever the .hex file of your program is(its the same place as the .c file, should be C:\documents and settings\your name\projects, but it may be different for you).

4. within the command prompt type in avrdude(this is one of the programs included in WinAVR tools) then -p t2313 then -c usbtiny then -U f:w:NAME OF PROGRAM.hex and replace NAME OF PROGRAM.hex with what you named your program, don’t forget the .hex though. Now press ENTER.

5. A bunch of stuff should pop up and some bars should go across the command prompt, if it doesn’t work post in the comments. If it does work, it should give you some message saying that its done. Now just plug your 2313 into the same spot on the breadboard(see Section I)(make sure the dip is pointing towards the top). Plug in your battery and it should work, if not post in the comments with any issues you may have, and please give some info to, it helps.

Advertisements

~ by emudeath on April 4, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: