PIC32错误的SPI MOSI
我正在尝试开发一个接口SPI,我从一个简单的配置开始。 问题是SCK似乎工作得很好,但MOSI不起作用。 这是我的代码和测试逻辑测试仪PIC32错误的SPI MOSI,c,embedded,spi,pic32,C,Embedded,Spi,Pic32,我正在尝试开发一个接口SPI,我从一个简单的配置开始。 问题是SCK似乎工作得很好,但MOSI不起作用。 这是我的代码和测试逻辑测试仪 #include <stdlib.h> #include <plib.h> // example functions prototypes int SpiDoMasterSlaveExample(int nCycles); void SpiInitDevice(int chn, int isMa
#include <stdlib.h>
#include <plib.h>
// example functions prototypes
int SpiDoMasterSlaveExample(int nCycles);
void SpiInitDevice(int chn, int isMaster, int frmEn, int frmMaster);
// some definitions
#define MIN_SPI_TXFER_SIZE 8 // min number of words per transfer
#define MAX_SPI_TXFER_SIZE 512 // max number of words per transfer
// configuration settings
#pragma config FNOSC = PRIPLL, POSCMOD = HS, FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPBDIV = DIV_2, FPLLODIV = DIV_1
#pragma config FWDTEN = OFF
int main(void)
{
SYSTEMConfigPerformance(72000000L);
srand(ReadCoreTimer()); // seed the pseudo random generator
if(!SpiDoMasterSlaveExample(100))
{
return 0; // our example failed
}
return 1;
}
int SpiDoMasterSlaveExample(int nCycles)
{
int fail=0; // overall result
SpiInitDevice(1, 1, 1, 1); // initialize the SPI channel 1 as master, frame master
SpiInitDevice(2, 0, 1, 0); // initialize the SPI channel 2 as slave, frame slave
while(nCycles-- && !fail)
{
unsigned int txferSize;
unsigned short* pTxBuff;
unsigned short* pRxBuff;
txferSize=MIN_SPI_TXFER_SIZE+rand()%(MAX_SPI_TXFER_SIZE-MIN_SPI_TXFER_SIZE+1); // get a random transfer size
pTxBuff=(unsigned short*)malloc(txferSize*sizeof(short));
pRxBuff=(unsigned short*)malloc(txferSize*sizeof(short)); // we'll transfer 16 bits words
if(pTxBuff && pRxBuff)
{
unsigned short* pSrc=pTxBuff;
unsigned short* pDst=pRxBuff;
int ix;
int rdData;
for(ix=0; ix<txferSize; ix++)
{
pTxBuff[ix]='A'; // fill buffer with some random data
}
ix=txferSize+1; // transfer one extra word to give the slave the possibility to reply back the last sent word
while(ix--)
{
SpiChnPutC(1, *pSrc++); // send data on the master channel, SPI1
rdData=SpiChnGetC(1); // get the received data
if(ix!=txferSize)
{ // skip the first received character, it's garbage
*pDst++=rdData; // store the received data
}
rdData=SpiChnGetC(2); // receive data on the slave channel, SPI2
SpiChnPutC(2, rdData); // relay back data
}
// now let's check that the data was received ok
pSrc=pTxBuff;
pDst=pRxBuff;
for(ix=0; ix<txferSize; ix++)
{
if(*pDst++!=*pSrc++)
{
fail=1; // data mismatch
break;
}
}
}
else
{ // memory allocation failed
fail=1;
}
free(pRxBuff);
free(pTxBuff); // free the allocated buffers
}
return !fail;
}
void SpiInitDevice(int chn, int isMaster, int frmEn, int frmMaster)
{
unsigned int config=SPI_CON_MODE16|SPI_CON_SMP|SPI_CON_ON; // SPI configuration word
if(isMaster)
{
config|=SPI_CON_MSTEN;
}
if(frmEn)
{
config|=SPI_CON_FRMEN;
if(!frmMaster)
{
config|=SPI_CON_FRMSYNC;
}
}
SpiChnOpen(chn, config, 4); // divide fpb by 4, configure the I/O ports. Not using SS in this example
}
抱歉,我无法发布我的信誉点的逻辑分析器图像
我正试图发送一个全时填充缓冲传输。这就是向SPI1发送数据
我正在从我的微芯片扩展板I/O读取SPI1,其中SPI1位于引脚41和43 41 SCK和43 SDO中。
在SPI2中,引脚23和25,显然我没有任何流量
有人知道这个错误吗
非常感谢PIC32MX系列为一些外围设备(包括SPI)提供了可映射的输入和输出引脚。这意味着,MOSI和MISO可以根据您的具体需要映射到不同的引脚 在开始使用SPI之前,您需要在代码中指定这一点,否则PIC将不知道要使用哪个引脚 以下只是如何设置引脚外围引脚选择的示例。您需要在PIC的数据表中查找映射。调用中的第一个参数是数据表中的表索引
/* inputs */
PPSInput(2, SDI1, RPF2); // F2, MEMORY MISO -> SPI1SDI
PPSInput(2, SDI2, RPG7); // G7, ZB MISO -> SPI2SDI
/* outputs */
PPSOutput(4, RPF3, SDO1); // F3, MEMORY MOSI -> SPI1SDO
PPSOutput(1, RPG8, SDO2); // G8, ZB MOSI -> SPI2SDO
如前所述,检查外围设备引脚选择。另外,您必须设置端口方向以输出PDx寄存器。MISO必须设置为输入。请在发布前预览您的帖子。你的代码格式到处都是,对此我很抱歉。我在标签中插入代码。我会设法解决的。当然。您是否正在将SPI1循环到SPI2,并且希望从SPI1发送并在SPI2上接收?