SPI配置如下:
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_5);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_5);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_5);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_5);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
SPI_I2S_DeInit(SPI2);
SPI_InitStructure.SPI_Direc
tion = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_RxFIFOThresholdConfig(SPI2,SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI2,ENABLE); //Enable SPI2
收发数据如下:
uint8_t SPI2ExchangeData(uint8_t data)
{
while((SPI2->SR & SPI_I2S_FLAG_TXE) == RESET);
SPI2->DR = data; //此处测量SCK,发现有16个时钟
// SPI_SendData8(SPI2,data);
while((SPI2->SR & SPI_I2S_FLAG_RXNE) == RESET);
return (SPI2->DR);
// return SPI_ReceiveData8(SPI2);
}
SPI读取字代码:
uint16_t SPIReadWord(uint16_t *Ressult)
{
char dataRx1,dataRx2;
dataRx1 = SPI2ExchangeData(0xFF);
dataRx2 = SPI2ExchangeData(0xFF);
*Ressult = (uint16_t) (dataRx2 + (dataRx1 << 8));
return *Ressult;
}
求大神指点,上面的代码是否有问题?
谢谢!问题解决了!配置没有问题!4楼给的8位收发函数解决问题了,开始一直按照F1系列的收发函数除了状况!感谢各位!
一周热门 更多>