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.

UART communication between atmega128 and arduino UNO

Status
Not open for further replies.

nranawat

New Member
I am trying to do UART communication between atmega128 and arduino UNO. Both are running at 9600 baud rate( checked both by doing serial communication with laptop using xbee radios from zigbee),but i am not receiving data properly. The data is following some pattern but i am not able to understand the relation.The lower four bits are always correct but upper four bits follow some weird pattern.


should receive---------actual received
1------------------------9
2------------------------A or 8
3------------------------B or 9
4------------------------8 or A
5------------------------9 or B

my atmega128 and arduino code are as follow: arduino sends one value and atmega128 displays the recieved value on array of 8leds on PORTA.

Arduino code
void setup()
{
Serial.begin(9600);
Serial.write(33);
}

void loop()
{
}

Atmega128 code:

#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>

volatile unsigned char tdata, rdata;

void USARTInit()
{
UBRR0L = 0X67;
UBRR0H =0x00;
UCSR0C= ((1<<UCSZ00)|(1<< UCSZ01));
UCSR0B=(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
}


ISR(USART0_RX_vect)

{
rdata = UDR0;
PORTA = rdata;
}



void init_devices()
{
cli();
DDRA =0xff;
USARTInit();
sei();
rdata=0;
}


int main(void)
{
init_devices();

while(1)
{
}

}
 
Have you connected the grounds of the two microcontrollers together?

Arduino code
void setup()
{
Serial.begin(9600);
Serial.write(33);
}
Is that value "33" supposed to be hexadecimal, or decimal?
Could you give more examples of data send and what you receive.. include code.
 
Last edited:
yes ground is common, 33 is decimal value... for 31 it gives 8E, 32 ---8F, 33 ---91 for 1 it gives 81, 2--- 82, 25 ---99, 26--9A, code is same have just changed value in serial.write.
 
yes ground is common, 33 is decimal value... for 31 it gives 8E, 32 ---8F, 33 ---91 for 1 it gives 81, 2--- 82, 25 ---99, 26--9A, code is same have just changed value in serial.write.
So, the lower four bits are not always correct.. You seem to have the MSB always 1 at the receiving end.. that makes me suspect a baudrate mismatch.
.. although the first two pairs you listed tells otherwise.

How long wire do you have between the two?
 
Last edited:
yes the problem is with baud rate only as in case sending 33 in a loop it gives 81 sometimes and 91 sometimes.
I have checked default fuse bit settings for arduino**broken link removed**. so it is set for 16mhz external crystal only.
For 128 i have used the fuse bits as: lfuse:FF, hfuse:99 (settings for 16mhz external crystal)
for register settings of 128 i have posted my code above.
What else i should be checking to make my baud rate correct?
 
Can you check the data with an oscilloscope? That should get those problems sovled in about ten minutes.
Also as misterT asked, how are the two micros physically connected? How long is the wire?
 
unfortunately i do not have oscilloscope with me.

have connected tx of one with rx of other and vice versa and a common ground... wire length is around 10 cm .
 
Last edited:
Could you do one test.. send these 8 messages from the arduino to the atmega128 and post what is received. And do it many times in a loop with a delay between transmissions... I do not like the fact that you are sending one message after reset and then nothing. Make a loop that sends the same character over and over again, with 200 ms delay between sends.

send these:
Serial.write(0b00000001);
Serial.write(0b00000010);
Serial.write(0b00000100);
Serial.write(0b00001000);
Serial.write(0b00010000);
Serial.write(0b00100000);
Serial.write(0b01000000);
Serial.write(0b10000000);

.. you get the idea.. post what was received after each of those.
 
The MSB is always high as you said
the output is :
0b10000001
0b10000010
0b10000100
0b10001000
0b10010000
0b10100000
0b11000000
0b10000000

for 0b00000000 it gives 0b10000000
 
hey.. my code is working now....
i tried my code with internal clock of atmega128 and it is working fine, it means the problem was with my extrnal crystal.. do crystals start work slowly after some time.. i ran a code for led blinking in 10s using _delay_ms function in loop and measured the time using stop watch it was coming more than 11s...was my external crystal the actual problem ??...i cannot just accept the idea of crystal running slow after sometime....
 
Do you have the oscillator fuse set properly to match your crystal? Can you post a circuit diagram?
 
hey.. my code is working now....
i tried my code with internal clock of atmega128 and it is working fine, it means the problem was with my extrnal crystal.. do crystals start work slowly after some time.. i ran a code for led blinking in 10s using _delay_ms function in loop and measured the time using stop watch it was coming more than 11s...was my external crystal the actual problem ??...i cannot just accept the idea of crystal running slow after sometime....
Possible, if you don't have the correct load caps connected to it, and/or it's been abused (eg. dropped, frozen, burned, twisted, shanked, pounded, beaten, stuffed, licked, kicked, verbally rebuked, etc)
 
my fuse bit settings for external crystal were lfuse:FF, hfuse:99 , calculated from here and cross checked... i can draw the exact ckt diagram as i am using a development board bought online(here) and crystal arrangement is below the small PCB of atmega.
 
You didn't happen to notice that the ATMega128 board is using a 14.7456 Mhz crystal did ya?
And your code is set up for a 16Mhz oscillator frequency...
 
Seems we cross posted. Any crystal with known frequency would do just fine. Or he could put the correct value of the actual crystal into the code.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top