i'm doing a project which requires to send small amount of data wireless from one MCU to another (using ATmega328p). i'm using RF Serial Data Link UART, 2.4 Ghz RF module from sunrom technologies (https://www.sunrom.com/161). but i'm receiving some garbage value like 255 again n again. so please him me out here.
Transmitting Code:
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR
// function to initialize UART
void uart_init (void)
{
UBRR0H = (unsigned char)(BAUDRATE>>8);
// shift the register right by 8 bits
UBRR0L = (unsigned char)BAUDRATE; // set baud rate
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
// enable receiver and transmitter
UCSR0C = (3<<UCSZ00); // 8bit data format
}
void uart_transmit ( unsigned char data)
{
while (!( UCSR0A & (1<<UDRE0)));
// wait while register is free
UDR0 = data;
// load data in the register
}
int main()
{
unsigned char i=8;
uart_init();
while(1)
{
uart_transmit(i);
_delay_ms(200);
}
return 0;
}
Receiving Code:
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"
// define some macros
#define BAUD 9600 // define baud
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR
// function to initialize UART
void uart_init (void)
{
UBRR0H = (unsigned char)(BAUDRATE>>8); // shift the register right by 8 bits
UBRR0L = (unsigned char)BAUDRATE; // set baud rate
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
// enable receiver and transmitter
UCSR0C = (3<<UCSZ00); // 8bit data format
}
// function to receive data
unsigned char uart_recieve (void)
{
while(~(UCSR0A) & (1<<RXC0));
// wait while data is being received
return UDR0;
// return 8-bit data
}
// main function: entry point of program
int main (void)
{
uart_init();
SetupPorts();
LCD_Init();
while(1)
{
LCD_Integer(uart_recieve());
// display the received value on LCD
_delay_ms(100);
// wait before next attempt
}
return 0;
}
Transmitting Code:
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define BAUD 9600
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR
// function to initialize UART
void uart_init (void)
{
UBRR0H = (unsigned char)(BAUDRATE>>8);
// shift the register right by 8 bits
UBRR0L = (unsigned char)BAUDRATE; // set baud rate
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
// enable receiver and transmitter
UCSR0C = (3<<UCSZ00); // 8bit data format
}
void uart_transmit ( unsigned char data)
{
while (!( UCSR0A & (1<<UDRE0)));
// wait while register is free
UDR0 = data;
// load data in the register
}
int main()
{
unsigned char i=8;
uart_init();
while(1)
{
uart_transmit(i);
_delay_ms(200);
}
return 0;
}
Receiving Code:
#ifndef F_CPU
#define F_CPU 16000000UL // set the CPU clock
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include "lcd.h"
// define some macros
#define BAUD 9600 // define baud
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) // set baud rate value for UBRR
// function to initialize UART
void uart_init (void)
{
UBRR0H = (unsigned char)(BAUDRATE>>8); // shift the register right by 8 bits
UBRR0L = (unsigned char)BAUDRATE; // set baud rate
UCSR0B = (1<<TXEN0)|(1<<RXEN0);
// enable receiver and transmitter
UCSR0C = (3<<UCSZ00); // 8bit data format
}
// function to receive data
unsigned char uart_recieve (void)
{
while(~(UCSR0A) & (1<<RXC0));
// wait while data is being received
return UDR0;
// return 8-bit data
}
// main function: entry point of program
int main (void)
{
uart_init();
SetupPorts();
LCD_Init();
while(1)
{
LCD_Integer(uart_recieve());
// display the received value on LCD
_delay_ms(100);
// wait before next attempt
}
return 0;
}