Search code examples
cembeddedspimicrochippic32

Microchip Starter Kits with SPI mode


I want to use microcontrollers for communicating data by SPI. So, I have chosen firstly the Microchip USB Starter Kit III module which has a PIC32MX470F512L. I tried several ways to code its SPI, but only the clock signal SCK can be seen on an oscilloscope.

Then, i tried the same code (just adjusted a few code lines to the new PIC) with the Microchip Starter Kit I which has a PIC32MX360F512L. And all run perfectly. So, i don't understand why the USB Starter Kit III doesn't work for SPI communication?

I give you the code used to test the SPI SDO & /SS.

#define _SUPPRESS_PLIB_WARNING

#include <stdio.h>
#include <stdlib.h>
#include <plib.h>
#include <p32xxxx.h>
#include <xc.h>
#include <peripheral/spi.h>

// DEVCFG2
#pragma config FPLLIDIV = DIV_2        // PLL Input Divider (12x Divider)
#pragma config FPLLMUL = MUL_20         // PLL Multiplier (24x Multiplier)
#pragma config FPLLODIV = DIV_1       // System PLL Output Clock Divider (PLL Divide by 256)

// DEVCFG1
#pragma config FNOSC = PRIPLL           // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = OFF            // Secondary Oscillator Enable (Disabled)
#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_1           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))

// DEVCFG0
#pragma config DEBUG = OFF               // Background Debugger Enable (Debugger is Enabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)


int main(void) {
  TRISGbits.TRISG6=0; //SCK2
  TRISGbits.TRISG7=1; //SDI2
  TRISGbits.TRISG8=0; //SDO2
  TRISGbits.TRISG9=0; //SS2
  OpenSPI2(SPI_MODE16_ON|SPI_SMP_ON|MASTER_ENABLE_ON|SEC_PRESCAL_5_1|PRI_PRESCAL_16_1, SPI_ENABLE);
  int data;
  PORTGbits.RG9 = 1;

  while(1)
  {
      PORTGbits.RG9 = 0;
      putcSPI2(0xaaaa);
      data=getcSPI2();
      PORTGbits.RG9 = 1;
  }
  return 0;
}

Thanks


Solution

  • Pin Mapping

    Do you do the pin mapping ? It does not appear on the code your posted.

    You need to assign the pin to the SPI Module using the PPS (peripheral pin select).

    OpenSPI is a library function, but it's also needed to do the pin mapping with the pin peripheral select (PPS)

    Point 12.3.1 http://ww1.microchip.com/downloads/en/DeviceDoc/60001120F.pdf


    Pin State (analog / digital)

    Check your pin are not in (default) analog state. If the pin also has an analog (AN) function, the default state will be analog and you cannot control that pin. You need to set the register ANSELx (or AD1PCFG) to set the pin.

    In the chip PIC32MX470F512L the pin you are using (RG6-9) also has analog function (AN):

    10 AN16/C1IND/RPG6/SCK2/PMA5/RG6
    11 AN17/C1INC/RPG7/PMA4/RG7 
    12 AN18/C2IND/RPG8/PMA3/RG8 
    14 AN19/C2INC/RPG9/PMA2/RG9 
    

    Page 7 http://ww1.microchip.com/downloads/en/DeviceDoc/60001185F.pdf

    Analog pin Section 12.2.5 http://ww1.microchip.com/downloads/en/DeviceDoc/60001120F.pdf