Simple Serial communication with a attiny 2313

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tge1986
 
Posts: 28
Joined: Mon Jul 21, 2008 10:09 pm

Simple Serial communication with a attiny 2313

Post by tge1986 »

I'm trying to get a simple serial application to work with a attiny2313, I'm trying to send the chip a value (1-5) and have it turn on one of 5 pins that corresponds with that value. I've tried to just get a simple serial program to work with the chip but even that won't work for me. here is the code I'm working with:

Code: Select all

#include <avr/io.h>

#define F_CPU 16000000UL
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

int main (void)
{
   char ReceivedByte;

   UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
   UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

   UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of t
   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte o

   for (;;) // Loop forever
   {
      while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been recieved and is
      ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived"

      while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to b
      UDR = ReceivedByte; // Echo back the received byte back to the computer
   }   
} 
there is something wrong with this code and i can't figure it out. when I use a serial port monitor and send it something and wait for a response nothing comes back. I have a 16hz crystal attached to the proper ports and everything else seems right. I'm following two different tutorials to try to get this to work the first one is on the AVRfreaks site it's the Using the USART with AVR-GCC tutorial and then i found some other tutorial on google because I kept getting errors because of the line

Code: Select all

 UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);

something about URSEL not being declared in the scope, so i found a tutorial that left the URSEL out. The AVR freaks tutorial is for the Mega16 so maybe that's the issue I'm coming up with.
-Tim

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Simple Serial communication with a attiny 2313

Post by mtbf0 »

tge1986 wrote:something about URSEL not being declared in the scope, so i found a tutorial that left the URSEL out. The AVR freaks tutorial is for the Mega16 so maybe that's the issue I'm coming up with.
oh, yeah. you can say that again.

you'll have to take a peek at the 2313 datasheet to get the correct names for its registers and bits.

User avatar
tge1986
 
Posts: 28
Joined: Mon Jul 21, 2008 10:09 pm

Re: Simple Serial communication with a attiny 2313

Post by tge1986 »

What am i looking for in the data sheet? I'm quite new to the AVR scene, after playing in the shallow end with the arduino i'm trying to go to the deep end and use AVR's outside the arduino environment.

Update:
I found what I was looking for in the data sheet, however it did not seem to solve the problem I'm having. I've followed the example code they use in the data sheet and it still doesn't work I receive lots of gibberish characters. Is is a timing issue? I read somewhere that I need to burn the fuse to tell the chip that i'm using an external crystal is that true? if so how do i burn fuses that is something I've never done before. I'm using Programmers Notepad to write the code and I'm compiling it with AVR-GCC, I have to burn fuses in the Makefile, thats all i know about burning fuses.

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Simple Serial communication with a attiny 2313

Post by mtbf0 »

coming form the factory your 2313 is running at 1MHz. you'll need to run avrdude using the same programmer type, part and port options as in your Makefile and the option

Code: Select all

-U lfuse:w:0xee:m
to turn on the fuse for high frequency external crystal and to turn off clock divide by 8.

trialex
 
Posts: 189
Joined: Mon Apr 03, 2006 6:25 pm

Re: Simple Serial communication with a attiny 2313

Post by trialex »

This thread makes you realise how good the arduino combination of hardware and software is in making microcontrollers easy to use.

Kudos to you though for working on the 2313 - if you could post up your code when you get serial comms working that would be awesome - I'll be using it for sure!

raykholo
 
Posts: 26
Joined: Fri Feb 19, 2010 6:54 pm

Re: Simple Serial communication with a attiny 2313

Post by raykholo »

I need something very similar to this, for an Attiny25 micro. All it needs to do is power up, transmit a 10 byte string that I define in the program, and then do nothing or sleep, doesn't matter which.

I take it that this is a 2 wire interface?

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: Simple Serial communication with a attiny 2313

Post by uhe »

If people refer to a 2-wire interface they normaly talking about I2C not RS232.
Both are using two wires but they are used differently. I2C has one wire for clock and one for data, RS232 has one wire for sending (TX) and one for receiving (RX). AFAIK the Tiny25 doesn't have a RS232 interface (USART).

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple Serial communication with a attiny 2313

Post by adafruit_support_bill »

At a minimum, RS-232 requires a ground as well.

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: Simple Serial communication with a attiny 2313

Post by uhe »

Since fiber optic is the only wired connection I know of that will work without a GND connection, I left it out. Of course there is always a "+one ground wire" even if the interface is called "1-Wire" ;)

raykholo
 
Posts: 26
Joined: Fri Feb 19, 2010 6:54 pm

Re: Simple Serial communication with a attiny 2313

Post by raykholo »

Allow me to clarify. Yes, I was talking about i2c. When I said "two wires" I was referring to the number of physical wires required. The tiny25 will be on a separate pcb that will plug in to the main module with arduino inside it via usb connectors. Hence, power, ground, and the two wires required for i2c - clock and data.

Programming wise, will I need to drastically change the above example, or only make some minor changes?

Thanks

uhe
 
Posts: 178
Joined: Mon Sep 03, 2007 4:50 pm

Re: Simple Serial communication with a attiny 2313

Post by uhe »

You can't use it since it's about RS232 (USART) and not i2c.

raykholo
 
Posts: 26
Joined: Fri Feb 19, 2010 6:54 pm

Re: Simple Serial communication with a attiny 2313

Post by raykholo »

Oh, now I understand what you were/ are saying. Thanks for pointing that out.

shashankkulkarni
 
Posts: 1
Joined: Wed Aug 10, 2011 6:46 am

Re: Simple Serial communication with a attiny 2313

Post by shashankkulkarni »

For a very basic yet conceptual tutorial on serial communication using RS232 on ATmega32/16...you can refer to the following link http://shashank-kulkarni.blogspot.com/2 ... a3216.html

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”