Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

problem with receiving and transmitting

Status
Not open for further replies.

nikhitha

New Member
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;
}
 
Does the communication work using cable/wires? You should get that working first, then change the cable to wireless module. Also test your LCD that it displays values correctly. Check that the wireless units have the correct baud-setting.
 
Last edited:
// 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
}

I think this should be:

while(!((UCSR0A) & (1<<RXC0))); // wait while data is being received

.. might not make a difference, but still, you should change that. It is more clear that way.
 
Last edited:
All the connections are according to the website that I have mentioned and I did all the initializations according to the specifications that where given in the website.
whatever the value I send LCD displays 255. I even removed the wireless module n connected the MCU directly in null mode, at that time LCD shows the correct output.
So what I should do now.
 
All the connections are according to the website that I have mentioned and I did all the initializations according to the specifications that where given in the website.
whatever the value I send LCD displays 255. I even removed the wireless module n connected the MCU directly in null mode, at that time LCD shows the correct output.
So what I should do now.

Connect the two MCUs using wires and see if that works. Then you know if the problem is in the code or in the wireless modules.
 
Are you sure that your MCUs are running at 16Mhz?
If so, how are you sure.. do you have external 16Mhz oscillator with correct fuse settings and you have cleared the CKDIV8 fuse-bit that is set by default?
 
yes I connected two MCU's n i got the output, so the problem is in compatibility with wireless module n MCU?
 
yes I connected two MCU's n i got the output, so the problem is in compatibility with wireless module n MCU?
..
why do I have to clear the CKDIV8 fuse-bit?

I think the problem is in the Baudrate then. Maybe your F_CPU define is not correct. The CKDIV8 fuse setting divides the clock by 8. Try to change your F_CPU setting to:

#define F_CPU 2000000UL // CPU clock frequency (16Mhz oscillator with CKDIV8 set)

Remember to change this to both codes.

If that does not work, you can try one of these:
#define F_CPU 1000000UL // Default factory setting (Internal osc with CKDIV8 set)
#define F_CPU 8000000UL // Internal osc with CKDIV8 cleared
 
Thank you!!!! When I set the F_CPU to 2MHz, I got the output. Thank you so much!!!!

Ok, good :)
That means that you have working 16 Mhz external oscillator, but the CKDIV8 fuse bit is set and the MCU runs at 2 Mhz.
If you need to run your MCU at 16 Mhz, you need to clear the CKDIV8 fusebit. (just remember to change the F_CPU setting also if you clear the fusebit)
 
I understand. I had a hunch that the fuse bits which I set and clock frequency were different. But I did not know much about the fuse bits. Anyways, Thank you.
 
My project is Green house Monitoring using Wireless sensor networks. I am trying to send the data between two microcontrollers using uart serial communication. Although I am able to send and receive the data, I am also receiving junk values in addition to the sent data. I know this is due to the fact that the transmitter and receiver are not synchronized. Can anyone suggest a way to transmit and receive strings of data properly without any problems??
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top