Serial Communications with the ATmega16820 May 2013
The first thing you do when you learn a new programming language or platform is to write a "hello world" application. This requires something like a "printf" function. That's not so straightforward when it comes to microcontrollers - where will the output of the "printf" go? That's where serial communications come in. Arduino users have it easy - they just need to use Serial.print(). But the situation is not so bad if you are using a standalone microcontroller - just choose a chip like ATmega168 which has USART - hardware support for serial communications.
The code needed to transmit serial data is very simple, and the datasheet has most of what you need:
Here is the schematic of a simple setup that will let you send debug data (strings) from an ATmega168 to your computer. If you are completely new to AVR programming, I recommend that you read Hackaday's tutorial on the subject. Note how the TX/RX lines are flipped when you connect it from the ATmega168 to the FTDI adapter.
A few things to remember in order to for this to work correctly (the Makefile in the GitHub link below takes care of all this):
- For getting a 9600 baud rate, the chip needs to run at 8 MHz, and for this, you need to unset the CKDIV8 fuse.
- For full sprintf formatting support, some additional flags are needed in the linker.
Here is a photo of the setup that I used:
Here is what the output looks like on CoolTerm, a serial monitor that I use on my Mac:
Having a "printf" function is very handy for debugging your projects - so choose a chip that will let you support this functionality without too much pain.
Here is all the code used in this project - do pay attention to the flags in the Makefile.