Microcontroller ATmega16 Programming — Hello World

FIFTYTWO
6 min readJun 4, 2015

--

by Dmitry Victorov

Today I’ve decided to experiment with hardware and create Hello World project on a micro controller. I’ve used ARV ATmega16 for this purpose, because I had it in my electronic components collection. I bought it some years ago dreaming of robot creating. In addition, I bought RS232 serial programmer JONIS PROG (kind of JDM programmer, self-made by a guy from electronic components store). However, my old PC was broken in those times, so I had a laptop only with no serial port interface, so I forgot about my dream for some years. Now I have desktop PC, but its RS232 COM port was not accessible (it’s sad, but old interfaces become extinct). But after opening the case I found that motherboard has it:

Internal RS232 COM port of Gigabyte GA-Z77-DS3H

I also have an old parallel port LPT interface card with additional COM ports panels. But panels sizes don’t fit my PC case:

LPT + 2 COM interfaces card

I decided to use external COM panel in non-standard way. It looks ugly but it works and it is the main thing for me:

Non-standard way of external COM panel embedding

After some research, I found two working solutions for me to write HEX dump into AVR controller using my programmer JONIS PROG:

  1. PonyProg is a quite old project that is rarely updating. It seems official project page was not updated very long time and the last version there is 2.07c. Its installer didn’t launch on my system at all. It disappointed me the first, because it requires Windows XP, and I have installed Windows 8 64 bit only. But after I’ve found that there is sourceforge project with new 2.08c version that was perfectly installed on my PC. Well, it is quite old too, but works fine.
  2. AVRDUDESHELL by Goloveiko Alexander is GUI wrapper for console AVRDude. Blog of Alexander is in Russian, but AVRDUDESHELL has English interface. He updates the program actively and last installer at this moment can be downloaded from his Google Drive. AVRDude supports a lot of programmers. I selected ponyser because JONIS PROG is improved clone of RS232 serial PonyProg.

I tested my programmer with both software and both of them can read FLASH memory and FUSE bits of my ATmega16. I choose AVRDUDESHELL finally, because it works much faster and I see that it updates regularly.

Well, time to create simple scheme and write program to control it. I found a simple scheme picture and implemented it:

ATmega16 LED blinking scheme

As we can see, controller will output data to pin 1 turning on and off LED1, so we will set the zeroth bit of the port B to turn LED1 on and unset it to turn off. Pin 2 is used as input: when button S1 released, state of the first bit of the port B will be set, otherwise unset. I would like to notice that I used R1 equal to 2 kOm instead of 330 Om, because I used very sensitive LED. I think that it is better to choose R1 corresponding to LED thinking that 5V will be passed through it. In the case of unknown LED parameter it is possible to choose from some value, say 2 kOm, decreasing it until light of LED will be bright enough. It will be OK for test purposes. R2 can be any from 10 kOm to 100 kOm.

Next, I wrote a simple program in C to blink LED. This program sets zeroth bit of port B (pin 1 of chip) as output and uses first bit of port B (pin 2 of chip) as input to change blink frequency.

Main source file

I used Atmel Studio 6 for program developing and found that it is perfect IDE based on Microsoft Visual Studio 2010 with Visual Assist embedded. It has ability to upload binary direct to chip, but supported programmer needed for that. I used AVRDUDESHELL instead, as I wrote before, because Atmel Studio 6 doesn’t support JDM programmers. I selected my chip ATmega16 in project settings. Complete Atmel Studio 6 solution can be downloaded from github.

Atmel Studio allows controller debugging using different hadware methods and using simulator. I tryed simulator debug. But as I noticed delays are much more on simulator than on real hardware. It will be better to go toolchain settings and turn off any optimization for Debug configuration. So debugger behaviour will be more adequate in step debugging mode. It is possible to open I/O View and simulate S1 button by setting pin 2 value to 1 or 0.

Well, after playing with simulator I built Release hex, that can be found in folder Release with extension hex. This is the file that I wrote to my chip using AVRDUDESHELL.

Also I would like to mention about FUSE bits. The most probably that a new chip don’t need to modify them for this Hello World. AVRDUDESHELL and PonyProg allow to read them and verify. There are services to calculate FUSE bits, for example http://www.engbedded.com/fusecalc. I selected my chip ATmega16 from the list there and found that my FUSE configuration is the same as on the site. The most I was worry about enabling internal clock, but it was enabled by default as I saw. It is better to don’t touch FUSEs if don’t know their meaning, because it is simple to lock chip and it will not be rewritable by ISP programmer. Therefore, only high voltage programmer (like STK500) will be able to unlock it.

Here is a video how it looks like:

What next? I think I will build new USB programmer compatible with Atmel Studio. USB programmer usually requires micro controller to process USB signals, so I will use my old JONIS PROG to create firmware of new programmer. By the way we can look at this, as at problem of chicken and egg (to make an egg we need chicken, but to give birth to the chicken we need another egg). Well, to build USB programmer we need to have another programmer. The simpliest programmers can be built with no programmable controllers for LPT or COM port and it is simple to find schemes in internet. But what if in the future we will not have RS232 and LPT ports on our comuters? As I know, there are adapters in shops to converts USB to RS232 and LPT, but they are not full-fledged. For example, such COM adapters can’t work in bitbang mode that required for building of COM programmer with no firmware. Actually, it is not a problem, because there are cool micro chips, like FT232R, that can work in bitbang mode and act as a bridge between USB and COM. I saw scheme of simple bitbang USB programmer using FT232R, for example here in Russian. But another question is price of an original FT232R. It is quite expensive. There is exotic solution to extract FT232R analogue from old phone cable that is cheap, because old phones used RS232 protocol internally to exchange data with PC. So their USB cables often had something like FT232R embedded.

And sure, we always can use Arduino and Raspberry Pi to develop something cool fast with less problems. They have a lot of code libraries already and they are cool to start deals with hardware fast. But I think it is cool to use micro controllers in final release constructions after testing on Arduino, because they are much cheaper and can be embedded in device permanently with no regrets.

--

--

FIFTYTWO

Creators of Kenshō & JELLIES! Writing about game development, design & code. Learn about us: www.wefiftytwo.com