Atmel ATmega8 Bitbanger UART
Posted: July 16, 2008
Being the total loser I am and not being able to get a real girl, I made this project to show I can at least bang bits. Basically this is a software UART. Instead of using the UART built into the Atmel Atmega8 chip to output a message to a computer over the serial port (rs232) I wrote code to program the start bit, data bits, and stop bit. This is what's known as bitbanging.
Btw, this code could have been done a little simpler.. for example by combining TX_BUSY and TX_STATE, but I don't feel like working on this anymore.
So for anyone not familiar with UARTs or rs232, here's a small explanation of what's going on here. Basically there are two hardware components here. The microcontroller which outputs voltages 0 and +5 on data pin PB0 and the DS275 which converts the TTL serial voltages to standard rs232 which a typical PC would expect. Using TTL voltages 0v = 0, and 5v = 1. With rs232, +3v to +25v = 0, and -3v to -25v = 1.
A single rs232 frame (using 8N1.. 8 bit data, no parity bit, 1 stop bit) will look like this: 0, 8 bits of data, 1. When the line isn't in use, we always keep the pin set to 1. So if we want to output the letter A over the UART, knowing A in binary is 01000001 we put the following things on PB0:
0 , 1, 0, 0, 0, 0, 0, 1, 0, 1
Note that we shift the binary code for A in reverse order (aka, least significant bit first) and the start bit is always 0 and the stop bit is always 1.
Now all we need is a timer. I'm running my circuit here at 4MHz and want a baud rate of 1200 so using 16 bit TIMER1 on the Atmega8, I can calculate 4,000,000 / 1200 = 3333.333 cycles per bit. Therefore we set TIMER1 to interrupt every 3333 cycles to possibly change the state of PB0. It can also be written that each bit is 1/1200 seconds (0.833ms) in length. Any questions? Feel free to email me.
I drew a schematic here with dia: bitbanger.png.
Copyright 1997-2021 - Michael Kohn