Programming the Arduino with Atmel Studio

Arduino is easy and simple, but comes with limitations

In my previous post I described why I choose Arduino as the platform for my thesis in electrical engineering. The Arduino is a great introduction to electronics, but there’s a lot more to microcontroller programming. The Arduino IDE and its whole hardware and software ecosystem makes it very easy to accomplish complicated things, without understanding how the underlying technology actually works. It gives you the confidence to take off, but if you really want to get into microcontroller programming, you have to go deeper. Most Arduino boards use an Atmel AVR chip, for example the heart of the Arduino Mega is an ATmega2560. You might think, that programming the Arduino is the same thing as programming the chip, but it isn’t. When you’re programming the Mega using the Arduino software, you’re using code that only works inside the Arduino ecosystem. If you take the chip out of the board and try to use it in a different application, your Arduino programming skills become worthless. Programming the chip itself in C requires a lot more knowledge and practice, but you’re not limited to code written by other people. You can embed the chip in any custom application and literally control every bit to do exactly what you want.

Programming the ATmega chip

Atmel AVR chips can be programmed in C with Atmel Studio, which uses the Microsoft Visual Studio shell. It provides a very comfortable way of manipulating control registers, IO-pins and everything else. With the help of this IDE and the datasheet of the given ATmega chip, you can really program anything the controller is capable of: set up SPI, I2C, USART, ADC (Analog Digital Converter) and so on. You just need an ISP USB programmer to connect the chip with your PC. But, there’s an easier way!

Why use an Arduino board after all?

Having an ATmega chip on an Arduino board has some major advantages. First of all, you don’t have to use the Arduino software for programming. You just have to connect the board to your PC via USB, set up Atmel Studio and you’re good to go. Secondly, you have very easy access to all of the different IO, communication and power pins. This is what makes Arduino so great for prototyping.

Setting up Atmel Studio for Arduino

I’ve got the Arduino Mega, which uses the ATmega2560 chip. To set it up in Atmel Studio, you first have to add your chip under “Tools”, “External Tools”. You have to look which COM port your board is plugged into in Device Manager. Next, in “Extenrnal Tools”, you have to add your board, using the following configuration (the Arduino software has to be installed for this to work). Obviously, you have to pick the right COM port:

Device Manager Arduino

Title: a name, like Arduino Mega 2560
Command: C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude.exe
Arguments: -C"C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -patmega2560 -cwiring -P\\.\COM3 -b115200 -D -Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i

Atmel Studio External Tools

When creating a new project, you have to choose “GCC C Executable Project” and pick the right chip in the next steps. For me, it’s the ATmega2560. When you’ve written some code, you have to first compile it and then you can load it onto the chip by clicking the name you gave to the board under “Tools”. It’s that easy!

Your best friends are the datasheet and Google

Writing my thesis involves a lot of coding in Atmel Studio. We’re building a system with one master and 24 slave microcontrollers to measure the voltage and temperature of each battery cell in an electric go-kart. Not only ADC has to be properly configured for the slaves, but they have to send all the data to the master using a reliable means of communication. We could use SPI or maybe I2C for this purpose, but we chose USART for a number of reasons – which I’ll explain later. Until recently, I had no idea how to program ADC or USART – or especially how USART worked -, but I was able to figure it out from the datasheet of the chip and by googling and watching YouTube. In the coming blog posts, I’ll show how to set up ADC and USART for the ATmega chips, since they all have to be programmed in pretty much the same way. Keep tuned!

Advertisements

I enjoy microcontroller programming (AVR), programming in C/C#, playing with electronics, the Raspberry Pi and Arduino. My other passion is for IT - virtualization, PowerShell, servers and so on.

Tagged with: , , , ,
Posted in uC Programming

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 167 other followers

%d bloggers like this: