Transmitting and Receiving data on USART of AVR

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

Hello,

I am trying to Transmit data taken from a port of AVR via USART to a Port on another AVR. but when I simulate in proteus I only get some garbag data. I am sending the data as char on 9600 and both AVRs are at the same crystal and baud. here is my code.....Plz help

Tx

#include
#include
#include
#include

void USART_Init( unsigned int ubrr);
void USART_Transmit( char data );

#define FCPU 10000000
#define baud 9600
#define myubrr FCPU/16/baud - 1

int main()
{

char data;

USART_Init(64);   //set to 9600

DDRC=0x00;        //portc all input
//PORTC=0xFF;    //enable pull ups

while(1)
{
data = PINC;
USART_Transmit('J');     //send J
USART_Transmit('A');     //send start character 

USART_Transmit(data);    //send data

USART_Transmit('Z');     //send data

_delay_ms(200);
}

return 0;
}

void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}

void USART_Transmit(char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}

Rx

#include
#include
#include

#define FOCS 10000000
#define baud 9600
#define myubrr FOCS/16/baud - 1

void USART_Init( unsigned int ubrr);
char USART_Receive( void );

int main()
{

char data;
char temp;

DDRC=0xFF;

USART_Init(64);
while(1)
{
//if (UCSR0A & (1<<RXC0))//if something comes in..
data=USART_Receive();        
if(data!='A') continue;     //if not A receive again
data = USART_Receive();
temp = data;
data = USART_Receive();
if(data!= 'Z') continue;

PORTC = temp;               //output the received data to portc
_delay_ms(200);
}
return 0;

}

void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
//Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) )
;
/* Get and return received data from buffer */
return UDR0;
}

Thanks

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

On the transmit AVR what is connected to portc? If nothing then the floating inputs will give you random data.

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

Simulations are simulations. I would not rely upon them to much especially for anything that is timing critical.
Why not put the code into a real controller & get real results.
Stage 1. Test TX with Br@ys terminal
Stage 2. Test RX with Br@ys terminal
Stage 3. Integrate TX & RX

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

dksmall wrote:
On the transmit AVR what is connected to portc? If nothing then the floating inputs will give you random data.

I have connected switches on portc at the transmitter end. when I open or close any of the switch the garbage data changes.....thank you

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

I doubt whether Proteus has a "floating input random noise generator"...but who knows? The JA & Z should not be corrupted but the data will rarely look like anything other then "garbage" because it wont be ASCII characters.

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?