I'm trying to send data from ATmega328p to Raspberry Pi 3 with UART using Pis gpios.
The problem is Pi does not seem to be getting any data from the microcontroller.
Here is how I wired the microcontroller to Pi:
- ATmega328p VCC to Raspberry Pi 5 volt pin. changed to 3.3 volt pin
- ATmega328p GND to Raspberry Pi ground pin.
- ATmega328p RESET to Raspberry Pi GPIO #12.
- ATmega328p SCK to Raspberry Pi GPIO #24.
- ATmega328p MOSI to Raspberry Pi GPIO #23.
- ATmega328p MISO to Raspberry Pi GPIO #18.
- ATmega328p TXD to Raspberry Pi RX pin.
- ATmega328p RXD to Raspberry Pi TX pin.
Here is the C code for microcontroller:
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600UL
#define BRC ((F_CPU/16/BAUD)-1)
int main(void){
UBRR0H =(BRC >> 8);
UBRR0L = BRC;
UCSR0B = (1 << TXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
while(1){
UDR0 = '8';
_delay_ms(1000);
}
return 0;
}
And the python 2.7 script for Pi:
import serial
port = serial.Serial("/dev/ttyAMA0", baudrate = 9600, timeout=5)
port.flush()
while True:
rcv = port.read(10)
print "AVR is sending" + rcv
With sudo raspi-config
I have enabled serial port hardware and made login shell to be not accessible over serial.
EDIT
I've tested if UART pins are any functional by hooking Pi pin 8(TX) to pin 10(RX) and running a python script which I copied from this tutorial http://www.elinux.org/Serial_port_programming .
Here is the script itself:
import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, timeout=3.0)
while True:
port.write("\r\nSay something:")
rcv = port.read(10)
port.write("\r\nYou sent:" + repr(rcv))
The script is running but nothing is appearing on the screen. Seems like the script is unable to write to the port.
I've also double checked that serial is enabled in hardware and serial console is disabled.
In /boot/config.txt
enable_uart=1
is set.
while(!( UCSR0A & (1 << UDRE0)));
ISR(USART_TX_vect)
to light up an LED when transmission is complete. It appears that microcontroller does transmit the bytes.