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
jraldrin
PostPosted: Apr 03, 2012 - 03:50 PM
Newbie


Joined: Apr 03, 2012
Posts: 7


Hi,

I'm starting my first project and I am trying to write a simple program to periodically output a character on a serial port. I've borrowed the code from tutorial programs I've seen and from the code in the app I need to modify.

If possible could someone eyeball and tell me what could b wrong? Obviously it is not doing what I expect.

Dev setup is:
Avr Studio 4.19
JTAGICE MKII
BDMicro-Mavric-IIB, ATmega128

Code:
int main()
{

   cli();

    //USART1
    // communicates with PC at 57.6kpbs
    // no interrupts, just polling
    UCSR1A = 0;
    UBRR1H = 0;
    UBRR1L = 16; //assumes U2X set to 0
    UCSR1B = 0x18; //0001 1000
    UCSR1C = 0x06; //0000 0110


//   sei();   


   char outChar = 'A';

   _delay_ms(500);

   while(1)
   {
      UDR1 = outChar;         
      _delay_ms(500);

   }

}
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Apr 04, 2012 - 12:13 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20374
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

First of all this is the Xmega forum, I'll move the thread.
2nd please use the CODE button when posting code. I'll fix this one.

Does the board have RS232 driver chips? I can't remember.
The data sheet has example code on how to PROPERLY send data out.

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Apr 04, 2012 - 12:24 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21261
Location: Orlando Florida

This should work, but only as long as the delay is longer than it takes the char to shift out. Usually you call a putchar(char c) function that polls the Uart Data Register Empty bit until it is empty, THEN you store the character in the UDR. Bet if you search for 'putchar' you'l see how someone else does it.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
ka7ehk
PostPosted: Apr 04, 2012 - 12:48 AM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA

Also, make sure that you are using a crystal to set the CPU clock frequency and that you have the baud rate bits set properly for the actual frequency of operation.

Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
cellcon
PostPosted: Apr 08, 2012 - 04:19 PM
Newbie


Joined: Apr 05, 2010
Posts: 15


I don't know how fast your clock source is, but the _delay_ms function has restrictions on how big the parameter value may be for correct operation:
The maximal possible delay is 262,14 ms / frequency in MHz.

Also you have to declare F_CPU before using the delay function. It should contain the CPU frequency in Hertz.

Both conditions can be found in the _delay_ms comments. Studio 5 shows it reliably on mouse over, maybe you should update your studio.

A better solution should be the active waiting approach, using a while loop (this is XMEGA code, check your datasheet for the status register contents of your controller):
Code:
// Wait for the transmit buffer to be empty
while ( !( USART_NAME.STATUS & USART_DREIF_bm) );

BTW: Makros like "USART_DREIF_bm" are defined in the header file for your cpu. The name of the file can be found in the file "io.h" in the WinAVR installation folder at avr/include/avr. For your ATmega128 it's the file "iom128.h" in the same directory. The makro names look similar to the register content names from the datasheet. Using the intelli sense in studio 5.1 it is easy to find them while typing - without leaving your own file.
 
 View user's profile Send private message  
Reply with quote Back to top
jraldrin
PostPosted: Apr 18, 2012 - 03:28 AM
Newbie


Joined: Apr 03, 2012
Posts: 7


Thanx for your help. Turned out I had some power issues.
 
 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