This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

UART communication

I am working on a project having three micro controllers....
Out of these three one is master and others are slave....
according to pin conditions of slave,it send some value which is recieve by the master
and showed at lcd....
When I use one of the both slave then it is working properly but when I use both the slave it is not working...
My programming for this..

void uart_init()
{ TMOD = 0x20; SCON = 0x50; TL1 = 0xFD; TH1 = 0xFD; TR1 = 1;
}

void uart_send(bit_8 value)
{ SBUF = value; while(!TI); TI = 0;
}

bit_8 uart_receive()
{ while(!RI); RI = 0; return(SBUF);
}

I am using same program in both slave becoz both have same circuit...but the value send by the slave are differ...

Parents
  • You really do have to start to learn how to try to step through your program in your head and consider what really happens.

    You have an expression like:

    if(uart_receive()==12 || uart_receive()==22 || uart_receive()==32 || uart_receive()==42 ||
       uart_receive()==52 || uart_receive()==62 || uart_receive()==72 || uart_receive()==82 )
    


    But doesn't every call to uart_receive() hand until you have received a single character?
    How do you think this long expression will manage to synchronize with your two slaves?

    Notice the huge difference between your code and:

    unsigned char c = uart_receive();
    if (c == 12 || c == 22 || ... || c == 82) { ... }
    


    or maybe

    unsigned char c = uart_receive();
    if (c >= 12 && c <= 82 && (c%10) == 2) { ... }
    

    Beginner or not - if you have multiple friends around you and want to know where they think you should go out and eat do you ask them all at the same time? Or do you instead try to ask them one-by-one? Since all slaves are listening to the same transmission, all slaves will hear any message from the master - so how do let each slave know when a message is aimed for slave 1 or for slave 2?

    Using multiple devices on the same serial channel is like having multiple threads sharing a memory resource - you need some form of "critical section" that serializes the access to the shared resource, i.e. only allows one at a time to make use of the UART transmit.

Reply
  • You really do have to start to learn how to try to step through your program in your head and consider what really happens.

    You have an expression like:

    if(uart_receive()==12 || uart_receive()==22 || uart_receive()==32 || uart_receive()==42 ||
       uart_receive()==52 || uart_receive()==62 || uart_receive()==72 || uart_receive()==82 )
    


    But doesn't every call to uart_receive() hand until you have received a single character?
    How do you think this long expression will manage to synchronize with your two slaves?

    Notice the huge difference between your code and:

    unsigned char c = uart_receive();
    if (c == 12 || c == 22 || ... || c == 82) { ... }
    


    or maybe

    unsigned char c = uart_receive();
    if (c >= 12 && c <= 82 && (c%10) == 2) { ... }
    

    Beginner or not - if you have multiple friends around you and want to know where they think you should go out and eat do you ask them all at the same time? Or do you instead try to ask them one-by-one? Since all slaves are listening to the same transmission, all slaves will hear any message from the master - so how do let each slave know when a message is aimed for slave 1 or for slave 2?

    Using multiple devices on the same serial channel is like having multiple threads sharing a memory resource - you need some form of "critical section" that serializes the access to the shared resource, i.e. only allows one at a time to make use of the UART transmit.

Children