HowTo: Serial communication between PC and Atmel attiny2313

The attiny2313 has a built in UART for serial communication. You need a few outside parts to allow it to communicate with a PC over a serial connection. The attiny2313 uses TTL signals. You have to convert them to true RS232 signals using an external converter. For this example I will use a MAX232.

The AVR has an internal RC oscillator. The Atmel data sheet says this oscillator is calibrated to within +- 10%. You may be able to get a serial connection to work, but because of the margin of error it is best to have your chip use an external watch crystal instead of the internal RC oscillator. I have been successful in getting serial communication to work using only the internal RC oscillator but your mileage may vary.

Attiny2313 datasheet page 27

Parts list:
1 MAX232IN ( mouser 595-MAX232IN)
4 16V 10uF capacitors ( mouser 140-MLRL16V10-RC)
1 3.6846 cyrstal
2 20pf capacitors
1 DB9 female serial connector

Below is the wireing schematic:
circuit diagram

To get the AVR to use the external 3.6846 crystal instead of the internal RC oscillator I had to burn the fuses. I am using linux and avrdude, so I ran this command:
avrdude -p attiny2313 -P /dev/parport0 -c dt006 -u -U lfuse:w:0xed:m

Now the avr will only run with the external crystal.

First we look at the data sheet see what UBRRL needs to be set for our baud rate and crystal. We will be transmitting at 2400 baud.
attiny2313 datasheet page 139

On the PC side I will be using minicom on a linux PC. Set minicom (or hyperterminal on windows) to 2400 baud, 8N1. Minicom gave me a few problems when it would first connect to the AVR. To get around this, I would start minicom, then AFTER minicom was running, I would power up the AVR. I am not sure why this happened. If I started minicom after the AVR was powered up, I would get some garbage characters only.

This sample program will have the Avr listen for a character, then transmit back the next highest character. So if you type in the letter “B”, the avr will send be the letter “C”. (This program is written C and should would with the gcc compiler)

The code may be downloaded here.
Code:
#include <avr/io.h>
#include <avr/interrupt.h>

/* Prototypes */
void InitUART (unsigned char baudrate);
unsigned char ReceiveByte (void);
void TransmitByte (unsigned char data);


/* --------------------------------------------------------------
 Main - program that recieves a character then transmits back the next character.  
An example would be if you send in an A, the chip will return a B
---------------------------------------------------------------- */
int
main (void)
{
  unsigned char i;
  InitUART (95);     /* Set the baudrate to
2400 bps using a 3.6846MHz crystal */
  while (1)
    {
      TransmitByte (ReceiveByte () + 1);       
/* Echo the received character + 1.  Example send in A then send out B */
      for (i = 0; i < 200; i++);
    }
}

/* Initialize UART */
void
InitUART (unsigned char baudrate)
{
  /* Set the baud rate */
  UBRRL = baudrate;

  /* Enable UART receiver and transmitter */
  UCSRB = (1 << RXEN) | (1 << TXEN);

  /* set to 8 data bits, 1 stop bit */
  UCSRC = (1 << UCSZ1) | (1 << UCSZ0);

}

/* Read and write functions */
unsigned char
ReceiveByte (void)
{
  /* Wait for incomming data */
  while (!(UCSRA & (1 << RXC)));

  /* Return the data */
  return UDR;
}

void
TransmitByte (unsigned char data)
{
  /* Wait for empty transmit buffer */
  while (!(UCSRA & (1 << UDRE)));

  /* Start transmittion */
  UDR = data;
}

Resources used to make this howto:
http://www.booksbybibin.blogspot.com/ (has a great free ebook on AVR programming)
Avrfreaks design note #026 (also includes an alternate way to convert from ttl to rs232) http://avrfreaks.net/modules/FreaksFiles/files/405/DN_026.pdf

did you find out why we have

did you find out why we have to power up AVR after the pc has been power up

fewer parts...

i used the internal clock at 8Mhz and it worked fine
over a 1m cable at 19200 baud. i didn't use an external
crystal.

i used the MAX233 instead, no need for extra caps (well
maybe one for good measure across the power supply). do
connect up the pins and *all* grounds on the MAX233
according to the datasheet. i incorrectly assumed that
the grounds (and other signals) were connected internally.

when testing, i programmed my attiny2313 to echo the
bytes received. if you interface with the serial port
on linux, remember to disable the character echo or
sending one character will trigger a stream of characters
instead. you may need to stty your port to raw to be able
to read the data (not sure about this, still experimenting).

while a bit of fun, unfortunately the serial ports are
fast disappearing...

Sending strings

Is it possible to send a string to the micro? If so, how do you modify the code to do it?

Caps

Are the caps necesary? I dont have any on me...

My hyper terminal wont let

My hyper terminal wont let me type
tyeo098(at)yahoo(dot)com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is used to make sure you are a human visitor and to prevent spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.