PIC master slave howto


Closed Thread
Results 1 to 5 of 5
  1. #1

    Default PIC master slave howto

    Hi all

    I have a project where I have a slave PCB reading voltages and Inputs and setting some outputs and a master showing the value's on an LCD and having a Keyboard to contol the slave Outputs
    Master and slave are connected over a halfduplex RS485 line (SP485)

    I was wondering what the best way is to send variables (from ADCIN) from the slave to
    the master
    Shall i poll each ADchannel by a command or send all ADC values in one string
    what about Sending from the master to the slave a cmd to control the IO's

    a SERIN2 with WAIT wont work i guess

    Eventually a CRC ?

    anyone who has some example code ? or did something like this ?

  2. #2


    Did you find this post helpful? Yes | No

    Default PIC to PIC

    This strategy does not use RS485 but 3 wires TTL. You could send tokens via RS-485 to achieve a similar result however.

    I have 6 PICs that need to talk among themselves in two side by side machines. Because they are always less than a metre apart I am using TTL in a peer to peer network.

    The tasks each micro does are varied with very different loop times and some cannot be interrupted during critical phases. I devised the following protocol to minimise the time impact on processors in the network that were not directly involved in the present data transfer. The data is pretty sparse, no more than about 1 packet per second per device.

    I have three active wires between all PICs. These all have 270 ohm series resistors for protection in case two outputs fight at any time. There are 16F88s, 16F877As and a 18F4550 in the micro network.

    Each PIC has an address 1 to 14 as a 4 bit nibble. This nibble is repeated twice to make a byte so address 3 becomes %00110011.

    There are three lines, ATTention, ACKnowledge and DATa, used as a data bus running through the system. These all have 10k resistor pullups at each PIC and by default all devices are set to inputs so the lines float high.

    When a PIC wants to talk it pulls the ATTention line low and repeatedly sends the destination address out the DATa line. This runs until acknowledged or it times out.

    All other micros are running their normal control loops and frequently call a subroutine named CheckComms at safe points within their main loops.

    CheckComms essentially does the following:-
    a. If the ATTention line is high the subroutine takes no action and immediately returns.
    b. If the ATTention line is low, the subroutine grabs the destination address from the DATa line and if the address does not match its own ID, it sets a "NotForMe' flag and returns. The NotForMe flag stays set until the ATTention line is eventually released. If that micro later checks the ATTention line and it is still low and the NotForMe flag is still set it immediately returns so it does not waste any time checking addresses on a data transfer between others.
    c. If ATT is low AND the unit ID matches the Destination being polled, the target micro pulls the ACKnowledge line low. The sending party now sees this line go low, stops sending destination addresses and starts repeatedly sending a Checksummed block of data. The receiving party only releases the ACK line after it has received an error free message. The sender holds the ATT line low and continuously repeats the message until it sees the ACK line go high.

    There are numerous safety checks for errors such as the destination address must be received at least four times in a row and the upper and lower nibbles must match in all of them. Addresses 00 and FF are not used as these could occur in steady mark or space on the data line. There are maximum numbers of retries and there are repeat timeouts to prevent calls to a nonexistent address from hanging the system. There is LCD indication of comms faults. Collisions cause a comms timeout and the backoff time is determined by the device ID so they are unlikely to recurr.

    The message transfer usually takes place within the minimum time but sometimes the message gets repeated a few times.

    Communications is Serin/Serout @ 9600 8N1. The message is fixed length (16 active bytes plus header and checksum). Messages shorter just use the first bytes and leave the others with whatever junk they last had in them. Some command packets are only 1 byte but response packets can be up to 8 channels of ADC data.

    This system is not foolproof and the bus could be stuck high or low by a faulty micro. The LCD panel indicates this and flashes an alarm LED on the front panel.

    I can post an early but working version of the comms routine if you like.

    HTH
    Brian

  3. #3


    Did you find this post helpful? Yes | No

    Default Wow That seems like an Awesome Protocol!

    BrianT made a great Protocol! i have a project that could use this idea!

    It depends in wheter you will use several or just one Slave, if you will use several you would have to make a Data Path Access Control (to avoid collision), this is what BrianT made with the Attention pin. If on the other hand your are using just one slave you can do so many things!! I have even done it with Serout and Serin Commands! But i will definitively recommend you using serial port and interrupts.

    A nice protocol for communication is used in one of Microchips AppNotes, the one with the Serial Bootloader for 18F and 16F devices (dont remember the number right now, easy to find on their webpage), in that protocol you can find that they try to connect a uC with a PC in both ways, it uses CRC and even an autobaud function. Pay attention to the packet they form it is simple just 4 or 5 extrabytes.

    From my point of view if you really have a "Master" the master can and should control all the communication, slave should just answer to the Masters Request, otherwise it could get tricky (or just trickier).

    The simplest form i have used before was just sending 1byte data from Master to Slave, there you can have 255 orders , but you can get in trouble if the byte gets messed up and the slave ends up receiving garbage.

    Whats i think is better is for you to give us one of your ideas, that way we can tell you pros and cons we find on it.

  4. #4


    Did you find this post helpful? Yes | No

    Default re PIC master slave howto

    Thanks for the reply

    Well as i'm only heving a remote slave and a local master i'm not using any
    bus or adressing, so a 2 wire differential interface in halfduplex is fine

    Here is what I want to do in detail

    Slave site:

    16F88 with 7 AN channels, I use 6 AN channels for voltage measuring 10bit

    Outputs: 1 AN channel (AN6) and one PortB
    Inputs: 4 input pins to read there status
    Bidirectional: 1 1wire port with 1 DS1820 temperature sensor
    RS422 with SP485 level converter using 3 PortB pins: TXD,RXD,TX or RX select

    Master Site

    RS422 with SP485 level converter using 3 PortB pins: TXD,RXD,TX or RX select
    LCD
    Keypad

    The master polls the slave
    and request 6 AN variables in 10bit, requests the status of the 4 input pins,
    request the 1wire temperatur sensor raw value (2 words) or eventually a converted value

    The keyboard allows to toggle both outputs which are "bits" (portb.x)

    So my question is how can I handle this with serin2 at the slave side with the use of "WAIT" i guess ? and if possible with a CRC added
    Can I read all variables in a row or variable by variable , and what if i want to toggle the 2 outputs ?
    Do I need to use an interupt of the buildin uart ? (SP485 is connected to the hardware RXD/TXD)

    Speed will be 9600 8N1

    Thanks

  5. #5


    Did you find this post helpful? Yes | No

    Default

    re PIC master slave howto

    --------------------------------------------------------------------------------

    Thanks for the reply

    Well as i'm only heving a remote slave and a local master i'm not using any
    bus or adressing, so a 2 wire differential interface in halfduplex is fine

    Here is what I want to do in detail

    Slave site:

    16F88 with 7 AN channels, I use 6 AN channels for voltage measuring 10bit

    Outputs: 1 AN channel (AN6) and one PortB
    Inputs: 4 input pins to read there status
    Bidirectional: 1 1wire port with 1 DS1820 temperature sensor
    RS422 with SP485 level converter using 3 PortB pins: TXD,RXD,TX or RX select

    Master Site

    RS422 with SP485 level converter using 3 PortB pins: TXD,RXD,TX or RX select
    LCD
    Keypad

    The master polls the slave
    and request 6 AN variables in 10bit, requests the status of the 4 input pins,
    request the 1wire temperatur sensor raw value (2 words) or eventually a converted value

    The keyboard allows to toggle both outputs which are "bits" (portb.x)

    So my question is how can I handle this with serin2 at the slave side with the use of "WAIT" i guess ? and if possible with a CRC added
    Can I read all variables in a row or variable by variable , and what if i want to toggle the 2 outputs ?
    Do I need to use an interupt of the buildin uart ? (SP485 is connected to the hardware RXD/TXD)

    Speed will be 9600 8N1

    Thanks



    Quote Originally Posted by Josuetas View Post
    BrianT made a great Protocol! i have a project that could use this idea!

    It depends in wheter you will use several or just one Slave, if you will use several you would have to make a Data Path Access Control (to avoid collision), this is what BrianT made with the Attention pin. If on the other hand your are using just one slave you can do so many things!! I have even done it with Serout and Serin Commands! But i will definitively recommend you using serial port and interrupts.

    A nice protocol for communication is used in one of Microchips AppNotes, the one with the Serial Bootloader for 18F and 16F devices (dont remember the number right now, easy to find on their webpage), in that protocol you can find that they try to connect a uC with a PC in both ways, it uses CRC and even an autobaud function. Pay attention to the packet they form it is simple just 4 or 5 extrabytes.

    From my point of view if you really have a "Master" the master can and should control all the communication, slave should just answer to the Masters Request, otherwise it could get tricky (or just trickier).

    The simplest form i have used before was just sending 1byte data from Master to Slave, there you can have 255 orders , but you can get in trouble if the byte gets messed up and the slave ends up receiving garbage.

    Whats i think is better is for you to give us one of your ideas, that way we can tell you pros and cons we find on it.

Similar Threads

  1. Reading a slave USB with a pic
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th October 2008, 12:00
  2. Master to slave 16F767
    By sachymo in forum General
    Replies: 6
    Last Post: - 3rd June 2008, 20:22
  3. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  4. Replies: 2
    Last Post: - 10th June 2005, 02:34
  5. 256 Pics Communicate to 1 Master Pic
    By mazlan in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 15th July 2004, 13:11

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts