论坛元老
- 积分
- 3464
- 金钱
- 3464
- 注册时间
- 2019-5-15
- 在线时间
- 258 小时
|
#include "24l01_spi.h"
//串行外设接口SPI的初始化,SPI配置成主模式
//本例程选用SPI1对NRF24L01进行读写操作,先对SPI1进行初始化
void SPIx_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPIx, ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);
//SPI1口初始化
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = SPIx_SCK;
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_10MHz;
GPIO_Init(GPIO_SPIx, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MISO;
GPIO_Init(GPIO_SPIx, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI;
GPIO_Init(GPIO_SPIx, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
GPIO_SetBits(GPIO_SPIx,SPIx_MISO| SPIx_MOSI| SPIx_SCK);
/* SPI2 configuration */ //初始化SPI1结构体
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI1设置为两线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI1为主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行时钟在不操作时,时钟为低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //第一个时钟沿开始采样数据
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由软件(使用SSI位)管理
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //SPI波特率预分频值为8
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPIx, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPI2寄存器
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
/* Enable SPI2 */
SPI_Cmd(SPIx, ENABLE); //使能SPI1外设
SPIx_ReadWriteByte(0xff); //启动传输
}
u8 SPIx_ReadWriteByte(u8 TxData) //SPI读写数据函数
{
u8 retry=0;
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) //发送缓存标志位为空
{
retry++;
if(retry>200)return 0;
}
/* Send byte through the SPI1 peripheral */
SPI_SendData8(SPIx, TxData); //通过外设SPI1发送一个数据
retry=0;
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) //接收缓存标志位不为空
{
retry++;
if(retry>200)return 0;
}
/* Return the byte read from the SPI bus */
return SPI_ReceiveData8(SPIx); //通过SPI1返回接收数据
}
|
|