Atmega1284p

The silent master: MicroController (Setup ->Hello world)

dhwty
The Andela Way
Published in
9 min readApr 9, 2018

--

In the Introduction blog we introduced microcontrollers, now we will look into setting up, writing your first program and flashing. Without further ado, let’s get started.

Programming Microcontrollers

In this section we will cover everything you need for coding, compiling, and flashing your programs into the bare silicon chip. To do so, you’re going to need some hardware (a flash programmer), some software (a code editor, C compiler, and the program that’ll communicate with the hardware flash programmer) and the microcontroller.

The words “program,” “programmer,” and “programming” are overloaded in the microcontroller world. We (as programmers) write programs, compile them, and then use a flash programmer to program the AVRs, which then runs our program. Phew! Let’s step through the actual procedure and see what’s actually going on

It’s a long and winding road from the code you type into your editor to a chip on your desk that turns a light on and off. Getting from typed letters on a computer screen to a working piece of electronic machinery requires a chain of tools called, predictably, a toolchain!

The first step in your toolchain is going to be a text editor, or whatever you’re most comfortable writing code in. Once you can write and edit code, you need to compile it for the AVR, turning your human-readable C code into machine code for the AVR. The compiler we’re using, avr-gcc, is the AVR specific version of the popular open source compiler GCC. (In fact, I would argue that the support from Atmel for avr-gcc and an open source toolchain is the main reason for the chip’s amazing success in the hacker community). In addition to the compiler, you’ll need a few more software tools from the avr- gcc suite to go from source code to machine code that’s ready for uploading. A script called a makefile is commonly used to automate all of the repetitive, intermediate bits of the process. Once you’ve compiled your C code into machine code in the right format, it’s time to send the machine code over to the chip and write it into nonvolatile flash memory. The flash programmer is a piece of hardware that sits in between your computer and the target AVR microcontroller. The AVR microcontrollers, when put into programming mode, listen over their serial peripheral interface (SPI) bus for incoming data to flash into program memory. The flash programmer’s job is to relay the compiled machine code to the target AVR over the SPI bus.

The main feature of the style of software development that we’ll use in this article is cross-platform compatibility. That is, if you’re used to the whole workflow of writing code and compiling it on a Mac, you’ll have the same tools available for you on Windows or Linux, and you can be sure that you’ll always know what you’re doing wherever you go. After all, the target of all our work here is a little 8-bit microcontroller that doesn’t know anything about what operating system you use.

Make and Makefiles

The C programming language lets you split up one big program or task into a bunch of individual functions, and lets you keep collections of functions together in their own files for easier maintenance and portability. That way, if you want to frequently reuse some serial-port input/output functions, for instance, all you have to do is include the serial library code files (by name) in your main code, and then tell the compiler where to find these files. Separating your code into functionally different files is good software design, but it means that you need to remember all of the dependencies among the different files in your codebase and type out potentially many filenames each time you compile.

Keeping track of all of these dependencies manually can quickly become unreasonable, and it was only a few years after C was invented that the make utility was designed to help. Instead of compiling your files together manually, a file called a makefile contains a bunch of dependency rules and instructions for processing them, and then you just run the make command and everything compiles. (That’s the idea, anyway). So, for instance, you can explicitly compile all of your source files together like this:

gcc main.c another_file.c serialLibrary.c -o main

which makes an executable file, main, from all of the listed .c files. Or, you can write a makefile that maps out these dependencies:

main: main.c another_file.c serialLibrary.c

and then simply type make main or even simpler, make. The make program knows that names on the left side of the “:” are targets, and on the right, their dependencies. If you need to run special commands to make the targets from their dependencies, these commands are listed on the next line, indented with a tab.

Dependencies can, in turn, have other dependencies, and make will keep digging deeper until it can resolve them. Things get complicated with makefiles when you add in variables and wildcards that match any filenames. You can start to write generic rules that compile any .c files together, for example, and then you only have to change the variable definitions when you move a makefile from project to project.

Linux Setup

Setting up the toolchain for programming AVRs on Linux is very simple. If you’re using a Debian-based distribution (like Ubuntu or Mint or Debian) you can simply type (all on one line):

sudo aptitude install avrdude avrdude-doc binutils-avr avr-libc gcc-avr gdb-avr

Red Hat and Fedora users type:

sudo yum install avrdude avr-gcc avr-binutils avr-libc avr-gdb

All other Linux users will find that it’s easy enough to find source packages for all of the above. For more information checkout http://www.nongnu.org/avr-libc/user-manual/install_tools.html.

Windows Setup

Windows users have two options for the software toolchain, one based on the (huge) Atmel Studio and one based on WinAVR. Weighing in at 1/20 the file size and 9/10 of the functionality, I’d choose WinAVR. The current download link is from SourceForge and is a little bit old, though I’ve had no troubles with it. It’s very well tested and fully debugged.

Mac Setup

AVR CrossPack is the way to go for Mac. It includes all the compile tools, AVRDUDE, and more. It’s kept up to date and should just work.

Arduino Setup

As a fourth option, the Arduino IDE is available for all three OS platforms. Most of you will have it installed already. If you’ve got Arduino up and running, there are some modifications you can make to turn your Arduino IDE into a working generic AVR C-language environment.

AVR vs the Arduino

I should start by saying most arduino boards use AVR chips, but this doesn’t mean that they are one and the same. Arduino adds an abstracting layer, both software and hardware. This is done by writing helper functions in c and peripherials that make it easy to dive into the world of microcontroller programming. In this article I will be diving in and I will cover most of the things that arduino abstracts from us.

Arduino Pros

One very real advantage of the Arduino hardware setup is that the chip comes pre-flashed with a bootloader, which is code that enables the chip to communicate with your computer over the serial line in order to flash program itself. This means that you can do away with the requirement for an external bit of hardware to flash the chip there’s a tiny bit of bootloader code already running in your Arduino that will flash the chip for you!

The second highlight of the Arduino package is that it comes with a built-in USB- to-serial converter, so you don’t have to buy a separate one just yet. I personally get a lot of mileage out of my USB-Serial cable, and you will too if you want to play around with freestanding microcontrollers, GPS units, old terminals, hacked WiFi routers, and other devices. If you’re going to get serious about embedded electronics hacking, you’re going to want a standalone USB-Serial adapter eventually, but it’s sweet that the Arduino lets you get away without buying one for the time being.

And finally, although it’s not such a big deal, the Arduino is powered by your computer’s USB power supply. This is handy if you’re developing code on your laptop in a park or on a plane. You don’t need to find a wall plug for your power adapter or remember to bring batteries along with you — all you need for flashing, communications, and power is a USB cable.

Arduino Cons

As good as the Arduino hardware is as a generic AVR development platform, it’s not perfect. For use with this article and our examples, there are a number of disadvantages to using an Arduino instead of just plugging an AVR chip into a breadboard.

Not all pins are available for use, the Arduino platform uses some of the pins. So when working have fewer pins and therefore it might be less optimum to use in a production grade project.

The arduino platform can be easily be extended with modules that provide extra functionality such bluetooth, Wifi and some sensors, these modules are called shields. This is a quick and dirty way of prototyping a project though it comes with a cost being clumsy. It may not be suitable to making the final product.

Writing C in the Arduino IDE

Sample c code

To get running in C with the Arduino IDE, there are two things that you’ll have to do only once, before you even start the IDE:

  • Copy over whatever libraries you need to go with your code. It makes more sense to put all of these common files in one place. To use this “library” of common code, create a directory in your libraries folder (~/sketchbook/libraries on Linux and Documents/Arduino/libraries on Windows) and copy your common code here. Now you’ll be able to use it within any other program by simply importing the library.

Now that your Arduino IDE is set up, it’s time to get coding.

  1. Start the Arduino IDE.
  2. Import the header files into the project using the Sketch → Import Library pulldown, where you should find the code that you copied to library folder at the bottom of the list. Notice that the Arduino IDE adds include lines for each header file in your directory.
  3. Save the (mostly blank) sketch with a descriptive name. This will create a directory for you to put your code in.
  4. Outside of Arduino IDE, copy the C code file that you’d like to use into the sketches directory. If you press Open and reopen the sketch, you should see this newly added code in a new tab.
  5. Alternatively, you can write new C code in the sketch by opening up a new tab (Ctrl-Shift-N, or from the arrow menu in the top-right corner) and then entering the code directly.
  6. To make sure that all works, click on the Verify button. If it does, then you’re ready to flash the code.

Flashing the Arduino as target

This is super simple from within the Arduino IDE, because programming is what the Arduino is meant to do. The only difference here is that you’re writing your code in real, portable C rather than Arduino dialect.

  1. Verify that your board type is selected in Tools → Board.
  2. Make sure you’ve included your library using Sketch → Import Libraries, and that the #include lines appear in the first sketch tab.
  3. Click Upload (or type Ctrl-U) to flash your code into the AVR inside the Arduino hardware. Easy.

Hello world! the microcontroller way

With limited resource in microcontroller we can not always print out things, so we will do a blinking LED.

The first two lines, we import AVRs input-output header file and delay header file.

All C programs need a main function which marks the entry point of the program. Then we set in the Data Register B as an output. Next, we have an event loop, this is where everything that we want to achieve will be placed. So the first thing we set the first pin in PORTB voltage to a HIGH then we wait for a 1 second then set the first pin in PORTB to a LOW voltage.

That is all that we need to do here, So connecting an LED to pin 1 of PORTB it will blink once per second.

Recap

We have gone through setting up the AVR toolchail, setting AVR up, writing a simple C program and event how to hack the arduino IDE to run plain C programs. Till next time happy tinkering.

--

--