Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
powerover
PostPosted: Feb 26, 2012 - 03:48 AM
Rookie


Joined: Jun 28, 2010
Posts: 24


Hi there guys I am new to micro-controller programming and right now I am trying to learn how to get the USART going on a ATmega164PA.

I found this tutorial (the one echos whatever it receives back to the pc) written by Dean Camera (also know as abcminiuser here) and followed it. It was clear and simple and I had the USART setup in no time, except that it isn't working properly and I can't figure out why.

Here is my code, pretty much the same as what is written in the tutorial:

Code:

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


void usart_init(void)
{
   UCSR1B |= (1 << RXEN1) | (1 << TXEN1);
   UCSR1C |= (1 <<  UCSZ10) | (1 << UCSZ11);
   

   UBRR1H = (BAUD_PRESCALE >> 8);
   UBRR1L = (BAUD_PRESCALE);
}

void usart_echo(void)
{

    char Receivedbyte;
   
      for (;;)
      {
         while ((UCSR1A & (1 << RXC1)) == 0);
   
         Receivedbyte = UDR1;
      
         while ((UCSR1A & (1 << UDRE1)) == 0);
   
         UDR1 = Receivedbyte;
   
      }   

}


and here is how I used it in the main:

Code:


int main(void)
{      

      usart_init();

                  
    while(1)
    {
      usart_echo(); //not working yet

    }
}


The micro is running at the default full speed of 8Mhz. The fuse that divide the clock speed by 8 isn't set.

I use Realterm on my PC, the PC is talking to the micro through a USB--serial converter cable, with common ground (the micro is powered by the 5V on the USB). Both TX and RX lines have 20Ohm series resistors.

When I send a character using Realterm, I immediately receives something back, but it is not what I sent, instead I receive some garbage code. This tell me that I got the settings wrong somewhere, probably the baud rate but I can't figure out specifically where I went wrong.


Any kind of help will be much much appreciated.

Thanks guys. [/code]
 
 View user's profile Send private message  
Reply with quote Back to top
dksmall
PostPosted: Feb 26, 2012 - 04:21 AM
Raving lunatic


Joined: Apr 16, 2001
Posts: 3522
Location: Phoenix, Arizona

Did you define F_CPU as 8Mhz?
 
 View user's profile Send private message  
Reply with quote Back to top
thygate
PostPosted: Feb 26, 2012 - 04:28 AM
Resident


Joined: Sep 19, 2005
Posts: 765
Location: Belgium

Does the USB Serial converter cable have a TTL level interface, instead of RS232 ?
 
 View user's profile Send private message  
Reply with quote Back to top
powerover
PostPosted: Feb 26, 2012 - 04:39 AM
Rookie


Joined: Jun 28, 2010
Posts: 24


dksmall wrote:
Did you define F_CPU as 8Mhz?


Hi there thanks for the reply. No I didn't. Do I have to define it as such? If so how do I do that? thanks.
 
 View user's profile Send private message  
Reply with quote Back to top
powerover
PostPosted: Feb 26, 2012 - 04:43 AM
Rookie


Joined: Jun 28, 2010
Posts: 24


thygate wrote:
Does the USB Serial converter cable have a TTL level interface, instead of RS232 ?


Hi there thanks for the reply.

It is TTL logic level, on the cable itself it says TTL-232R-3v3. I was considering using a level shifter chip but I was told that it should work with series resistors, just to limit the current.
 
 View user's profile Send private message  
Reply with quote Back to top
dksmall
PostPosted: Feb 26, 2012 - 05:07 AM
Raving lunatic


Joined: Apr 16, 2001
Posts: 3522
Location: Phoenix, Arizona

Depends on what you're compiling with. Assuming AVR studio, then it's part of the project settings.
 
 View user's profile Send private message  
Reply with quote Back to top
powerover
PostPosted: Feb 26, 2012 - 05:12 AM
Rookie


Joined: Jun 28, 2010
Posts: 24


dksmall wrote:
Depends on what you're compiling with. Assuming AVR studio, then it's part of the project settings.


I am using avr studio 5...can you please explain a bit more? thanks.
 
 View user's profile Send private message  
Reply with quote Back to top
dksmall
PostPosted: Feb 26, 2012 - 05:00 PM
Raving lunatic


Joined: Apr 16, 2001
Posts: 3522
Location: Phoenix, Arizona

I don't use AS5 some someone else will have to jump in. Typically you should have a menu command that get to Project Properties, and the frequency should be defined somewhere in the properties.
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits