0
\$\begingroup\$

I'm working on a project which involves me to send data from AVR atmega16 microcontroller to a beaglebone black.

I had written a code for the atmega USART but when connected, the beaglebone black didn't receive any data. So I tried using an arduino uno to see what's going wrong. The USART interface between the arduino and beaglebone black worked perfectly but while connected to the atmega, the same problem kept occurring. I then tried doing USART between the atmega and arduino and the arduino didn't receive any data. The Baud rates are same on all the devices and I've even connected all of them to a common ground.

I'm using PySerial on the beaglebone black to receive the data. I'm posting the USART library I'm using for the atmega.

Can anyone please help me and tell me where I'm going wrong?

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

void usart_init()
{
UCSRB |= (1 << RXEN) | (1 << TXEN); //Turn on the transmission and reception circuitry
UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1);
// Use 8-bit character sizes

UBRRL = BAUD_PRESCALE;
// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}

unsigned int usart_getch()
{
while ((UCSRA & (1 << RXC)) == 0);
// Do nothing until data has been received and is ready to be read from UDR
return(UDR); // return the byte
}

void usart_putch(unsigned char send)
{
while ((UCSRA & (1 << UDRE)) == 0); // Do nothing until UDR is ready..
// for more data to be written to it
UDR = send; // Send the byte
}
\$\endgroup\$
1
\$\begingroup\$

It would be really good to know about used F_CPU...

  1. Atmega can't run correctly usart at 9600bps with 1MHz clock at default settings.
  2. (1<<USBS) is the receiver set to 2 stop bits ?
  3. The real clock can be set in fuses, not by defining F_CPU (just do 1s ledblink to check if 1s is exactly 1s).
  4. Internal oscillator can drift with voltage/temperature causing unacceptable baud error (>2%).

http://wormfood.net/avrbaudcalc.php

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.