I have 20 odd years in programming starting from pascal 7 to delphi. I want to start programming micro controllers using C and the tool most electronics kit recommend is winAVR with programmers notebook. I have installed the software and would like to start compiling code and I'm lost to say the least and can't find any simple documentation to get myself onto a track where I can start testing code. Can anyone offer some good starter material?
-
1Those 20 odd years have also seen the rise of internet search engines, which today have become advanced enough to give you plenty of results if you search for "winavr tutorial". – Michael Apr 03 '13 at 17:08
-
2Go to the Atmel web site for resources. Also, AVR Studio from atmel is a nice IDE for AVR. If you haven't already, buy an evaluation board. – UncleO Apr 03 '13 at 17:11
-
@michael I mentioned the 20 years because that would presumably be the first thing a person would try. To put it another way, I'm not looking for information on road rules, I'm trying to figure out how to start the car. There are hundreds of sample codes to learn from but skip over how C code is compiled because they assume it to be common knowledge. – Clinton Apr 03 '13 at 20:42
-
1@user1981529 You don't really need to learn how the C code is compiled to start. There are a few occasions where you need some assembly language, but not many. Rather, the trick with embedded systems is learning the mechanisms for interacting with all the peripherals in the device: how to turn on and off ports, use timers, read voltages, uart, and so on. These all involve reading and writing to registers specific to the device family, and AVR is a good family to start with. But you really need to get a board to play with. Some require hardware to load code, but Atmel has some that use only USB – UncleO Apr 04 '13 at 00:00
1 Answers
Whereas for PC's the usual first program is "Hello, World!", in the embedded world (one lacking displays, as least to start with, the equivalent is the blinky led: you attach a LED to some output pin of your processor (don't forget the current-limiting resistor!: you need a resistor in series with the LED), and you make the LED blink. You can find plenty of blinky LEDs for AVR, but we can write one right here:
// The next define tells delay.h what your CPU speed is, assuming 1Mhz
#define F_CPU 1000000UL
#include <util/delay.h>
main() {
while(1) { // loop forever
DDRB = 0xFF; // Set the direction of all pins
// on port B to OUTPUT (can change to some other port)
PORTb = 0xFF; // Set all pins on port B high (can change to some other port)
_delay_ms(1000); // Wait one second;
PORTb = 0x00; // Set all pins on port B low (can change to some other port)
_delay_ms(1000); // Wait one second;
}
}
It should compile on WinAVR, and load correctly. Change PORTB
and DDRB
to some other port of you'd like. Note that this program changes all the pins on that port: so if your port B has 8 pins, all of them will blink a led hooked up to them. Don't forget the current-limiting resistors, and that LEDs are directional: they only work when plugged in one way, and not the other.

- 1
- 1

- 4,086
- 2
- 19
- 36