We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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...
Don't you think it's a good idea to spend some time reading up on all information available on Google?
You can use the UART in "normal" mode, and have the master send: "SLAVE1: Talk\n"
or
"SLAVE2: Talk\n"
To inform either of the slaves to enable the TX pin and send any message and then release the TX pin.
You can also read up on the ability to do 9-bit transfers where the UART hardware can use the extra bit to know if the master sends data or an address byte, and can use this to pick up the address and see if the following message is addressed to slave 1 or slave 2 - and then know if it's ok to enable the TX pin and send any answer back.
Never forget the "R" in R&D - you are supposed to spend some time doing research too. Looking for what possible solutions that are available.
. @Per Westermark I changed my program as show above... Here I enable the Tx pin only for transferring the value and immediately disable... Is above program according to u...? If not than edit same program as u want.....And post here
I don't see anything in your code that indicates that the master tells the slaves which of the slaves that may talk.
If both slaves hear the same command and both slaves thinks they should respond, then both slaves will still make use of the TX line at the same time.
But aren't you actually busy right now debugging your code? Aren't you using an oscilloscope and looking at the signal on the TX line? What does it look like? What did you expect it to look like?
Programming isn't about writing some text, compile, run. Then ask in a forum for what to change before compiling and running again and then back to a web forum.
I am just a beginner and actually I have very less idea about these things... I am just studying through net and apply the things which r necessary for my project if I failed I do same thing again and again till I don't come at any conclusion.... And I am still looking for the codes by which the master tells the slaves which of the slaves that may talk....
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.
Per,
Clearest answer yet. You nailed it.
(Also, I love the use of 'goto'. You just can't beat that kind of code.)
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA