UART Communication Echo Program trouble

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was just implementing Dean's UART tutorial. When I just echoed a single letter like UDR=97,it worked fine. But when I try the echo program i.e echoing the received byte back to the terminal i see nothing on the terminal. This is my code,clock is 12Mhz,Baud rate 9600 bps

//Without interrupts 
#include  
typedef unsigned char U8bit; 

int main (void) 
{ 
   U8bit c; 
  UCSRB =0x18 ;   // Turn on the transmission and reception circuitry 
   UCSRC =0x86 ; // Use 8-bit character sizes 

   UBRRL = 0x4D; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register 
   UBRRH = 0x00; // Load upper 8-bits of the baud rate value into the high byte of the UBRR register 

   for (;;) // Loop forever 
   { 
      while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been recieved and is ready to be read from UDR 
      c = 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 be written to it 
      UDR = c; // Echo back the received byte back to the computer 
   }    
} 
Last Edited: Sun. Sep 27, 2009 - 04:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please fix first code tag so the format is preserved.

Without going to deep into things I woud say that the AVR is not getting anything in so it cannot echo anything back.

If you are using a terminal (Hyperterminal?) that looks for hardware handshake then make sure you turn it OFF (NONE).

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i have that OFF,any other common bugs?. I am usin Bray's Terminal by the way.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excuse me, what AVR are you using? With the magic number approach you are using to set the control registers and without knowing which AVR you are using it is difficult to interpret the settings to the control registers.

As an experiment, modify your code as following:

   for (;;) // Loop forever
   {
//      while ((UCSRA & (1 << RXC)) == 0) {}; 
//      c = UDR; 
      c = 'U';
      while ((UCSRA & (1 << UDRE)) == 0) {}; 
      UDR = c; 
   }   

You now have your original code "rewired" to just send characters. If this works, the problem is in receive.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am pretty sure my transmit is fine..I am skeptical about the RECEIVE part. Any common issues in receive?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is your hardware, namely the RS232 level translator?

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

wek wrote:
What is your hardware, namely the RS232 level translator?

JW


MAX -232...my baud rate and hardware settings are right i assume..as I am able to tranmit strings and characters.

when I tried this code-

#include
#include "usart.c"

void main()
{
unsigned char c;
USART_init(77);
c='U';
while(1)
{
USART_Transmit(c);
c=USART_Receive();
}

}

on Bray's terminal I received a 'U' as expected.Then I type a character in "Transmit Terminal" in Bray's I receive nothing back on the terminal. So it's -
Getting stuck in Receive loop,but I am confident my hardware is fine,I am including my custom made header file for perusal to see if there are any software/code issues.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why don't you jumper rx/tx on the avr to test everything else 'upstream' first. If you can't echo in the pc app that way, no use troubleshooting the avr (yet).