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_Direction = 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; } 求大神指点,上面的代码是否有问题? |
STM32F3 硬件I2C 驱动OLED 求助!!
STM32F303使用定时器触发双ADC常规同步采样模式
哪位大哥传下STM32F334 Nucleo的工程模板给我下
STM32F334 不能串口下载
关于STM32F334的HRTIM分辨率问题
STM32F303+LCD1602程序问题,不只为什么点不亮?
STM32F334的ADC使用问题
STM32F334 HRTIM形成移相全桥波形问题
有用过STM32F373VC的板子么 iic 例程不好使
STM32F3 ADC外部触发采样达不到7.2MSPS
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPIx_ReadWriteByte(u8 TxData)
{
u8 retry=0;
uint32_t spixbase = 0x00;
SPI_TypeDef* SPIx;
SPIx=SPI1;
spixbase = (uint32_t)SPIx;
spixbase += 0x0C;
while((SPI1->SR&1<<1)==0)//等待发送区空
{
retry++;
if(retry>200)return 0;
}
*(__IO uint8_t *) spixbase = TxData;
while((SPI1->SR&1<<0)==0) //等待接收完一个byte
{
retry++;
if(retry>200)return 0;
}
return *(__IO uint8_t *) spixbase;
}
F3 需要这样写。强制转为8位,不然就是16位发送
8位数据收发,上面的SPI配置有问题吗?特别是
SPI_RxFIFOThresholdConfig(SPI2,SPI_RxFIFOThreshold_QF);
谢谢!问题解决了!配置没有问题!4楼给的8位收发函数解决问题了,开始一直按照F1系列的收发函数除了状况!感谢各位!