How to Program and Debug AVR Microcontrollers.
The first question a beginner embedded developer faces is how to program a microcontroller.
To program a microcontroller, you need three components:
Every microcontroller has a programming mode; in this mode some microcontroller pins change their functions so that it can receive data and commands from the programmer. On AVR, programming mode is entered using the Reset pin.
As you know, in normal operation this pin must be at logic one, and applying a logic zero to it resets the microcontroller; more precisely, a brief zero that is then replaced by a one. But if you apply and hold a zero for a long time, the microcontroller will enter programming mode.
The simplest and at the same time popular programmer is the Gromov programmer; its schematic is shown below.
The whole circuit fits into a DB9F connector housing.
Essentially it just converts the COM port logic levels (±12 volts) into microcontroller logic levels (0 and +5 volts). Let's see how this happens using the SCK pin as an example. The other data pins, except MISO, are also connected through a diode and two resistors.
When there is no signal, the SCK pin is pulled to ground through resistor R6.
When a logic zero is transmitted, the voltage is 12 volts. This voltage is distributed between the diode and two resistors. The voltage drop across the diode is 1 volt, and the remaining voltage is split between the two resistors, 5.5 volts each, and from one of them it goes to the MCU pin. So when the COM port sends 12 volts, only 5.5 reaches the microcontroller.
When a logic one is transmitted, the voltage is -12 volts. This voltage reverse-biases the diode and no current flows through it, so the microcontroller pin ends up pulled to ground through resistor R6.
I should say that for a COM port, logic zero is in the range from +3 to +12 volts and logic one from -3 to -12, and the case we looked at only applies to PCs where logic one is encoded as -12 volts and logic zero as +12. In some laptops logic one is encoded as -3 volts and logic zero as +3. So don't be surprised if you can't program a microcontroller from a laptop using the Gromov programmer. By the way, the program that works with programmers connected via a COM port is called UniProf.
Such a programmer is fine if you need to program a few devices, but it's inconvenient for everyday use for two reasons: it needs a COM port, which is increasingly rare on modern PCs, and programming is slow.
USB programmers don't have the drawbacks described above, for example the USBasp or SP200S.
As for the SP200S: USB connection, a large number of supported chips, high programming speed and a low price of about $15, but this programmer isn't recognized on computers running Windows 8.1. The reason is the PL2303HX USB-UART converter, which isn't supported by Windows 8.1, and that sometimes matters.
Now on to debugging: if you don't have a hardware debugger, you can debug on the hardware in two ways:
Let's look at the first method with an example: we have a conditional statement and need to know whether it is executed. To do this, inside it we turn on an LED, wait a while, and turn the LED off. We compile, flash the program into the controller, and if the LED blinked, the program entered the if().
In this way, by blinking a different number of times in different parts of the program, you can determine which parts of the code were executed.
If we need more detailed information, for example data from a temperature sensor, we can use a USB-UART converter and the Terminal program. After getting the information from the sensor, we send it over UART to the Terminal window.
You need to know these methods, but a hardware debugger is more convenient to use, and that brings us to what this article was meant for: the AVRDRAGON programmer-debugger. The programmer supports almost all Atmel microcontrollers, the only exceptions being some XMEGA chips. The debugger lets you stop the program anywhere and look at the values of variables and registers, step through the program, view the function call stack and much other useful information.
It supports the following interfaces:
You can read more about each of the interfaces here.
The programmer is controlled directly from Atmel Studio and works without problems under Windows 8.1. The only thing that makes you think twice before buying it is the price: mine came from AliExpress for $79, and at the time of writing it costs $88. Speaking of price, not long ago I got a Chinese jlink for $13. After connecting it to the computer, it asked to update; at the end of the update it complained that the hardware was unlicensed, and that was it: the cheerfully blinking LED went out; in short, the firmware got wiped. It took three days to find firmware that wouldn't get wiped on update and to get it working; by the way, I only managed to flash it under XP, as under 7 and 8 it wasn't recognized correctly. How it will work and what will happen at the next update is unknown; that's the downside of a fake.
In short, whether to buy it or not is up to you; if you do, be sure to put it in some enclosure, or make a hole for the cable in the original box, so it will last much longer.
I bought the AVRDRAGON here.
To program a microcontroller, you need three components:
- the chip you want to program
- a programmer
- a special program on the computer that controls the programmer
Every microcontroller has a programming mode; in this mode some microcontroller pins change their functions so that it can receive data and commands from the programmer. On AVR, programming mode is entered using the Reset pin.
As you know, in normal operation this pin must be at logic one, and applying a logic zero to it resets the microcontroller; more precisely, a brief zero that is then replaced by a one. But if you apply and hold a zero for a long time, the microcontroller will enter programming mode.
The simplest and at the same time popular programmer is the Gromov programmer; its schematic is shown below.
The whole circuit fits into a DB9F connector housing.
Essentially it just converts the COM port logic levels (±12 volts) into microcontroller logic levels (0 and +5 volts). Let's see how this happens using the SCK pin as an example. The other data pins, except MISO, are also connected through a diode and two resistors.
When there is no signal, the SCK pin is pulled to ground through resistor R6.
When a logic zero is transmitted, the voltage is 12 volts. This voltage is distributed between the diode and two resistors. The voltage drop across the diode is 1 volt, and the remaining voltage is split between the two resistors, 5.5 volts each, and from one of them it goes to the MCU pin. So when the COM port sends 12 volts, only 5.5 reaches the microcontroller.
When a logic one is transmitted, the voltage is -12 volts. This voltage reverse-biases the diode and no current flows through it, so the microcontroller pin ends up pulled to ground through resistor R6.
I should say that for a COM port, logic zero is in the range from +3 to +12 volts and logic one from -3 to -12, and the case we looked at only applies to PCs where logic one is encoded as -12 volts and logic zero as +12. In some laptops logic one is encoded as -3 volts and logic zero as +3. So don't be surprised if you can't program a microcontroller from a laptop using the Gromov programmer. By the way, the program that works with programmers connected via a COM port is called UniProf.
Such a programmer is fine if you need to program a few devices, but it's inconvenient for everyday use for two reasons: it needs a COM port, which is increasingly rare on modern PCs, and programming is slow.
USB programmers don't have the drawbacks described above, for example the USBasp or SP200S.
As for the SP200S: USB connection, a large number of supported chips, high programming speed and a low price of about $15, but this programmer isn't recognized on computers running Windows 8.1. The reason is the PL2303HX USB-UART converter, which isn't supported by Windows 8.1, and that sometimes matters.
Now on to debugging: if you don't have a hardware debugger, you can debug on the hardware in two ways:
- with an LED
- with a USB-UART converter
Let's look at the first method with an example: we have a conditional statement and need to know whether it is executed. To do this, inside it we turn on an LED, wait a while, and turn the LED off. We compile, flash the program into the controller, and if the LED blinked, the program entered the if().
if(condition)
{
some code.......
led_on();
delay_ms(100);
led_off();
}
In this way, by blinking a different number of times in different parts of the program, you can determine which parts of the code were executed.
If we need more detailed information, for example data from a temperature sensor, we can use a USB-UART converter and the Terminal program. After getting the information from the sensor, we send it over UART to the Terminal window.
You need to know these methods, but a hardware debugger is more convenient to use, and that brings us to what this article was meant for: the AVRDRAGON programmer-debugger. The programmer supports almost all Atmel microcontrollers, the only exceptions being some XMEGA chips. The debugger lets you stop the program anywhere and look at the values of variables and registers, step through the program, view the function call stack and much other useful information.
It supports the following interfaces:
- SPI programming (SPI)
- High Voltage Serial Programming (HVSP)
- Parallel Programming (PP)
- JTAG Programming (JTAG)
- PDI Programming (PDI)
- aWire Programming (aWire)
You can read more about each of the interfaces here.
The programmer is controlled directly from Atmel Studio and works without problems under Windows 8.1. The only thing that makes you think twice before buying it is the price: mine came from AliExpress for $79, and at the time of writing it costs $88. Speaking of price, not long ago I got a Chinese jlink for $13. After connecting it to the computer, it asked to update; at the end of the update it complained that the hardware was unlicensed, and that was it: the cheerfully blinking LED went out; in short, the firmware got wiped. It took three days to find firmware that wouldn't get wiped on update and to get it working; by the way, I only managed to flash it under XP, as under 7 and 8 it wasn't recognized correctly. How it will work and what will happen at the next update is unknown; that's the downside of a fake.
In short, whether to buy it or not is up to you; if you do, be sure to put it in some enclosure, or make a hole for the cable in the original box, so it will last much longer.
I bought the AVRDRAGON here.







