May 20, 2013 | Circuits |
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.
Need help with a hardware project or product? Drop us an email at firstname.lastname@example.org. We offer consulting services on AVR and Nordic nRF BLE - hardware design, firmware development, prototyping, PCB design/assembly, sourcing and manufacturing. We can help you bring your product to market!
Bluey nRF52 BLE IoT dev board
We love hearing from our readers. Email us at email@example.com for questions or comments on this article. If you found this article useful, please support us by buying some of our hardware products.