SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By smdFan
#57764
Hi there,

What is wrong with the following code. I wanted to see the output on my PC with Bray++ but it is not working. What could be wrong?





#include <avr/io.h>


void main(void)
{
// USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x17;

printf("Hello World!\r");
}
By theatrus
#57770
printf() on WinAVR is not bound to any device by default.

Either write byte by byte to the UART data register, or use the fdevopen() function they provide.
By smdFan
#57775
theatrus wrote:printf() on WinAVR is not bound to any device by default.

Either write byte by byte to the UART data register, or use the fdevopen() function they provide.

How would I write it to data register. Is the following correct?

UDR ="A";
By theatrus
#57778
Yes, but you'll also have to wait until it is empty before loading the next byte. Off hand I don't remember the register/bit to check for.
By wiml
#57820
UDRE in UCSRA. Something like this:
Code: Select all
while(!( UCSRA & (1<<UDRE))) {
   /* USART data register is not Empty, keep looping */
}
UDR = somechar;  /* store the character into the data register */
/* USART transmit is automatically started by writing to UDR */
In more complicated situations you'd set up an interrupt routine which would be invoked by the USART when it's ready to accept another byte, and the ISR would retrieve the byte from a fifo or ring buffer somewhere.
By smdFan
#57821
wiml wrote:UDRE in UCSRA. Something like this:
Code: Select all
while(!( UCSRA & (1<<UDRE))) {
   /* USART data register is not Empty, keep looping */
}
UDR = somechar;  /* store the character into the data register */
/* USART transmit is automatically started by writing to UDR */
In more complicated situations you'd set up an interrupt routine which would be invoked by the USART when it's ready to accept another byte, and the ISR would retrieve the byte from a fifo or ring buffer somewhere.

winl,

Thanks for responding. I have used exactly the same thing which I got from datasheet. But still have problem. The problem seems to be in initializing the UART. If you can not initialize then not transmission or reception. Can you please have a look at the initialization part? I tried like the above and like the datasheet, with WinAVR and with Codevision. I could not do it.

Here is what I have or do:

A laptop with only one serial com1, I connect this com1 to RS232 CTRL of STK500 After downloading the program to AVR, I take the serial cable connected to PC and connect it to RS232 spare of STK500, establish connection between the STK500 and Brays++ terminal but nothing happens.

The setting on Bray++ and STK500 are the same, or so I believe not sure about STK500.
I forgot to mention I use ATMEGA16

I would appreciate your help.
By stevech
#57827
I didn't look closely at your UART setup code, but just wanted to assure you know that printf() and puts() and the rest of the stdio cannot work unless you connect a low level UART output function with the C library stdio functions. With GCC/WinAVR, it's done as mentioned above, and as documented in WinAVR's help files.

The most common newbie error in UARTs is calculating the baud rate incorrectly, e.g., crystal frequency, etc. There are two or three nice freeware utilities to calculate the settings: AVRcalc is one I can recall.

also, check out the examples in the projects section of avrfreaks.net such as

http://www.avrfreaks.net/index.php?modu ... tem_id=563
By smdFan
#57836
stevech wrote:I didn't look closely at your UART setup code, but just wanted to assure you know that printf() and puts() and the rest of the stdio cannot work unless you connect a low level UART output function with the C library stdio functions. With GCC/WinAVR, it's done as mentioned above, and as documented in WinAVR's help files.

The most common newbie error in UARTs is calculating the baud rate incorrectly, e.g., crystal frequency, etc. There are two or three nice freeware utilities to calculate the settings: AVRcalc is one I can recall.

also, check out the examples in the projects section of avrfreaks.net such as

http://www.avrfreaks.net/index.php?modu ... tem_id=563


Thanks a lot for the information. I downloaded and will put it to use. Please provide me the name of the other untilities.

I use the STK500 with ATMEGA16 and NO additional crystal. So what is my Crystal Frequency? Is it 3.6864 MHz?

Regards
By theatrus
#57839
You'll then be using the RC oscillator inside the microcontroller. If you open up the STK500 programming window, there is a section where you set the oscillator speec.

I usually don't find RC oscillators steady and consistent enough for UART communication, especially at higher speeds.
By smdFan
#57840
theatrus wrote:You'll then be using the RC oscillator inside the microcontroller. If you open up the STK500 programming window, there is a section where you set the oscillator speec.

I usually don't find RC oscillators steady and consistent enough for UART communication, especially at higher speeds.
You said:

"STK500 programming window"

Are you refering to STK500 manual? Thanks

Are RC Oscilators of all AVR the same frequency?


.