Programming an ATmega328

Ever heard about Barebone Arduino? It consists of trimming down an Arduino to it’s very basic parts and can be really effective in cost reduction.

3197918087_c578d8c12f

Tipical Barebone Arduino

Even it might seem the simplest version of an Arduino (actually I wouldn’t be calling it an Arduino any more) we can go even further and drop the external crystal in favor of the internal (yes, there is one) oscillator: that is now just a plain ATmega328:

328pb_rev1_pinout

Thanks to pighixxx for his beautiful pinout diagrams!

To program an ATmega328 or, for what matters, many other MCUs, you can use an USBasp, an hardware device providing direct access to the MCU via ICSP, aka a programmer. Installing the USBasp drivers under Windows 10 has been time consuming, but luckily a very simple solution does exist.

I’m not going into the details of how you connect the USBasp to the ATmega328 as this is information widely available on the web and the first link is quite comprehensive in this regards. So the remaining part is, obviously, software!

There is only one option in my mind when it comes down to programming microcontrollers: Sloeber (formerly known as Arduino Eclipse Plugin). The latest release is just great and not only because I did personally contribute a few bits here and there, but because the whole thing is just a leap step forward in MCU development compared to Arduino IDE.

Programming a bare ATmega328 though has provided some challenges I’m here to describe for those who might encounter the same issues.

Installing the tools

With Sloeber it’s quite easy to install support for new boards, even those which are not built-in or, if you prefer, third-party.

Just open Window > Preferences > Arduino > Locations, you’ll find a link in the bottom part of the settings page, click on it and search the web page for the board or board set you’d like to install. I was looking for “barebone” and that is what I added into my list of locations: https://raw.githubusercontent.com/carlosefr/atmega/master/package_carlosefr_atmega_index.json (if you want to shortcut and avoid the issues I’ve faced, an alternative URL is available at the very bottom of this article).

locations

Then switch to the Platforms and Boards section to select and obtain the definitions to build for a bare ATmega328: obviously I’m going for the latest available version.

platforms

After a few seconds I got my Sloeber ready for compiling what would be a normal Arduino project but targeted at a barebone Arduino!

build config

My choice was to create a custom build configuration, but that’s totally optional.

Filling the gaps

I encountered a few compilation problems though, which I was able to overcome with some support.

Making the long story short I had to modify the JSON file associate to the Barebones ATmega Chips in order to provide some missing information:

  • the tools section, originally consisting of an empty array, should contain the references to the two tools used by the package, avr-gcc and avrdude
  • the toolDependencies section needs to be updated in order to match the tool versions now being linked

I’ve prepared a fixed JSON package file available from https://raw.githubusercontent.com/rlogiacco/arduino-eclipse-plugin/packages/package_atmega_index.json.

Also I had to copy the programmers.txt from arduinoPlugin\packages\arduino\hardware\avr\1.6.13 to arduinoPlugin\packages\atmega\hardware\avr\1.3.0 in order to make the USBasp protocol available with the new boards file.

Setting the fuses

When you buy a plain ATmega328 micro controller you are more than probably going to get the chip configured to the factory settings, meaning it will run at a 1MHz with it’s internal oscillator. That’s obviously a very conservative setting on which you have control, along with some other settings, via the chip fuses.

If you never had to deal with chip fuses before and the AVR ones in particular, you might want to read at least an introductory explanation  before going to change any of them.
WARNING: an erroneous setting of the chip fuses can turn your micro controller into a desk decoration, don’t blame me if that occurs to you!

I want the chip to run at 8MHz which requires just one bit to be changed, but I’m taking the opportunity to also tweak other parts of the settings and I’m going to use a nice AVR Fuses Calculator web application to do so:

  • low fuse 0xE2 means “use the internal 8MHz clock source and run at full speed
  • high fuse 0xD6 means “don’t clear the EEPROM each time and don’t reserve any space for a bootloader
  • extended fuse 0x07 means “don’t auto reset on min voltage

To do so I’m using the avrdude tool, which I already got installed automatically by Sloeber.

To quickly get to avrdude look at the last command printed by Sloeber into the upload console when you try to upload any firmware/sketch to the micro: it should look like the following

Launching X:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/bin/avrdude -CX:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/etc/avrdude.conf -patmega328p -cusbasp -Pusb -Uflash:w:D:\Projects\.cpp\Serial/Release/Serial.hex:i

The bold part is what you want to copy past into a command prompt and then add the missing bits:

X:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/bin/avrdude -CX:\Sloeber\arduinoPlugin\tools\arduino\avrdude\6.0.1-arduino5/etc/avrdude.conf -patmega328p -c usbasp -U lfuse:w:0xE2:m -U hfuse:w:0xD6:m -U efuse:w:0x07:m

If all goes well you should get a nice and reassuring confirmation message like the following:

avrdude: safemode: Fuses OK (H:07, E:D6, L:E2)
avrdude done. Thank you.

Upload a firmware

Once you have all of the above in place, uploading a firmware to your micro controller is not going to be different from doing the same on any of your Arduino boards: hit the upload button, let Sloeber compile your code and enjoy your cheap and very professional setup!

Advertisements

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