©2019 Robotics for COMPLETE Beginners

18

Flashing the Program

 

This is the lesson where we are going to weave everything together.

Now that you know what a C program consists of, its time to flash.

I assure you, it's completely legal.

Flashing the program to the AVR is another way of saying we are going to move the program from the computer to the AVR chip.

The way we do this is simple but can appear complicated to newcomers who will often get mixed up and encounter "strange" errors in the process.

Don't worry if this is you.  I've been there...for about 3 years.

So, hopefully this tutorial will make this clear enough that you don't pull your hair out wondering what went wrong.

Before we can flash the program we need to make sure our program is saved as a C file.

In Programmer's Notepad or whatever other program you are using (You COULD use the Arduino program) make sure you save the file with a .C at the end.

So you should have a folder on your computer where all your programming related stuff is going to be at, this is important when you start making header files that you just learned about.

You save the C file which is your program to a project folder. Give it the same name as the C program.

Next, we need to generate a Makefile into that same folder that your C program is in.

The Makefile is great because you can copy it for multiple projects.

It is a text file that you can make or use from someone who has already made one.

It contains a list of settings for needed to flash your program successfully.

When you open a Makefile you will see the type of programmer, the type of chip being flashed to, the name of your program and other information.

The Arduino software does this for you after you choose which type of board you are uploading your program to which is great for beginners or lite hobbyists.

But you and I are a different breed.

When you first lay eyes on a Makefile you will feel intimidated, don't.

Most of what is written is not important to know anyway and there will likely be more comments explaining what the different options for settings do.

You can download the Makefile I use here. I got it from Elliot Williams which he provides for reuse in his INCREDIBLE book AVR Programming: Learning to Write Software for Hardware.

I cannot recommend these books enough.

I have a few of the Maker series and they produce fantastic reference and tutorial books!

When learning such highly complex and technical information books on the matter become holy scrolls containing invaluable magical spells.

Definitely consider getting some of them as you learn.

Make sure to actually READ them as well..that helps.

These are the books that have helped me tremendously.

If you click on "Properties" in Windows you will notice the file type is just "File", there are no formats attached to it.

 

Also, notice there are 6 major sections for settings:

 

1.  MCU: Your chip type.

     F_CPU: Your clock speed, this is where many beginners will fail, remember we are at 1/8 of the full potential clock speed if you are using the single AVR chips from the Microchip manufacturer. If you are using Arduino, it has a crystal oscillator that speeds up the chip to 16Mhz so make sure you have the correct clock speed here).

     BAUD: This is the serial speed. This is for communicating over the serial line, such as hooking up the chip to talk with your PC which you will learn later on. For now, leave it at the default 9600UL

 

 

2.     Programmer type:  Make sure you have this set correctly to the type you have. These are all included in the AVRDUDE download you installed previously. Some very nice people have set up the protocols for you!  

 

3.     Program Location:  See the note, you don't need to change this if you installed the WINAVR/AVRDUDE software enabling the PATH along with it correctly.

4.     The code to convert .elf which is like an edited rough draft of your code to .hex file with is like a final revised draft of your code.  Do NOT change anything here.  Just witness its majesty.

5.     USB Flash Programmer details: You don't need to do anything here really.

6.     Fuse Settings:  This is an advanced topic that you don't need right now and should NOT change. Hands off!

In software such as Programmer's Notepad you will find a button in the Tools tab at the top that says "Make All".  Click that.

What that does is create the necessary Hex file version of your program that will be used to flash through your USB programmer onto the chip.

If you go into your program's folder you will see there are also .elf, .map, and .o files.

Your .C file should also be there as well as your Makefile.

One big happy family.

Now, if you make any changes to your code you need to click the Make CLEAN button and then Make ALL button after that.

This erases the previous .elf, .map, and .o files that were based off of the old code and generates NEW files that match your new code.

OK!

Now it's time to open up AVRDUDESS my dudes.

The whole reason for using this software is so you don't have to manually type all of these settings in the Command Prompt in Windows or the Terminal in Apple Computers each time you try to flash the program.

 

This gets very annoying and wastes TONS of time if you have to fix errors in your code or are just having errors trying to flash the code.

 

AVRDUDESS is a simple graphical interface made for free by some great people to save you all this headache.

 

You can see the version I have installed at the top.

I have not highlighted anything because I don't want you to skip over anything.

I want you to take it all in.

 

Under that you can choose which type of USB programmer you have. I chose the usbtiny here.

 

Under that is "port", this should be USB not Com1,2,3,4..etc.

 

Next to that is the Baud Rate which you don't need to touch (remember we set it at 9600UL in our Makefile).

 

There is a bit clock option which you don't need.

On the far right side there is the chip name which you can auto-detect once you have the chip correctly wired up to your usb programmer.

 

Let's do that right now!

 

Remember, in the USB Programmers lesson we learned how to hook up our USB Programmer to our AVR chip.

Here is how it should look again.

So, you should have the AVR chip wired up to the USB programmer correctly and have already created your folder containing your appropriate files that you learned about in the Header Files lesson.

Now let's go back to AVRDUDESS.

Look at the middle section labeled "Flash".

You can see the path to the .hex file for my C program I created called "skynetDemo".

It is on my C drive in my PC I named "Black Beauty" because it is a giant rig colored black and red.

I love it.

So, in my PC you can see my program is located in "Documents" in a folder I made named "AVR".

In my AVR folder I created another folder with the same name as my program's name "skynetDemo".

When I created my files with Programmer's Notepad with the "Make All" function it created the hex file you can see here called skynetDemo.hex.

Get it?

Ok!

Go ahead and click on the " ... " tab next to the white box that will display your path.

Find the hex file in your program's folder on your computer now.

It's really very simple but it's VERY common to get lost.

One other thing you need to know but is a bit advanced is the "Fuses lock bits" area on the right side.

Each AVR chip is different but setting the bits to what you see here is what you need to do in order to set the chip up correctly, these bits control the speed that the AVR clock operates at.

Remember it comes from the factory at 1/8 the maximum speed right?

I found these values online but if you make a mistake you can brick your chip and make it unusable or require some difficult way to unbrick them.

Here is a great website for the AVR chips to see what values you need to set for these fuses depending on what speed you want, etc.

Trust me, if you have the 328p chip, just copy the values I have listed here.

If you have a different chip, PLEASE look up the correct values.

The bits I have set for the 328P chip are shown here.

 

Now look at the grey box right above the large black window.

It reads: -c usbtiny  -p m328p  -P usb  -U flash:w:"C:\users...":a

This is what you would normally have to type yourself in the Command Prompt in Windows or the Terminal in a Mac.

See how long and annoying that is?

Imagine trying to flash the chip but it doesn't work the first few times, can you imagine how much time that would take?

This is why AVRDUDESS is so great!

You can download a simple ledBlink.c   program I have provided here.

Go ahead and download that code and put the file anywhere on your computer or just leave it in your "downloads" folder.

Open the Makefile I provided and make any necessary changes to it that you learned about and save it.

Now open up your "Programmer's Notepad" or similar program.

Open the C program file I included and then "Make All" to generate the necessary files.

Hook up your AVR, LED, USB Programmer to your PC and the 6 pins wired correctly to your AVR chip that you learned how to do.

Now, open up AVRDUDESS and select the type of programmer you have, set your Port to USB, attempt to detect the chip, then select the hex file you just made.

When you are ready, go ahead and click the "Program!" button.

You will see the progress in the black window below.

If something went wrong, 99 times out of 100 it is your wiring, your wiring is mixed up or you need to swap a wire.

1 time out of 100 it is the fuse bits or other config not set up correctly so...

RELAX, take a breath and try again.

Even experienced programmers will mix up wiring or forget to select the correct port etc. so don't feel bad.

Here are the steps in picture format as alot of us learn better visually, I completely understand.

I am primarily a visual and kinesthetic learner myself.

Get my favorite books on programming AVR chips I use.

Equip yourself with the most dependable, no hassle, quality usb programmer I use daily.

Make sure you have a basic kit. Arduino is fine, it contains the removable AVR 328p chip we will use.

 

I started with something very similar.

Summary

Of course you made it to the end, congratulations just for looking over that all!

If you are having trouble, I can't stress this enough: even experts run into problems flashing chips from time to time and the three main causes are:

1.  Bad or wrong wiring

2.  Not setting AVRDUDESS up correctly for their chip

3.  Messing up the files

If this happens and you troubleshoot up to #3 then go ahead and delete everything except for the Makefile and the .c file.

 

Then, try to "Make All", generate your various files, and try flashing again.

I made a ton of mistakes and felt so lost but it felt so rewarding once I got it working and realized I was making it much much more complicated than it really is.

Have CONFIDENCE.

Take BREAKS.

Good luck!

See you in the next lesson where we will start to design our first robot.