Lately, I have been trying to serially communicate raspberry pi with Atmega32 using UART. The raspberry pi has to calculate some coordinate value and transmit it to Atmega which controls motors. I took some help in writing the code from internet for raspberry pi in python and wrote receiver code for Atmega with the help from its data sheet. I transmitted some character data from raspberry pi. I tried to check whether correct output is coming, by sending the received data to port A of atmega-32 but the output is completely unexpected (some leds glow while some do not at random). Could anyone tell me, what I have been doing wrong?
Transmit section code (Raspberry pi):
import serial
from time import sleep
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=None)
while 1:
pin = raw_input("Activate pin: ").strip("\r\n")
port.write(pin)
sleep(0.1)
Receiver section code (Atmega 32):
#include <avr/io.h>
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <util/delay.h>
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
void uart_init(void);
void uart_transmit(unsigned char);
unsigned char uart_receive(void);
int main(void)
{
unsigned char a, num;
char buffer[10];
uart_init();
DDRA = 0xff;
while (1)
{
a = uart_receive();
PORTA = a;
_delay_ms(100);
}
}
void uart_init(void)
{
UBRRH = (BAUDRATE>>8);
UBRRL = BAUDRATE;
UCSRB |= (1<<TXEN)|(1<<RXEN);
UCSRC |= (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
}
void uart_transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR = data;
}
unsigned char uart_receive(void)
{
while(!(UCSRA) & (1<<RXC));
return UDR;
}