1. 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.
    Dismiss Notice

UART communication between atmega128 and arduino UNO

Discussion in 'AVR' started by nranawat, Apr 9, 2014.

  1. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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)
    {
    }

    }
     
  2. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Have you connected the grounds of the two microcontrollers together?

    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: Apr 9, 2014
  3. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    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: Apr 9, 2014
  6. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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 here. 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?
     
  7. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    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?
     
  8. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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: Apr 9, 2014
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    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.
     
  10. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    The MSB is always high as you said
    the output is :
    0b10000001
    0b10000010
    0b10000100
    0b10001000
    0b10010000
    0b10100000
    0b11000000
    0b10000000

    for 0b00000000 it gives 0b10000000
     
  11. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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....
     
  12. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    Do you have the oscillator fuse set properly to match your crystal? Can you post a circuit diagram?
     
  13. skimask87

    skimask87 Member

    Joined:
    Mar 23, 2014
    Messages:
    128
    Likes:
    10
    Location:
    Logan (Minot), ND
    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)
     
  14. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    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.
     
  15. skimask87

    skimask87 Member

    Joined:
    Mar 23, 2014
    Messages:
    128
    Likes:
    10
    Location:
    Logan (Minot), ND
    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...
     
  16. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    I would just try replacing the crystal and see what happens.
     
  17. skimask87

    skimask87 Member

    Joined:
    Mar 23, 2014
    Messages:
    128
    Likes:
    10
    Location:
    Logan (Minot), ND
    Ya, with a 16Mhz crystal.

    16Mhz = ~8.5% faster than 14.7456Mhz
     
  18. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    ONLINE
    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.
     
  19. nranawat

    nranawat New Member

    Joined:
    Apr 9, 2014
    Messages:
    8
    Likes:
    0
    damn... my code was for 16mhz.... took 16m for granted.......
     

Share This Page