In order to get started, a programmer is necessary. That is not the case if you have an Arduino board.
I own a programmer from Tuxgraphics.org. Their webpage contains lots of ATMEGA related information and electronics in general. The programmer I bought was a kit that I needed to solder. Also the firmware wasn't installed on the device. But one can load the firmware to it without having a programmer. I it is very interesting to see how it works.
However, building a kit is tedious if you just want to get started with playing. Tuxgraphics.org offers assembled programmers as well. But if you are short on money, you can find lots of (incredibly) cheap offers for programmers on ebay. I recently bought an USBasp programmer from a Chinese manufacturer for about 4 EURO. I donated a bit of money to the inventor of hard and firmware. I think it is fair and it supports the open hardware and open source movement.
All you need in addition is a breadboard, one ATMEGA8 controller in a DIP (dual in-line) package that you can place on the board and some wires plus all components you want to control (such as an LED and a ~500 ohms resistor). No soldering required at all.
The "hello world" in microcontroller programming is a blinking LED. I will show how to do this in this first post.
My programmer has a 10pin adapter. The basic wiring is like this:
The LED is connected from VCC through a 220 ohm resistor to the PD0 pin of the controller.
If the hardware is set up, the controller can be programmed with a firmware. To create the firmware, I write programs in C. To get them compiled and copied to the controller I need C-compiler and a programming software. I use Linux and the packages "avr-gcc" (compiler) "avrdude" (programming software).
The program that makes the LED blink (blink.c) looks like this:
#include <avr/io.h> #define F_CPU 1000000UL // 1 MHz #include <util/delay.h> int main(void) { DDRD = 1; while(1) { _delay_ms(200); PORTD ^= 1; } }
To get this program running on the microcontroller, three steps are neede:
1. Compiling
avr-gcc -mmcu=atmega8 -Os blink.c
The flag -mmcu=atmega8 informs the compiler about the target arcitecture. -Os optimizes for size of the resulting program.
The result of this call is a file "a.out" which has to be converted to a .hex file that can be copied to the program memory of the controller.
2. Converting
avr-objcopy -O ihex a.out blink.hex
The flag -O ihex specifies the desired output format and the resulting file is "blink.hex"
3. Loading
avrdude -p m8 -c usbasp -U flash:w:blink.hex
copies the program to the controller, where -p m8 specifies the target device (or part), -c usbasp is my programmer and -U flash:w:blink.hex specifies the program data.
If the last command doesn't work you may not have access to the USB device. The simplest way to solve this is to become root before loading the firmware.
If a LED is connected, it should start blinking now. If everything is working, the door is open to the fascinating world of embedded programming!
The door is not opening...
AntwortenLöschenI followed all the steps but I have no blinking led..
The output I get is:
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9307
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "blink.hex"
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: writing flash (86 bytes):
Writing | ################################################## | 100% 0.06s
avrdude: 86 bytes of flash written
avrdude: verifying flash memory against blink.hex:
avrdude: load data flash data from input file blink.hex:
avrdude: input file blink.hex auto detected as Intel Hex
avrdude: input file blink.hex contains 86 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.04s
avrdude: verifying ...
avrdude: 86 bytes of flash verified
avrdude: safemode: Fuses OK (E:FF, H:D9, L:E3)
I googed the warning and it looks that I have to update some firmware. I read this:
http://blog.lincomatic.com/?p=1480
But I didn't really understand...
Thanks.. I am having fun! :)
I get the same warning, and it works. Is your LED mounted correctly? To the correct pin of the micro and in the right direction? Does the LED light up if you connect it from Vcc to GND (in series with a 220 ohm resistor of course)?
AntwortenLöschenI think the connections are correct. Yes, it light up when connected between Vcc and ground...
AntwortenLöschenOh yea, there was a mistake in the photograph of the setup, I saw that the LED was connected from Vcc to the resistor to the PC0 pin... different than described in the text :(
AntwortenLöschenI think is PD0 no PC0..
AntwortenLöschenNow I have a led that lights.. should it blink or something? I tried changing the number in the main program, but nothing changes...
:)