Thursday, April 11, 2013

Tutorial - USART and AVR

USART - a tutorial on how to setup a simple communication between the AVR chip and the computer!

In this tutorial i will go through the steps on how to get the USART on an Atmega88 to send and receive commands from and to the computer.

First of all you need the right hardware and software:

-Atmega88 - the microchip

-USB to COM-port adapter - this one works on windows 7 64 bit, mac and linux. (Drivers)

- Termite - a free windows program i used before i made my own (which i haven't yet decided if i should release or not). Anyway Termite has the capability of logging the correspondence to a file, and also has a timestamp function which is very useful!

Setup
You only need to connect three of the cables from the USB to COM adapter to the Atmega88.
Atmega88        COM adapter
   GND      =>       GND
   TXD       =>       TXD (PD1)
   RDX      =>        RXD (PD0)

USB 2.0 to TTL UART 6PIN Module Serial Converter CP2102 STC PRGMR Free cable













Then if you want to you can use either the 5 V or 3,3 V pin from the COM adapter to supply the power to the AVR.

Programming
I haven't put down a lot of work on this so it's probably not optimal, but i will show you how i got it to work:
I had problems when i used the chip on standard 1MHz, but it worked perfectly when changing to 8MHz, so i start by the includes like this:


Since the chip is fused to set the clock prescaler to a division of 8 (8MHz/8=1MHz) as factory default, i simply just change the prescaler back to "1" in code. This is accomplished by this little function: 








And the USART is initialized by calling this function

 To send a byte to the computer, use this function:








And to receive a byte from the computer:









Instead of constantly calling the USART_Receive-function, I strongly recommend you to setup an interrupt, that automatically triggers when the computer sends something to your chip. The interrupt is initialized like this:


And put the function that gets triggered by the USART receive interrupt at the very bottom of your code:


 Now here is the main function, that just waits for a USART command from the computer:












If you for example want to send "Kalle" to the computer you would write:







Setting up computer
First of all, when inserting the USB to COM-adapter, Windows will not find the right driver, and will direct you to the silicon labs website. This usually works great, but if not,  here is the link to the drivers.

After installed, go to device manager and make sure it is set like this (default value i think, and mine still works if i change the "Stop bits" to one):









Now install and open "Termite.exe" and press the "Settings" button:

It will look something like this: change the "Port" to the port the computer chose for your adapter, and set it up like this: (no need to log the file, and put a timestamp, but nice features that i usually have set)

















When pressing the "Log File" you get to choose where to put the logged file, and when checking the "Timestamp" you get to choose how long time every package-set is allowed to take, to still get on the same row (timestamp)...

Example:












Here i send "011" to the chip (type in the bottom row and press enter), which returns ":Sent! 1#" (which is just something the chip i have connected right now is programmed to answer when it successfully sent the package "011" to my home made IR-RF Remote control after 1 retries)


I think it is possible to use USART on a ATtiny chip as well, but haven't looked in to that yet! (if anyone has a working code for that, please share your experiences!)

Hope this is helpful... it took me a long time to find a free program like Termite that has a timestamp and a log-file option, the only thing it lacked was the TCP-server ability that mine has ;)



If you have questions i might be able to help out in the comment field =)

7 comments:

  1. I have some questions, http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=144820,
    I wonder what kind of USB to COM-port adapter you used? I use one labeled pl2303HX.
    How long the Dupont Line is?

    ReplyDelete
  2. While this transcript agrees to errors.
    Please source code download.

    ReplyDelete
  3. Thanks for the excellent blog on avr's.
    i found sei() [Global interrupts] call is missing in the main function due to which the ISR(USART_RX_vect) will never get called.

    ReplyDelete
  4. Just like your nrf24l01+ tutorial, this was very helpful. Thanks!

    ReplyDelete
  5. MestiQQ Adalah perusahaan judi online KELAS DUNIA ber-grade A

    Sudah saatnya Pencinta POKER Bergabung bersama kami dengan Pemain - Pemain RATING-A

    Hanya dengan MINIMAL DEPOSIT RP. 10.000 anda sudah bisa bermain di semua games.

    Kini terdapat 8 permainan yang hanya menggunakan 1 User ID & hanya dalam 1 website.
    ( POKER, DOMINO99, ADU-Q, BANDAR POKER, BANDARQ, CAPSA SUSUN, SAKONG ONLINE, BANDAR66 )

    PROSES DEPOSIT DAN WITHDRAWAL CEPAT Dan AMAN TIDAK LEBIH DARI 2 MENIT.

    100% tanpa robot, 100% Player VS Player.
    Live Chat Online 24 Jam Dan Dilayani Oleh Customer Service Profesional.

    Segera DAFTARKAN diri anda dan Coba keberuntungan anda bersama MestiQQ
    ** Register/Pendaftaran : WWW-MestiQQ-POKER
    Jadilah Milionare Sekarang Juga Hanya di MestiQQ ^^

    Untuk Informasi lebih lanjut silahkan Hubungi Customer Service kami :
    BBM : 2C2EC3A3
    WA: +855966531715
    SKYPE : mestiqqcom@gmail.com

    ReplyDelete