中级会员
 
- 积分
- 232
- 金钱
- 232
- 注册时间
- 2016-8-9
- 在线时间
- 29 小时
|
继续做SPI的实验,Stm32F107VCT6的SPI1做主机,MISO放在那里,不接从机。结果接收数组里一直是0xff,这是为什么?
代码如下
#include "SPI1.h"
/* Private macro -------------------------------------------------------------*/
//#define SPI3_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_15) //片选引脚/CS拉低
//#define SPI3_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_15) //片选引脚/CS拉高
#define BufferSize 8
#define ReSize 50
char SPI1_Buffer_Tx[BufferSize] = {0x01,0x02,0x04,0x08,0x10,0x11,0x20,0x40};
char SPI1_Buffer_Rx[ReSize] = {0x00};
/**-----------------------------------------------------------------
* @函数名
* @功能
*
* @参数 无
* @返回值 无
***----------------------------------------------------------------*/
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能SPI1和GPIOA的RCC时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI --------------------------------*/
/*Pin5:SCK; Pin7:MOSI-------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI1初始化配置*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI通信数据的大小
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //时钟极性
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟相位
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //片选信号,软件模式
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //SPI分频值,分频后的值为SCK的时钟频率
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //选择SPI通信时,是高位数据在前还是低位数据在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //与CRC校验有关
SPI_Init(SPI1, &SPI_InitStructure);
/*使能SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
//void SPI3_WriteData(char da);
//uint16_t SPI3_ReadData(void);
void SPI1_Test(void)
{
char Tx_Idx = 0, Rx_Idx = 0;
while(Tx_Idx < BufferSize)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx]);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
SPI1_Buffer_Rx[Rx_Idx] = SPI_I2S_ReceiveData(SPI1);
Tx_Idx++;
Rx_Idx++;
}
/*拉高片选信号*/
//SPI3_CS_HIGH();
}
void SPI1_SendData(void)
{
char Tx_Idx = 0;
while(Tx_Idx < BufferSize)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Idx]);
// while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
// SPI1_Buffer_Rx[Rx_Idx] = SPI_I2S_ReceiveData(SPI1);
Tx_Idx++;
}
}
void SPI1_ReceiveData(void)
{
char Rx_Idx = 0;
// while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
// SPI_I2S_SendData(SPI1,0x11);
while(Rx_Idx < ReSize)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,0x11);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
SPI1_Buffer_Rx[Rx_Idx] = SPI_I2S_ReceiveData(SPI1);
Rx_Idx++;
}
}
我在main函数里运行,SPI1_init ,和 SPI1_ReceiveData 函数。用keil仿真,看到SPI1_Buffer_Rx中全是0xff
哪位高手,能解释下原理。大家一起探讨探讨
|
|