我想使用微控制器通过 SPI 进行数据通信。因此,我首先选择了具有 PIC32MX470F512L 的 Microchip USB Starter Kit III 模块。我尝试了几种方法对其 SPI 进行编码,但在示波器上只能看到时钟信号 SCK。
然后,我使用具有 PIC32MX360F512L 的 Microchip Starter Kit I 尝试了相同的代码(只是调整了几行代码到新的 PIC)。一切运行完美。所以,我不明白为什么 USB Starter Kit III 不能用于 SPI 通信?
我给你用于测试 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;
}
谢谢