BY G.Y. XU
rate is 9600 bits per second, data is
eight bits in one transaction, there is no
error checking, and one start bit and
one stop bit are attached to each transaction. So, it actually transmits 10 bits
instead of eight bits for a byte of data.
To program the ATtiny11, you first
assemble the source code using
Atmel’s free assembler:
AVRASM -I UARTAVR.ASM
A Simple Hardware
The Atmel ATtiny11 is an inexpensive MCU (only 52 cents each or
$38/100). It’s an eight pin chip,
without a built-in serial port. However,
using the circuit shown in Figure 2 and
the simple assembly language program
in Listing 1, we will show that a working serial port had been created for it.
This circuit is simple enough to
build it on a breadboard. The LED is not
required; it only serves as a reminder
if you forget to plug in the power
supply and the circuit is not working.
Furthermore, you can even dispense
with the 1 MHz crystal (XTAL) and use
the chip’s internal 1 MHz RC-oscillator
instead. Either way, the demo still works.
Refer to both Figure 2 and Listing 1
and notice that ATtiny11 pin 7 (PB2) is
configured as RX, and pin 6 (PB1) is TX.
The Dallas Semiconductor’s DS275 is a
voltage level shifter which shifts the TTL
level to an RS-232 level, and vice versa.
J1 is a female DB- 9 connector.
Only three wires are necessary to
connect to the PC serial port and
match its DB- 9 male connector. You
can use three longer wires starting
from the breadboard and directly plug
each end of wire into the female
connector’s corresponding pin holes.
This facilitates the connection
between the DB- 9 and breadboard.
Now let’s look at the software
program in Listing 1. It contains four sections. The first section starting at RESET is
the main program. The remaining sections are three subroutines; their detailed
executions will be analyzed later, but
note that each subroutine corresponds
to an essential part of the UART: bit rate
generation, transmission, and reception.
So, we have a software UART here.
Then you use an ATtiny11 programmer
(such as the one published in the
February ‘06 issue of Nuts & Volts). You
can download all UARTAVR files from
the Nuts & Volts website ( www.nuts
volts.com). Notice that the size of this
demo program is quite small — it only
takes 94 bytes of program memory.
By the way, if you don’t have an
ATtiny11 or its programmer, but you
have an AT90S1200 (which is a 20-pin
Flash MCU and doesn’t have a built-in
serial port), you can still use the same
program source code. The only change
you need to make is in Figure 2;
change component U2 to AT90S1200
and the corresponding pin numbers.
Any Windows PC has a terminal
emulation program called Hyper Terminal,
which is the one we will use to interact
with the demo circuit. From the Windows
Start button, go ahead to HyperTerminal
and configure it as connected to:
COM1(or COM2), 9600 bps, 8N1. When
the HyperTerminal window along with a
“-“ cursor appears, it’s ready to go.
Power-up the demo circuit. You’ll
see an “X” appear on the screen.
Typing any character from the
keyboard will be echoed on the
screen (that’s what the program is
expected to do). At this point, the software serial port is functioning okay.
Timing is Everything
First and foremost, the UART
program must have a way to generate
bit rates. This is done by calling
the DLY49us routine, which delays
49 microseconds (µs).
Why go with a 1 MHz crystal?
Because at a 1 MHz frequency, one
clock cycle = 1 µs, and for all AVR
MCUs, most of their instructions take
just one clock cycle (or 1 µs) to
execute. This makes the timing
■ FIGURE 2.
Port Demo Circuit.
June 2007 53