AdSense

Friday 19 July 2013

Arduino code on the ATMega

(Deutsche Version) The following post is not from Udo. I am honoured to write a guest post for his blog ;)

Programming an ATMega "by Hand" in C can be very time-consuming - especially when you need "special" features as SPI, I2C, etc. You often need to deal with cryptic code and register shifting. It is much simpler if you develop for Arduino. Due to the user-friendly commands, all wishes are easily implemented. Additionally, there are libraries for a lot of external hardware (e.g. ultrasonic modules or radio communication modules). The disadvantage of the Arduino is his high price. You don't want to spend 20 euro to buy a new Arduino for every new project.

It would be nice if the low price and the compact design of the ATMega would be combinable with the user-friendliness of the Arduino. Good news: This is easily possible!

I will explain everything using the example of an ATMega8. First, you need a microcontroller with a suitable circuit (power supply and external quarz). The circuit is explained on this Page.

Additionally, you need a programmer for the ATMega. I use an USBasp, which I bought cheaply on Ebay. The programmer has to be applied to the microcontroller - as described in the link above.

Now we can start with the programming. Therefor you need the Arduino development environment. It can be downloaded here. After the setup, you choose "File - Examples - 01.Basics - Blink". This example flashes a LED every second. To run the code, you have to add the LED to your microcontroller. Be careful, in the code, the pins are numbered for Arduino (in this example: Pin 13). The corresponding pin on the ATMega can be found in this overview. Pin 13 of the Arduino is Pin 19 of the ATMega8, which is PB5. You have to apply the LED to this pin.

Now you can compile the code and flash it onto the microcontroller. At first, you have to select the suitable programmer at "Tools - Programmer", in our case, this is the USBasp. Depending on the used microcontroller, you have to choose the suitable board. I use an ATMega8, so I choose at "Tools - Board" the "Arduino NG with ATMega8". To flash the program onto the microcontroller, I simply click on "File - Upload with programmer". The development environment compiles the code and flashes it onto the ATMega. And now you're done ;)

Following these instructions, you can also create complex programs. I attached radio modules via SPI to the ATMega8. How I did this will be explained in the next post.

No comments:

Post a Comment