#include <stdio.
h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BAUD 19200 //Baud rate
#include <util/setbaud.h>
int inChar;
int inChar1;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
//Initialise the LCD
lcd.begin();
lcd.backlight();
lcd.setCursor(0,0);
//Initialise serial comm
Serial.begin(19200);
USART_Init();
void loop() {
//Write data to serial
unsigned int data=0x80;
uart_putchar(data);
delay(20);
unsigned int data2=0x81;
uart_putchar(data2);
//Read serial input
if(bit_is_set(UCSR1A, UDRE1)){
delay(100); //allows all serial sent to be received together
lcd.clear();
while(!(UCSR1A & (1<<RXC1))){
//Serial.println("OK......");
inChar = uart_getchar();
inChar1 = uart_getchar();
lcd.print(inChar);
lcd.print(inChar1);
}
}
delay(30000);
}
//Initialise UART1,Per example in Atmel datasheet
void USART_Init(void)
{
UBRR1H = UBRRH_VALUE;
UBRR1L = UBRRH_VALUE;
#if USE_2x
UCSR1A |= _BV(U2X1);
#else
UCSR1A &= ~(_BV(U2X1));
#endif
UCSR1B = _BV(UCSZ12);
UCSR1C = _BV(UCSZ11)| _BV(UCSZ10);
UCSR1B = _BV(RXEN1) | _BV(TXEN1);
//Per example in Atmel datasheet at page 232
void uart_putchar(unsigned int data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR1A & (1<<UDRE1)))
;
/* Copy 9th bit to TXB8 */
UCSR1B &= ~(1<<TXB81);
if ( data & 0x0100 )
UCSR1B |= (1<<TXB81);
/* Put data into buffer, sends the data */
UDR1 = data;
}
//Per example in Atmel datasheet at page 235
unsigned int uart_getchar(void)
{
unsigned char resh, resl;
//wait for data to be received
while ( !(UCSR1A & (1<<RXC1)) );
//Get 9th bit, then data from buffer
resh = UCSR1B;
resl = UDR1;
//Filter the 9th bit, then return
resh = (resh >>1) & 0x01;
return ((resh << 8) | resl);