CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to use both HW SPI & SW SPI at the same time?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
zephyrtr



Joined: 07 May 2010
Posts: 3

View user's profile Send private message

How to use both HW SPI & SW SPI at the same time?
PostPosted: Fri May 07, 2010 1:08 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 07, 2010 1:13 pm     Reply with quote

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.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri May 07, 2010 5:40 pm     Reply with quote

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
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Fri May 07, 2010 8:08 pm     Reply with quote

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
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri May 07, 2010 8:20 pm     Reply with quote

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
zephyrtr



Joined: 07 May 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon May 10, 2010 9:31 am     Reply with quote

How can I write

setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16)

with #use spi directive?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 10, 2010 11:19 am     Reply with quote

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.
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Tue May 11, 2010 6:32 am     Reply with quote

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
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed May 12, 2010 12:41 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group