RyanJ
Joined: 19 Jul 2007 Posts: 6 Location: New -Brunswick
|
how to get 2 PIC 18F4431 to talk |
Posted: Fri Jul 20, 2007 7:58 am |
|
|
Hi,
Im trying to get 2 PIC18F4431 to talk to each other, im new to this so im wondering if it is easier in hardware or software mode! 1 PIC as the slave and one as the master. Does anyone have code to make them talk!
thanks
Ryan |
|
Bill Boucher
Joined: 04 Feb 2005 Posts: 34 Location: Chatham,ON,CA
|
|
Posted: Fri Jul 20, 2007 12:19 pm |
|
|
By "Talk", you could use
-parallel data bytes passed between ports
-asynchronous serial rs232-style bytes between I/O pins, either on 1 wire, 2 or even more
-SPI synchronous transfers
-something you invent
and any of these could be done in software or hardware peripherals and using interrupts or not.
That said, you have a world of options available. If this is new to you, I suggest that you start very simply such as by using #USE RS232() and 2 pins on each PIC connected to each other (thru 220 ohm resistors just in case you make a mistake, don't wanna blow pins) and try sending just a couple of bytes back and forth and get that working first. Later you can learn about interrupts and how to make transmit and receive buffers and stuff like that. For now, keep it simple.
With software or hardware serial, so long as there's no interrupts involved, you can create pretty simple exchanges. Read the CCS manual for examples on how to implement simple RS232 transmit (putc,fputc) and receive (getc,fgetc) functions and how to use kbhit() to see if data is available before calling getc() and how to use the "stream" identifier to refer to a specific RS232 channel if you have more than one defined.
After a #use_rs232() is defined, a PIC wanting to send data could do so as simply as:
or
or
Code: |
putc("Hello World");
|
The receiving PIC might do this:
or
Code: |
if (kbhit())
{
my_data = getc(); //receive 1 byte and put it into my_data;
}
|
If you want to receive more bytes, you'd have to call a getc() for each one. Say there where supposed to be 4 bytes...
Code: |
int8 my_data[4];
int8 idx;
for(idx=0;idx<4;++idx)
{
my_data[idx] = getc();
}
|
There are limitations to the above, but like I said, it's simple stuff meant to demonstrate the basic functionality. There is no limit to how much more complicated communications can get, such as implementing interrupt-driven reception and transmission, ram buffers, checksums or 16-bit CRC checksums, or ascii-encoding and decoding, structured data packets, automatic timeouts if data doesn't arrive as expected, and so on. |
|