| View previous topic :: View next topic |
| Author |
Message |
SeeCwriter
Joined: 18 Nov 2013 Posts: 164
|
| #use spi statements |
Posted: Tue Jun 02, 2026 11:47 am |
|
|
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
|
|
Posted: Wed Jun 03, 2026 12:16 am |
|
|
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
|
|
Posted: Wed Jun 03, 2026 11:00 am |
|
|
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
|
|
Posted: Wed Jun 03, 2026 11:05 am |
|
|
That's the whole point about stream names.
I use two spi streams set to different speeds on the same port this way. |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1415
|
|
Posted: Wed Jun 03, 2026 11:22 am |
|
|
| 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. |
|
 |
|