PIC18F4685 to multiple SPI slave devices

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
PaulXPC
Posts: 12
Joined: Sun Jun 29, 2008 12:35 pm
Location: The Netherlands
Contact:

PIC18F4685 to multiple SPI slave devices

Post by PaulXPC »

Hi everybody,

I'm working on a project in wich I want to connect three ADC devices to a single PIC18F4685 device over SPI. To make things clearer, I've made a quick diagram of the hardware setup.

Image

As you can see the three ADCs share the SDO, SDI and SCK lines. To select wich chip I want to 'talk' to, I connected pin 8, 9 and 10 (E-port, bit 0, 1, and 2) to the corresponding #CS pin on the ADC (selection by pulling the correct #CS pin low).
I've never used multiple SPI slave devices on a single SPI bus before, so I'm looking for some advice on the software implementation of this setup.
It's obvious that I need the SPI component and that it all depends on sharp timing of when to pull up/down the CS-lines, but I'm not really sure what's the best way to do it. Is the following method correct?
  • Setup the SPI component for clock polarity = idle high, data transmit on falling edge
  • Init SPI hardware
  • Make E0, E1 and E2 high
  • Pull E0 low
  • Send 1 byte over SPI to one of the ADCs
  • Pull E0 high
  • repeat the previous three steps for every other byte I want to send/read
I pull down E0 for every byte I send, since I assume I can't send multiple bytes in a row without pulling E0 high between every byte. At least that's what I understand from the diagram on page 20 of the datasheet (Analog AD7738: http://www.analog.com/static/imported-f ... AD7738.pdf).

Any thoughts on this are welcome (before I damage the ADC calibration by sending the wrong data 8) ). Thanks in advance.

Paul

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: PIC18F4685 to multiple SPI slave devices

Post by Benj »

Hello Paul

The easiest way to do this is to control your chip select lines via output icons using single bit operation.

You can then use the SPI component macros Init to ready the SPI peripheral, Send_Char and Get_Char to send and receive data.

Page 22 of the datasheet seems to contain a bit of overall information regarding the setup and operation of the device.

As you suggested the chip select should be lowered before sending or receiving a byte of data. If you wanted you could create user macros in Flowcode that will either take an input parameter or return a value by performing the chip select and then handling the read / write functionality.

Post Reply