Create Your Own Software Serial Port
for these instructions execution time.
That’s why we created DLY49us.
Similarly, the RCV_CHR routine
receives a byte of data from the outside
world and stores it in register R17 or
RCVREG. The receiver keeps sampling
its RX line until a logic low is detected;
that is the beginning of start bit.
To find out the value of LSB, the routine pauses for three DLY49us. By doing
so, it samples the middle of the LSB. This
is the best position for correct sampling.
The incoming bit value is read and the
carry bit is set or cleared accordingly.
The instruction ROR RCVREG rotates
that bit into register RCVREG bit7 each
time, until all eight bits are transmitted
and count equals zero, so RCVREG has
converted the bits into a byte.
The technique of converting a byte
into a series of bits for transmission, or
converting a series of bits into a byte for
reception, is known as “bit-banging.”
Listing 1 continued ...
rcall DLY49us ; ~Half_Bitime
rcall DLY49us ; ~Half_Bitime
rcall DLY49us ; at the Middle of next bit
CLC ; clear Carry
sbic PINB, RX ; Test RX=’0’ or ‘1’?
SBR RCVREG, $80 ; set bit7=’1’ if RX=’1’
; otherwise don’t change
You might think that even though
the above demo program
works in simple cases, it may
not work in more complex
ones. I had the same
concern. I have not seen any
other product that’s built
using a software serial port;
all I’ve seen are just simple
demo circuits. I was
determined to give it a try,
however, I decided to make
an I2C EEPROM programmer.
Atmel has two very similar MCUs: the AT90S1200
and the AT90S2313. Both
are 20-pin chips; the
differences are the 2313 has
2K Flash memory and a
built-in serial port; the 1200
only has 1K Flash memory
and no serial port.
My strategy was two
steps: First, build an I2C EEPROM
programmer using the 2313, then try to
migrate it to the 1200 using my
software serial port. It took a few days
to complete the first step, however, the
migration took much longer than
expected. Originally, the 1200’s RX and
TX pins were not assigned the same pin
numbers as the 2313’s RX (pin2) and
TX (pin3). In experimenting I learned
these pins could only get the EEPROM
written correctly; reading was incorrect.
I still don’t know why it can’t read
correctly, but I finally discovered that if
the 1200 pin assignment was the same
as the 2313 (RX to pin2, TX to pin3),
then both reading and writing are correct. The 1200-based programmer completely works this way. The programmer
schematic is shown in Figure 3.
Notice this serial EEPROM programmer is wall-wart-less, meaning the power
supply is taken from the PC serial port
itself. This is convenient to the user. It’s
possible because the circuit contains
only three small chips; the total current
consumption is much less than 10 mA.
To program the 24CXX I2C
EEPROM, only two pins are needed: the
serial clock pin SCL and the serial
data pin SDA, which is bidirectional.
The programming command or data is
■ FIGURE 3. Schematic of
Wall-wart-less Serial EEPROM
June 2007 55