Sarel Rautenbach
Sarel Rautenbach

Reputation: 3

Using a PIC18F25K22 as both master and slave at the same time with SP1 and SPI2

I want to use SPI1 on a 18F series PIC as master and then use SPI2 on the same PIC as a slave to another PIC ? Can this be done ?

Upvotes: 0

Views: 269

Answers (2)

EBlake
EBlake

Reputation: 755

I use the AD7793 as well, but I've rolled my own SPI_READ and SPI_WRITE macros. These macros work on 16-bit processors, and maybe on 32-bit (I have not tried). The first parameter passed is the SPI port number (e.g. 1 or 2). They work in slave and master mode.

e.g. SPI_WRITE(1, 'H'); or SPI_READ(2, char_to_send, char_to_receive_data);

  #define  _SPI_WRITE_final(_SPIBUF, _SPIRBF, _SPIROV, ch)             \
     _SPIROV = 0;                                                      \
     while (_SPIRBF)                                                   \
        __asm__ volatile ("mov %0, w0" : : "g"(_SPIBUF) : "cc", "w0"); \
     _SPIBUF = (uint8_t)(ch);                                          \
     __asm__ volatile ("nop");                                         \
     while (!_SPIRBF);
  #define  _SPI_WRITE_int(bus, ch)                                     \
     _SPI_WRITE_final(SPI##bus##BUF, SPI##bus##STATbits.SPIRBF,        \
     SPI##bus##STATbits.SPIROV, ch)
  #define  _SPI_WRITE(bus, ch)              do {_SPI_WRITE_int(bus, ch);} while(0)

  #define  _SPI_READ_final(_SPIBUF, _SPIRBF, _SPIROV, ch_out, ch_in)   \
     _SPIROV = 0;                                                      \
     while (_SPIRBF)                                                   \
        __asm__ volatile ("mov %0, w0" : : "g"(_SPIBUF) : "cc", "w0"); \
     _SPIBUF = (uint8_t)(ch_out);                                      \
     __asm__ volatile ("nop");                                         \
     while (!_SPIRBF);                                                 \
     ch_in = _SPIBUF;
  #define  _SPI_READ_int(bus, ch_out, ch_in)                           \
     _SPI_READ_final(SPI##bus##BUF, SPI##bus##STATbits.SPIRBF,         \
     SPI##bus##STATbits.SPIROV, ch_out, ch_in)
  #define  _SPI_READ(bus, ch_out, ch_in)    do {_SPI_READ_int(bus, ch_out, ch_in);} while(0)

Upvotes: 0

Roger Rowland
Roger Rowland

Reputation: 26259

Yes it can. In fact there would be little point providing two serial modules if you were then constrained to use them in the same way.

As it says in the datasheet:

independent

Note that each operates independently from the other. Also, for each module in SPI mode:

slave

You can see that both master and slave modes are supported.

Each MSSP module has its own set of registers, which are used to configure each module as required.

Upvotes: 1

Related Questions