• AVR Freaks

Hot!How to use SPI on 16F1454 ?

Author
nim65s
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/07/14 01:58:47
  • Location: Toulouse
  • Status: offline
2019/07/17 05:39:02 (permalink)
0

How to use SPI on 16F1454 ?

Edit: moved to https://www.microchip.com/forums/m1105424.aspx
 
Hi,
 
I am trying to use SPI on the 16F1454. To get started, I tried to use 2 PIC : one SPI master and one slave:
 
Linux ← USB ← Master PIC → SPI → Slave PIC → USB → Linux.
 
I adaptated a CDC serial example from MLA to get the parts "Linux ← USB ← Master PIC" and "Slave PIC → USB → Linux" work.
 
So now, I can monitor from linux the master trying to send SPI data to the slave, and the slave trying to receive those data. But the slave behavior shows that there is no reception.
 
Here is the code I used, stripped from USB related stuff:
 

 
// SPI helper functions for Master

void SPI_Master_Init()
{
  // Set SPI Mode To Master + Set SCK Rate To Fosc/64
  SSP1M0 = 0;
  SSP1M1 = 0;
  SSP1M2 = 0;
  SSP1M3 = 0;
  // Enable The Synchronous Serial Port
  SSPEN = 1;
  // Configure The Clock Polarity & Phase (SPI Mode Num. 1)
  CKP = 0;
  CKE = 0;
  // Configure The Sampling Time (Let's make it at middle)
  SMP = 0;
  // Configure The IO Pins For SPI Master Mode
  TRISC0 = 0; // SCK -> Output
  TRISC1 = 1; // SDI -> Input
  TRISC2 = 0; // SDO -> Output
  TRISC3 = 0; // SS -> Output
}

void SPI_Write(uint8_t Data)
{
  SSP1IF = 0;
  WCOL = 0;
  SSPBUF = Data;
  while(!BF);
}


// SPI helper functions for Slave

void SPI_Slave_Init()
{
  // Set Spi Mode To Slave + SS Enabled
  SSP1M0 = 0;
  SSP1M1 = 0;
  SSP1M2 = 1;
  SSP1M3 = 0;
  // Enable The Synchronous Serial Port
  SSPEN = 1;
  // Configure The Clock Polarity & Phase (SPI Mode Num. 1)
  CKP = 0;
  CKE = 0;
  // Clear The SMP Bit
  SMP = 0;
  // Configure The IO Pins For SPI Master Mode
  TRISC0 = 1; // SCK -> Intput
  TRISC1 = 1; // SDI -> Input
  TRISC2 = 0; // SDO -> Output
  TRISC3 = 1; // SS -> Input
}

uint8_t SPI_Read()
{
  uint8_t Data = 42;
  if(BF) // Check If Any New Data Is Received
  {
    Data = SSPBUF; // Read The Buffer
    BF = 0; // Clear The Buffer-Filled Indicator Bit
    SSP1IF = 0; // Clear The Interrupt Flag Bit
    SSPOV = 0; // Clear The Overflow Indicator Bit
  }
  return Data;
}


// master / slave test connection

uint8_t spi_data[] = {1, 2, 0};

void master()
{
  RC3 = 0; // !SS
  __delay_ms(1);

  spi_data[0]++;
  SPI_Write(spi_data[0]);
  if (USBUSARTIsTxTrfReady()) { putUSBUSART(spi_data, 3); }

  __delay_ms(1);
  RC3 = 1; // SS
  __delay_ms(1000);
}

void slave()
{
  spi_data[1] = SPI_Read() * 2 + 1;
  if (USBUSARTIsTxTrfReady()) { putUSBUSART(spi_data, 3); }
  __delay_ms(500);
}

 
So with this code, I expect to read from the master

 
1 2 0
 
2 2 0
 
3 2 0
 

 

 
And I do (or almost: it is counting 2 by 2…), and from the slave:

 
1 3 0
 
1 5 0
 
1 7 0
 

 

 
But I only read lines with "1 85 0", where 85 == 42 * 2 + 1, so this shows that "SPI_Read()" receive nothing.
 
I got this from a few tutorials online, and I also found some pieces of similar stuff in MLA, like
 

 
 void SPIPut(uint8_t v)
{     
        uint8_t i;
        
        PIR1bits.SSP1IF = 0;
        //clear SSP1BUF
        i = SSP1BUF;

        do
        {
            SSP1CON1bits.WCOL = 0;
            SSP1BUF = v;
        } while( SSP1CON1bits.WCOL );

        while( PIR1bits.SSP1IF == 0 );

        PIR1bits.SSP1IF = 0;
}

 
But it wasn't giving me much results.
 
 
My final goal will be to have a DWM1001 module to stream geolocation data by USB. So far, it works great if I plug its SPI to a RPi, but I can't get a 2m SPI cable, and that's why I bought a few PIC16F1454. For this, the same PIC will be used for both emission and reception of SPI data.
 
Am I doing anything wrong ?
 
Also, I don't use any composant but the PICs, and I put SDO from master to SDI on slave and vice versa. SCK and SS are directly connected.
post edited by nim65s - 2019/07/18 21:54:57
#1

1 Reply Related Threads

    ric
    Super Member
    • Total Posts : 24241
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to use SPI on 16F1454 ? 2019/07/18 13:35:41 (permalink)
    +1 (1)
    Aargh, I just answered your other duplicate question at https://www.microchip.com/forums/m1105424.aspx
    Please stick to one topic, or you end up with parallel streams of answers.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5