Exploring Embedded — A Primer

Vinay Lanka
9 min readAug 19, 2020

--

Beyond Arduino, Atmel’s legendary 8 Bit AVR series.

Embedded Systems

An embedded system is a system with embedded hardware and software that is designed to perform a dedicated function. At the core is a microcontroller that is programmed to carry out real-time operations.

A Microcontroller is a small computer system on a single chip. They contain just enough memory, power and I/O required to perform small tasks. There are different microcontroller architectures and vendors, a few notable ones include — ARM’s Cortex-M, Microchip’s AVR and PIC, NXP’s LPC, STM’s STMx and Intel’s 8051.

The Arduino Revolution

While writing a microcontroller article, we cannot ignore the significance of the Arduino. Contrary to popular opinion, Arduino don’t make a microcontroller themselves. They make hardware development boards based on the Atmega series which have made a name for themselves to be the go-to hobbyist development board.

The first thing many of us turn to when we have a new project idea is the Arduino ecosystem, and rightly so. Arduino has a plethora of MCU options, shields to expand your project, and its open-source core has spawned libraries for every conceivable purpose.

Beyond Arduino, what and how?

Even though Arduino’s are increasingly being used in production and we see widespread adoption of the teal-colored boards, they do have their limitations, be it I/O or power or cost.

AVR Microcontrollers

If we’re looking beyond Arduino, why not look at the series present on the Arduino board itself, the 8-bit AVR microcontrollers. This series, manufactured by Atmel (now Microchip), is one of the most popular MCUs in the world.

AVR microcontrollers

AVR is characterized by its 8-bit, RISC, modified Havard Architecture. We’ll go through each of these one-by-one.

  • 8,16 or 32 bit refers to the amount of data a microcontroller handles per operation cycle. It is the size of the data lines connecting the memory to the CPU.
  • RISC or Reduced Instruction Set Computer, refers to an architecture that performs a small and highly optimized set of instructions as opposed to the complex instruction set architecture found on an Intel 8051 for instance.
Havard Architecture
  • Havard Architecture allows us to have different memory spaces for the program instructions and data memory. This allows for concepts like pipelining (Fetching instructions when an instruction is being executed) which leads to greater performance.

Why 8-bit?

Why choose an 8-bit MCU when the standard for PCs is 64 bit (x86 arch) and even 32-bit microcontrollers are available?

The same reason why we don’t use a sword for chopping vegetables, or using an RTX 2080 for watching some YouTube videos. It isn’t required.

Plus, 32-bit MCU’s have quite a few downsides, with an additional learning curve and greater power consumption. It’s a myth that 8-bit MCUs are only used for simple applications and aren’t powerful enough for IoT designs. The goal to add intelligence to everyday objects is easier with such microcontrollers.

How to get started?

The first step would be to buy the products required to get started with AVR development —

  • Atmega 16/32 Development Board such as this one here — Doesn’t matter what AVR development board you go for, as long as you have no I/O such as an LCD Display pre-installed. Using constrained dev boards forces you to conform to someone else’s implementation of an MCU. Pins may have extra hardware connected, preventing you from utilizing their different functions.
  • Any AVR programmer. Again, the exact model you go for does not matter as long as you know what type of programmer it is (avrISP, USBasp, JTAG ICE). This is important as many clones work only with proprietary software, so select one after some research. You can find one type of programmer here.
  • AVRDUDE — If you have Arduino installed then please skip this step or download the program for flashing the microcontroller here. Don’t forget to add it to PATH with the env command in windows start.
  • (Optional, but highly recommended) — Atmel Studio 7. It’s an all in one IDE for AVR development at just the right cost(free). You can set up your development environment in an editor like VSCode but Atmel Studio provides a smooth programming experience with easy imports, code-complete, and debugging features.

Embedded C

You can program the microcontroller in various ways with the easiest and most adopted language being embedded C. Embedded C is just a set of language extensions for the traditional C programming language. One can think of it as C optimized for embedded systems.

A benefit of using embedded C over a language like assembly is that programming in C is less time consuming and the essence of the program can be ported over to different microcontrollers with no major change in code.

Programmers

If you’re coming from an Arduino background, the concept of having a separate programmer seems weird to program microcontrollers.

AVR-ISP: One of the most popular programmers available

The “external” programmer is an extra bit of hardware connects between a PC and the microcontroller. The Arduino avoids using this due to the presence of a bootloader present on the microcontroller.

A bootloader is a small piece of software that runs on the microcontroller when it is reset and allows the microcontroller to be programmed in situ. It sits there waiting for a new program to be downloaded, and loads it into memory not used by the bootloader.

Programmers offer better flexibility ie. allows you to have more control over things like the fuse bits of the chip, avoids the bootloader delay when you power or reset your board, and allows you to use the full program space (flash) of the chip on the Arduino board.

If you need help choosing a programmer, check this link.

Let’s get started

Once you’ve downloaded and installed Atmel Studio, let’s try to get an LED to blink on the AVR board. I’m using an Atmega16 on a standard AVR development board and I had a JTAG ICE mki programmer lying around.

To start:

  • Press Ctrl + Shift + N or go to File>New>Project to create a new project.
  • Select GCC C Executable Project and set the name to any name.
  • In device selection, search and select the device you own, in my case, it’s the Atmega16A.
  • After the project is created, navigate to main.c. This is where your application’s code must reside. It already contains some starter code, similar to the void setup, loop in Arduino.

Some code to make LEDs blink!

#define F_CPU 1000000UL //Define CPU Frequency#include <avr/io.h>
#include <util/delay.h>
int main(void){
DDRB = 0xFF; //Makes Port B Output
while(1){ //infinite loop
PORTB=~PORTB; //Toggle All LEDs in PORTB
_delay_ms(500); //1 second delay
}
}

The main statements to note here are -

F_CPU — We define the current operating frequency of the CPU. This is used in delay functions invoked from delay.h. Can be determined by looking at a datasheet.

DDRB — Data Direction Register X. Specifies whether the pins in a port are either input or output. X is replaced by the port you are dealing with.

PORTB — Used to give values to the port when in output mode. Can also be used to activate pullup resistors in input mode.

After you’re done, press F7 to build the file. This builds and generates an intel hex file which is supposed to be flashed to the program memory of the AVR. It’s present in the project directory under the debug folder.

AVRDUDE

AVR Downloader/UploaDEr is a command-line utility that helps us upload programs to and interface with the microcontroller through any programmer. It supports a lot of the standard programmers and has support for all the AVR microcontrollers.

To get started with AVRDUDE, plug in your programmer and microcontroller to the PC.

Check what port it is connected to on the device manager (Windows) or with dmesg | grep tty (Linux).

For Arduino users

If you have Arduino Installed, there’s one small step that you need to do to get avrdude working smoothly.

  • Go to your Arduino install directory/hardware/tools/avr/etc> to find a file called avrdude.conf, copy that and paste it in- <arduino directory>/hardware/tools/avr/bin/. Usually in normal avrdude installs, placing the conf file in the same folder will eliminate us pointing to it every time with the -C flag.

Get Started

In the windows terminal type -

avrdude -p <part number> -c <programmer type> -P <Port No>

Here,

  • -p refers to the Part number. Type avrdude -p asd to get a list of all the parts to find the your microcontroller. The device used for this tutorial is Atmega16 so -p m16.
  • -c is the type of programmer connected. Type avrdude -c asd to get a list of all the programmers supported. The programmer used here is a JTAG ICE mki, so that’s -c jtag1.
  • -P is the port the programmer is connected to. Some programmers like the ISP do not have a COM port so they can skip this flag. The port used in this tutorial is COM6, so -P COM6.
A successful connection.

Now press enter, and watch it connect to the controller. It’s very polite, and ends with a Thank you.

This step need not be executed every time, just once to see if the programmer is working as expected.

Uploading a program

Uploading a program consists of flashing the program flash of the AVR microcontroller.

With avrdude, it’s just one simple command.

avrdude -p <part number> -c <programmer type> -P <port> -U flash:w:<full path to hex file>.hex:i

Let’s break down the new flags,

  • -U <type of memory>:<operation>:path[:type of file] — So the type of memory we’re uploading to is the program flash so we select flash. The operation performed is write so — w.
  • The path to the file is obtained by navigating to the debug folder and locating your hex file. Get the full path and paste it here.
  • The [:type of file] is optional, but we set it to i, for intel hex.

So my full command would look like:

avrdude -p m16 -c jtag1 -P COM6 -U flash:w:"C:\Users\Vinay Lanka\Documents\Atmel Studio\7.0\GccApplication1\GccApplication1\Debug\GccApplication1.hex":i
A successful avrdude upload

Congrats! You just uploaded a program to an AVR microcontroller!

The result! :)

Making Life Easier

AVRDUDE makes the process easier, but typing in the command every single session is a little time consuming and redundant.

Fortunately, Atmel Studio allows us to set external programmers as tools, so upload is literally a single click

  • Go to Tools > External Tools to open the menu.
  • Click on ADD and set any relevant title to the programmer.
  • The command field takes an argument to execute the command, in this case avrdude. Find the path to avrdude.exe, <path_to_avrdude>/avrdude.exe.
  • The argument field should take these arguments
-p <part number> -c <programmer type> -P <port> -U flash:w:$(ProjectDir)Debug\$(TargetName).hex:i

This will automate the process and create a new tool for your programmer, from Atmel studio itself.

External Tools Configuration

So to upload from Atmel Studio, Go to Tools,<Name of your tool>

AVRDUDE with Atmel Studio

So, What next?

You’ve successfully set up a workflow for your embedded system projects. Now, all there is to learn is embedded systems itself.

A great book to get started with AVR development would be:

The AVR Microcontroller and Embedded Systems: Using Assembly and C — By Sarmad Naimi, Muhammad Ali Mazidi, Sepehr Naimi, a pdf version could be found here.

The next-hop from AVR would be to program ARM-based dev boards, such as the famous STM32 series.

If y’all liked the tutorial, and would like to know more about AVR, or would like a tutorial on ARM-based boards, let me know on my LinkedIn or Twitter.

--

--

Vinay Lanka
Vinay Lanka

Written by Vinay Lanka

Robotics Graduate Student @ UMD

Responses (1)