View previous topic :: View next topic |
Author |
Message |
Sith Guest
|
communication between multiple 16f877 |
Posted: Fri Mar 21, 2008 10:29 am |
|
|
Hi,
I'm looking around for a way to communicate between several 16F877's. But reading through the forum I get more and more confused... Could someone direct me to a nice piece of example code, or some documentation I could go through.
The plan is to firstly set up an easy RS232 comm between two PICs, then gradually increase the number with one beeing the master and polling the others for data (addressable). If that works nice I plan to put in RS485 or maybe go wireless.
All help and suggestions are welcome. I'm not asking for a clear cut piece of code (allthough that would be nice), a kick in the good direction would be appreciated as well. |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE: |
Posted: Sat Mar 22, 2008 5:18 am |
|
|
Hi,
RS 485 is a better option compared to RS 232 for the kind of application you are making.
Wireless modules may be expensive compared to RS 485 transceivers, but you must also consider wiring costs Iin case of RS485) of the devices.
You could also consider ethernet based communication system. In this case you may have to use an 18F device that has a built-in ethernet transciever.
thanks
arunb |
|
|
Sith Guest
|
|
Posted: Sat Mar 22, 2008 7:28 am |
|
|
Hey arunb,
I totally agree that RS485 is the way to go compared to RS232, especially when bridging quite long distances. I'm still thinking or/if I would go wireless in some cases, seeing that it's going to be very hard in some cases to get wiring over there.
But nevertheless, I can't seem to find a good example where a master device is polling several (adressable) slaves. You wouldn't happen to know some good code, do you? Or anyone else for that matter?
thanks,
and see you later... |
|
|
baltazar_aquino
Joined: 16 Mar 2008 Posts: 27
|
|
Posted: Sat Mar 22, 2008 7:45 am |
|
|
Quote: | The plan is to firstly set up an easy RS232 comm between two PICs, then gradually increase the number with one beeing the master and polling the others for data (addressable). |
I'ts quite difficult to post a code without having a schematic. Your target project is more of a hardware extensive than a software one. Bear in mind though that RS232 is not efficient in data sharing - it is actually not intended for that (RS485 is). There is a danger of corrupting your data not unless you use a software UASRT for each slave leaving only one slave connected to the hardware usart. For 2 PICs, its quite straightforward. Tx-Rx and Rx-Tx. Try to make one then post your code when you encounter problems.
__baltazar__ |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
master/slave rs232 |
Posted: Sun Mar 23, 2008 2:32 am |
|
|
if each slave has his own address and have int_RDA enabled can use rs232 when all on the same wire. the addressed will answer.
with max485 (or others) you can enable/disable each one, separate wires.
if you want to go wireless, in any case you will use one rftx and one rfrx for the master, (otherwise will be too much rf and too much price) so same as rs232.
make a simple rs232 program for the communication.
send 55h,AAh,slave address; from the master. the slaves will sinchronize on 55h,AAh and read the address. the slave with address matching will answer.
best regards
joseph |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
master/slave rs232 |
Posted: Sun Mar 23, 2008 3:35 am |
|
|
simple master rutine. the slave rutine can be almost the same, RDA enabled all the time, TBE enabled on address match, disabled after sending check sum.
in the main I have made:
while(1)
{
restart_wdt();
enable_interrupts(GLOBAL);
}
wait for interrupt
have no errors in compilation. version with more words working in my applications with 16F876A (16mhz) 18F252 (32mhz)
best regards
joseph
Code: | int slave10address=10;
short int slave10fail=0;
int scomtxwords=0;//switch for tx words to send
int scomtxwchs=0;//check sum word
int scomrxwords=0;
int scomrxwchs=0;
int scomrxw0=0;
int scomrxw1=0;
int scomrxw2=0;//data from the slave
int scomrxw3=0;
int slave10data=0;
//simple master program for one slave, rs232 hardware pins.
//variables
//master tx
#int_TBE
void TBE_isr()
{
switch(scomtxwords)
{
case 0:
{
putc(85);//55h
scomtxwchs=85;
scomtxwords++;
}
break;
case 1:
{
putc(170);//AAh
scomtxwchs=255;
scomtxwords++;
}
break;
case 2:
{
putc(slave10address);
scomtxwchs=scomtxwchs+slave10address;
scomtxwords++;
}
break;
case 3:
{
putc(scomtxwchs);//sends check dum lsb
disable_interrupts(INT_TBE);
scomtxwords=0;
enable_interrupts(INT_RDA);
set_timer1(32768);//start counting scomrx time. if slave will not answer during this time, slave fail.
}
break;
}
}
//master rx
#int_RDA
void RDA_isr()
{
switch(scomrxwords)
{
case 0:
{
scomrxw0=getc();//55h
if (scomrxw0==85)//testing if the word is 55h
{
scomrxwchs=85;
scomrxwords++;
}
else
{
scomrxwords=0;
}
}
break;
case 1:
{
scomrxw1=getc();//AAh
if (scomrxw1==170)//testing if the word is AAh
{
scomrxwchs=255;
scomrxwords++;
}
else
{
scomrxwords=0;
}
}
break;
case 2:
{
scomrxw2=getc();
scomrxwchs=scomrxwchs+scomrxw2;
scomrxwords++;
}
break;
case 3:
{
scomrxw3=getc();
if (scomrxwchs==scomrxw3)//if check sum correct
{
slave10fail=0;
slave10data=scomrxw2;
}
else//check sum fail
{
slave10fail=1;
}
scomrxwords=0;
disable_interrupts(INT_RDA);
}
break;
}
} |
|
|
|
baltazar_aquino
Joined: 16 Mar 2008 Posts: 27
|
|
Posted: Sun Mar 23, 2008 6:04 am |
|
|
Quote: | if each slave has his own address and have int_RDA enabled can use rs232 when all on the same wire. the addressed will answer. |
as what I have said, tying together RS232 devices is not a common design practice otherwise, other protocols like RS 485, 422 would have not come out in the market. It may be ok for short distances but still you have to bear in mind that you don't have any efficient isolation from each slave - which means a failure of one may mean a failure of your whole system. Consider also the bus impedance as you add more slaves. Anyways, we are not setting any standard here. Just be prepared for any performance consequences as with this scheme you will not get any performance guarantee from any microchip data sheet simply because rs232 is not intended for this. |
|
|
gjs_rsdi
Joined: 06 Feb 2006 Posts: 468 Location: Bali
|
rs232 |
Posted: Sun Mar 23, 2008 6:18 am |
|
|
same as I2C multi slave. if one will hold the line, all the system fails.
rs422/485was developed to reach big distances and safe emi/rfi because the reference is not the ground, is the difference beween +tx/-tx; +rx/-rx.
also in rs422/485 many systems are up to 32 masters & slaves on the same bus (data sheet max485 etc').
just with separate lines for each slave you will get the security you want.
how you will use the system with rf tx/rx? also with rs422/485?
best regards
joseph |
|
|
baltazar_aquino
Joined: 16 Mar 2008 Posts: 27
|
|
Posted: Sun Mar 23, 2008 8:13 am |
|
|
Quote: | how you will use the system with rf tx/rx? also with rs422/485? |
With RS232, you can use the old proven multiplexing techniques:
TDM - Time division multiplexing, it means each slave is given a time slice for data exchange - similar to polling BUT - each slave has its hi-Z (tri-state) line which effectively isolates itself from the rest.
FDM - Frequency division multiplexing - each communication with a slave is on a different frequency/channel. Your bandwitdh depends on the modulation.
There are dedicated rf modules for rs232/485 that are available in electronic retial shops. In fact, I have here 2 units of 915Mhz transceiver capable of transmitting up to 500metes.Unfortunately , I cannot see any brand/name but is driven by an ATMEGA8 SMD & a tiny RF Unit. |
|
|
Sith Guest
|
|
Posted: Sun Mar 23, 2008 8:26 am |
|
|
Thanks very much for the example code. The RS232 suggestion was just to get things working, since I think it would be easier to set up then RS485. I totally agree that it's no valable option to use RS232 in a more elaborate setup.
I intend to use RS485 in RF rx/tx, or are there other/better protocols for this? I'm still looking around for suitable rf modules to hook up, maybe zigbee or such...
Anyway, I'm going to play around with the code and see where I end up... |
|
|
red32terra
Joined: 28 Sep 2008 Posts: 7
|
Re: master/slave rs232 |
Posted: Thu Nov 06, 2008 3:40 am |
|
|
gjs_rsdi wrote: | if each slave has his own address and have int_RDA enabled can use rs232 when all on the same wire. the addressed will answer.
with max485 (or others) you can enable/disable each one, separate wires.
make a simple rs232 program for the communication.
send 55h,AAh,slave address; from the master. the slaves will sinchronize on 55h,AAh and read the address. the slave with address matching will answer.
best regards
joseph |
hi, i am just asking if this can be utilized in a power line communication set up? we have a line coupling unit and fsk mod/demod, do we need to build another hardware? thanks for any reply. |
|
|
|