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

#use spi statements

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



Joined: 18 Nov 2013
Posts: 164

View user's profile Send private message

#use spi statements
PostPosted: Tue Jun 02, 2026 11:47 am     Reply with quote

I have multiple SPI devices in my design. Can I configure the spi bus using #use spi, then when communicating with each device, have another #use spi statement that just sets the ENABLE pin to use before comm with each device?
For example:
Code:

// myheader.h
#use spi( MASTER, SPI2, MODE=0, BITS=8, STREAM=SPI_1)

// adc.c

 unsigned int8 ReadADC()
{
    #use spi(ENABLE=PIN_D0)
    unsigned int8 rData[2];
    spi_transfer_read(SPI_1, rData, 2);
    ...
}

// dac.c

void WriteDAC(unsigned int8 *wData, int8 NumBytes)
{
    #use spi(ENABLE=PIN_D1)
    spi_transfer_write(SPI_1, wData, NumBytes);
}

[/code]
Ttelmah



Joined: 11 Mar 2010
Posts: 20084

View user's profile Send private message

PostPosted: Wed Jun 03, 2026 12:16 am     Reply with quote

What you do is setup the same SPI, with two streams, and the different
enable settings:
Code:

#use spi( MASTER, SPI2, MODE=0, BITS=8,ENABLE=PIN_D0,STREAM=SPI_1D0)
#use spi( MASTER, SPI2, MODE=0, BITS=8,ENABLE=PIN_D1,STREAM=SPI_1D1)

Then use the stream names in the different transfers.

You can use different modes, and other options this way.
SeeCwriter



Joined: 18 Nov 2013
Posts: 164

View user's profile Send private message

PostPosted: Wed Jun 03, 2026 11:00 am     Reply with quote

That's even better. I thought the most recent #use statement was the one that was used.

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 20084

View user's profile Send private message

PostPosted: Wed Jun 03, 2026 11:05 am     Reply with quote

That's the whole point about stream names.
Very Happy

I use two spi streams set to different speeds on the same port this way.
jeremiah



Joined: 20 Jul 2010
Posts: 1415

View user's profile Send private message

PostPosted: Wed Jun 03, 2026 11:22 am     Reply with quote

SeeCwriter wrote:
That's even better. I thought the most recent #use statement was the one that was used.

Thanks.


It does for any initialization code. But for any code generated after initialization, the stream name will tailor the generated code.
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