UART: Serial Communication and the Atmel ATmega324p in OS X with CrossPack
I am a stickler about using your preferred operating system to do the things you want. In my case this is
OS X. Many times, you can find tutorials for just Windows &/or Linux, so here is one for OS X to program
your ATmega324p[1]. I am using the AVR Dragon[2] programmer, which you can pick up at Atmel for around
$49. I also prefer vim and the command line over IDEs – particularly for smaller projects – so this tutorial
is not for those of you wanting to use AVR Studio in OSX. Not yet anyway…
Enjoy!
If you want the microcontroller to respond to something you type on a keyboard you're going to need a means communication. UART, a.k.a USART ( Universal Synchronous/Asynchronous Receiver/Transmitter ), is one way we send and receive data to/from our computer and the Atmega microcontroller. It requires minimal code and wiring to get working. In this case, the most common communication we use on our machines to interface with other devices is the USB port so we will use that. On the other end we will be using what is called a DB9 and the MAX232 chip.
Completed Hello World! wiring & AVR Dragon programming board
USB to DB9 cable (a.k.a RS232)
MAX232 chip
Five 1uf capacitors
Regular breadboard wire (red, black and two other colors)
DB9 adapter (with soldering heads)
DB9 Gender Changer
Soldering iron and rosin-core solder
330Ohm resistors
1 blue LED
(basic soldering skills required)
Step 1 - Setting up Your Machine for Serial Communication
Plug your RS232 cable into your machine's USB port. You do not need anything attached to the DB9 end yet as this section covers your cable and the driver for your USB port only.
Open a terminal window (see Hello World! tutorial for details) and enter the following command:
ykk$ ls /dev/tty.*
This command tells you a list of your device hosts on your machine[3].
You should see:
Your serial USB device wont show up yet because you have not downloaded the driver to 'turn on' this communication for your USB port. So let's get that driver now. Download the FTDI serial driver and install it as you would any other application for your version of OS X here:
www.ftdichip.com/Drivers/VCP.html
A VCP driver (as opposed to the D2xx), causes your USB port to show up as an additional communication port when your USB to DB9 cable is plugged to your machine so you can use the standard RS232 device that I will show you later. For now, just find the Mac OX X link for your processor (mine is the 32bit) and download the driver.
When you are finished, plug your RS232 cable to your USB port and execute the ls /dev/tty.* command again.
You should see:
The /dev/tty.usbserial-FTF539ZI in the list is my serial communication port and the FTF539ZI tagged onto the end is the serial number for the cable I am using - yours will be different. You can match this to the list of your USB buses by plugging your cable then entering:
ykk$ system_profiler SPUSBDataType
Along with others, you should see:
See the 'Serial Number' line?
Similarly you can plug the AVR Dragon programming board to your machine and, executing the same command, you will see:
Now you're ready to wire your MAX232 and Atmega324p! For more information on the MAX232 chip, see my details on this. For now, you can just follow the wiring diagram.
a. See the Power Regulator How-To for On-Board Power with a wall wart/power adapter.
b. You'll need to solder three wires to your DB9 adapter:
Your adapter should have the male pins on one side and soldering points on the other. The black plastic, on the end with the solder points, has very small numbers above each solder point/pin. Solder a 2" piece of black wire to the ground pin (pin 5). Solder another 2" piece of wire to the RX pin (pin 2) and another to the TX pin (pin 3). It's best to use colors that are not red or black for the RX and TX pins.
Attach the gender changer to the male end to convert the adapter to female so it will fit your RS232 cable.
c. See the MAX232 Chip How-To to wire this chip.
d. The wiring for your Atmega324p is the same as the Hello World! with the addition of the wires on pin 14 (RXD0) and 15 (TXDO) coming from your MAX232 chip. Once your MAX232 chip is wired, connect a wire from the RX of the MAX232 to the TX pin of the Atmega324p and the TX pin of the MAX232 to the RX pin of the Atmega324p.
Just as you did for the LED code, use vi to create the following main.c file in your Documents/AVR/Demo/firmware directory. You can save your Hello World!'s main.c file by executing the following command:
ykk$ mv main.c helloworld.c
Then open a new main.c file with:
ykk$ vi main.c
And make it look exactly like this:
NOTE: If you're interested in what all those UBRs, USCs, and UDRs mean, see the UART section of the Atmega324p's datasheet and my discussion on UART. In essence, you are enabling the registers for transmit and receive on the microcontroller - which then waits until it sees something you type and prints it on your screen - as you will see in the next step.
Just as you did with the Hello World! project, execute the following commands to compile your new code:
ykk$ make clean
then:
ykk$ make
If your code does not have errors or misspellings you can now type:
ykk$ ls
You should see:
Program your board by entering the following command:
(Don't forget to plug your AVR Dragon to your machine and the breadboard!)
ykk$ avrdude -c dragon_isp –B4 -b4800 -p m324p -P usb –U flash:w:main.hex:i
The option -b 4800 is a new option to your programming command. It tells your microcontroller how many bits per second should be communicated between it and your machine. You will tell your machine to communicate at the same rate from the console in the next section. For more details on this see the UART and Baud Rate page.
Now that your breadboard, microcontroller, MAX232 chip are wired up and your computer is set to send and recieve with your RS232 cable, it is time to open a serial console and see this UART thing do some work.
Plug your RS232 cable to your machine and the DB9 end to your breadboard. Wait to power the breadboard with your wall power.
Open your Terminal window and enter the following command:
ykk$ screen /dev/tty.usbserial-FTF539ZI 4800
(the serial number at te end will be different for you) The 4800 on the end of the command tells your machine to communicate at the same bts per second we told the Atmega324 to communicate. As long as the Baud Rates are the same between the two they can communicate - if these are different values with this UART configuration, you will get gibberish or nothing at all when you power the board.
Until you power your breadboard, you will see blank screen with a blinking cursor.
Plug the power to your board.
You should see:
Now try typing "Hello World!"
You should see:
To Exit the Screen console, hold the control (CTRL) key, hit the 'a' key. Then CTRL and the '\' key. You will need to confirm the decision with a 'y' or 'n'.
To Exit Screen:
CTRL-a
CTRL-\
y
Alternately, you can use the Arduino Serial Monitor. To do so, download the Arduino Environment, set it to use your
serial port:
Then select the Serial Monitor icon:
Again, plug the power to your board. Select the 4800 baud rate from the drop down list at the bottom right of the window.
You should see:
Typing into the upper bar and hit Send.
You should see:
[1] Atmel Atmega324p datasheet can be found at: http://atmel.com/dyn/resources/prod_documents/8272S.pdf
[2] AVR Dragon can be found for purchase at: http://store.atmel.com/PartDetail.aspx?q=p:10500053
[3] Further reading on TTYs: http://www.linusakesson.net/programming/tty/index.php