3
votes

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

2
Are you sure about pin configuration (alt function and so on) of PIC32MX470F512L? Maybe prescalers have different configurations on 470 while on 360 works well.LPs
@LPs : I use the default pin configuration on both PICs. I checked after your reply and i don't find any error. This is my configuration bits for SPI1 on the PIC32MX470F512L: { TRISDbits.TRISD10=0; //SCK1 TRISCbits.TRISC4=1; //SDI1 TRISDbits.TRISD0=0; //SDO1 TRISBbits.TRISB2=0; //SS1 } Regarding, prescalers and configuration of the SPI Channel, in the datasheet of the PIC32MX470F512L, it is indicated that the pic can support until 25Mbps SPI.Dey
I checked again and tried to configure pins without relying on "default configuration". Now, the SPI works fine with PIC32MX470F512L too ! I thought that in the table mentioning "PPS", it was only if i would like to custom my configuration pins. But it was necesary.Dey
@Dey: if you solved the issue, you can answer your own question below and accept the answer.Groo

2 Answers

0
votes

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

0
votes

Hi everyone and thanks for your replies !

Thanks to your help, i found out that issue. Pin configuration was necesary. Below the code i added for pin configuration.

  // Mapping SPI1 & SPI2
  SDI1Rbits.SDI1R = 0xa; // SDI1 to C4
  RPD0Rbits.RPD0R = 0x8; // SDO1 to D0
  RPB2Rbits.RPB2R = 0x7; // SS1 to B2
  SDI2Rbits.SDI2R = 0x1; // SDI2 to G7
  RPG8Rbits.RPG8R = 0x6; // SDO2 to G8
  RPG9Rbits.RPG9R = 0x6; // SS2 to G9