Purpose of this article:
1) to learn how to connect the Micro controller in a simple circuit and how to power it
2) to see how to create a simple programmer (a device to connect the micro controller to a PC for uploading software)
3) to present a simple software program in C that controls a series of LEDS
4) to show everything in action
Note: its my first time when I'm working with micro controllers. The info presented here might be inexact, but not incorrect, since I will stick to my findings and to the easy path for a beginner.
All that is presented below was the result of one day of reading, soldering, testing - a good score I would say.
Short Intro and Motivation
Since I'm a C++ developer for Embedded Devices, small processing units were always a strong point of interest for me. Furthermore, building a robot for my bachelor degree, a few years ago, required getting over serious hardware limitations related to power consumption and weight. So I'm still looking for a way to perfect my robot, and to extend its functionality, and the microcontrollers look like a very interesting approach, to say the least.
For a start, I had two micro controllers at hand: the popular ATMega8-16PU and the not-so-popular PIC24FJ64GA002. The first one I've bought on Ebay from a very good seller in Thailand:
For this article I will be targeting the ATMega8, since it gave me excellent results, thanks to available documentation. Can't say the same about the PIC.
So let's start:
Our first ATMega8 circuit
The ATMega8 is an excellent micro controller:
28 PINS (23 for Input/Output !!!)
8-Kbyte self-programming Flash Program Memory
512 Byte EEPROM
6 or 8 Channel 10-bit A/D-converter
Up to 16 MIPS throughput at 16 Mhz
2.7 - 5.5 Volt operation
Its datasheet is available here
I suggest you download the ATMega8 image to the left and print it, since you will need it often to consult the PIN layout when doing your circuit.
The Input/Output pins are important when it comes to how many devices you want to connect/control/use. To control a simple LED you will need to use 1 PIN (for output). To control a temperature sensor, you will need another PIN (for input).
To do a simple circuit and power the micro controller, first thing you need to know is that you need a stable 5V power source. The best way to achieve this is to use a regulator like the 7905/7805. This component has constant output regardless of the input current - in regards to some given boundaries, of course.
Here is the circuit. I suggest you use sockets for the ATMega8, to easily change the chip on your board.
By implementing the schematics above, you'll have the microchip powered. But you'll need to program it in order to have a useful result.
Developing the Atmega software
Let's try something simple - like controlling a LED on/off.
For this, we need to attach the led to any of the ATMega8's available I/O pins.
I'll use Pin 28 named PC5. This pin is part of the PORTC set of 7 pins PC0 - PC6. Notice there are other sets of pins like PORTB (PB0-PB7) and PORTD (PD0-PD7).
Next you'll need a C compiler for the ATMega8 and a software programmer that will upload the compiled code to the micro controller.
For the compiler I've used AVR Studio from ATmel. Download the 3 files below and install them in the given order (the avr studio itself and 2 additional service packs):
1 AVR Studio, version 4.13, build 528 (73.8mb)
2 AVR Studio 4.13 SP1 (build 557) (37mb)
3 AVR Studio 4.13 SP2 (build 571) (45mb)
For the uploader I've used AVRDude that comes with the WinAVR package:
As soon as you've installed everything, start AVR Studio. You'll get a page like this:
Click on "New Project", and select AVR GCC, then add a project name "TestLED" and select a location to store the new project's files:
Click Next, and in this new page you can select "AVR Simulator" and to the right under Device, select ATMega8 as show below:
Simply press Finish, and you can start writing code:
First things we need to know:
- #include <avr/io.h>
Will include this library in our project so we can access basic Input/Output functions and macros.
- int main()
is the entry point of our program, here is where the program execution starts.
So, first thing to do is to set the PORTC as output. For this we'll use a special register named DDRC:
- DDRC = 0x20;
why 0x20? Let's have a look in binary:
Basically we set a value of 1 to the position associated with our target pin.
To set all pins to output, the value would have been:
- DDRC = 0x7F (in binary: 1111111)
To control other ports (B or D) you can use DDRB or DDRD in a similar way. At this point you should consult the micro controller's datasheet available here.
Next thing to do is to turn the led on and off by code. We want this in an infinite loop, so here's the approach:
- DDRC = 0x20; //PC5 set to output
- while (1) //forever
- PORTC = 0x20; //set PC5 on . again computed in binary
- delay_cycles(1000); //a short delay to keep led on
- PORTC = 0; //all C Pins off
- delay_cycles(1000); //a short delay to keep led off
An even better approach that doesn't interfere with other PORTC pin's settings would be:
- DDRC |= 0x20;
- PORTC |= 0x20; //set 1 on first position, or simpler: PORTC = 0x20;
- PORTC &= 0x5F; //remove position 5 value, or simpler: PORTC = 0x0;
After the code is ready, press Build in the menu. It will produce a .hex file, in this case testled.hex
A parallel programmer for the ATMega 8
A programmer is a hardware interface that connects the micro controller to a PC.
On the PC you create a software program, that you can then upload to the ATMega8 using the programmer. It's quite simple.
To make things easier, I'll describe how you can create a parallel programmer, but this will work for you only if your computer has a parallel port since that's where you need to connect it. If it doesn't, you can purchase an USB2Parallel adapter or build an USB Programmer directly (you can use this)
Use any of the three variants below. My first programmer was a BSD parallel programmer.
BSD Parallel programmer
The first one is called bsd, probably because it was originally available at AVRPROG program for FreeBSD.
For this parallel programmer you'll need a LPT 25pin connector and 4x470 Ohm resistors. This is the way you need to make the connections for the BSD type parallel programmer:
To download the previously generated .HEX file to the atmega8, we will use our Parallel BSD programmer. Go to WinAVR folder, and enter the BIN subfolder. Copy testled.hex here and make sure your parallel programmer is connected.
Type in this command:
- avrdude -p atmega8 -c bsd -U flash:w:testled.hex:i
stk200 Parallel programmer
This was my second build. It behaves like the STK200 AVR Starter Kit, and it is also compatible with the PonyProg software. The design is simple:
To write the hex you can use:
- avrdude -p atmega8 -c stk200 -U flash:w:testled.hex:i
DAPA Parallel programmer
I newer built this, as not long after the stk200 I moved to usb programmers. Dapa, which means Direct AVR Parallel Access cable, can be seen below:
The command to write the hex is:
- avrdude -p atmega8 -c dapa -U flash:w:testled.hex:i
As soon as the software is uploaded, the code will be automatically executed.
Sometimes you might need to disconnect your parallel programmer after the code has been uploaded!
I've already built a very stable and flexible 4bit HD44780 LCD interface for the ATMega8, so stay tuned for the next article.
You can download my code here: testled.zip
If you need to learn more on how to correctly set the fusebits, see this article.