View previous topic :: View next topic |
Author |
Message |
zephyrtr
Joined: 07 May 2010 Posts: 3
|
How to use both HW SPI & SW SPI at the same time? |
Posted: Fri May 07, 2010 1:08 pm |
|
|
I tried to connect two SPI devices(digital compass and optical mouse) to PIC16f877. I reserved the HW pins for digital compass and want to use SW SPI for the optical mouse. I had a confusion with the SPI functions in CCS. There is setup_spi() function and #use spi directive. Can I use both of them in the same program, one for HW one for SW? ![Question](images/smiles/icon_question.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 07, 2010 1:13 pm |
|
|
I think it should work. Use setup_spi(), spi_write() and spi_read()
for the hardware SPI.
For the software SPI, invoke #use spi() with some other i/o pins.
Then use spi_xfer() to do the transfers.
You didn't give your compiler version. In some earlier versions, it's
possible that #use spi() is a little buggy. Here's an example which
uses hardware SPI, and software SPI, but the software SPI is done
with code, rather than the #use spi() library:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
You can do that if necessary. |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Fri May 07, 2010 5:40 pm |
|
|
PCM,
I've done a mix of #use SPI for hardware and software and then just name my streams..
I will concur that success is version sensitive and should be taken with a grain of salt.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Fri May 07, 2010 8:08 pm |
|
|
The usual thing (to me anyway) would be to use just one SPI bus (typically the hardware one) and then separate CS lines to each device. I presume there must be a reason for wanting separate SPI buses? _________________ Andrew |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Fri May 07, 2010 8:20 pm |
|
|
Only in situations where you might want both buses to run at the same time, but if they don't need to, a single bus with mulitple nCS lines is just dandy too.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
zephyrtr
Joined: 07 May 2010 Posts: 3
|
|
Posted: Mon May 10, 2010 9:31 am |
|
|
How can I write
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16)
with #use spi directive? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 10, 2010 11:19 am |
|
|
I think this will work:
Code: |
#use spi(MASTER, SPI1, BITS=8, MODE=1, MSB_FIRST, BAUD=1000000, ENABLE=PIN_C0)
|
This uses the hardware SPI module as an SPI master.
Note that I've picked Pin C0 as the SPI chip select pin, going from the
PIC (the master) to the slave.
I'm also assuming that your PIC runs at 20 MHz, and that with your
divide-by-16 divisor, you want an SPI clock of about 1 MHz. |
|
![](templates/subSilver/images/spacer.gif) |
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Tue May 11, 2010 6:32 am |
|
|
When I've used #use spi, I've not specified the enable pin in the directive, but managed it myself. Otherwise, spi_xfer() becomes complicated to use with multi-byte transfers. _________________ Andrew |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Wed May 12, 2010 12:41 pm |
|
|
andrewg wrote: | When I've used #use spi, I've not specified the enable pin in the directive, but managed it myself. Otherwise, spi_xfer() becomes complicated to use with multi-byte transfers. |
I've done multi-byte regularly without any real issues (except when PCD came out and still had some bugs which I've seen are not gone)
Depends on if the ENABLE is specified to control the nCS pin.
If I'm doing multiple devices or multiple bytes, I control the nCS pin and off I go.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
|