在第一批设备中使用的是STM32F103产品,后来为了节省一点成本,打起了F030的主意,毕竟2块钱的东西,速度也可以,于是画板子,调程序,发现NRF2401就是不通讯,也是反复测试,抓波形,就是没有数据,后来在发现时钟是16位的,在回过来去网上找资料,发现SPI的发送数据默认是16位,所以找到库函数,发现有SPI_SendData8(SPI1,TxByte);这个,说明F030和103不一样,于是将发送函数改为这个,(SPI_PORT->DR = TxByte; //发送数据)括号里是103的发送,改为8位发送之后,立马通讯成功,附代码如下,有需要的拿走,已经成功
void spi_init( void )
{
GPIO_InitTypeDef SpiGpioInitStructer;
SPI_InitTypeDef SpiInitStructer;
/** SPI引脚配置 */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA , ENABLE ); //打开端口时钟
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE );
SpiGpioInitStructer.GPIO_Pin = SPI_CLK_GPIO_PIN;
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_AF;//复用模式
SpiGpioInitStructer.GPIO_OType= GPIO_OType_PP; //推挽输出
SpiGpioInitStructer.GPIO_PuPd =GPIO_PuPd_UP;; //设置为上拉模式
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_10MHz;
// SpiGpioInitStructer.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init( SPI_CLK_GPIO_PORT, &SpiGpioInitStructer ); //初始化SCK
//SCK MOSI MISO 配置为复用
SpiGpioInitStructer.GPIO_Pin = SPI_MISO_GPIO_PIN;
GPIO_Init( SPI_MISO_GPIO_PORT, &SpiGpioInitStructer ); //初始化MISO
//
SpiGpioInitStructer.GPIO_Pin = SPI_MOSI_GPIO_PIN;
GPIO_Init( SPI_MOSI_GPIO_PORT, &SpiGpioInitStructer ); //初始化MOSI
GPIO_PinAFConfig(SPI_CLK_GPIO_PORT,GPIO_PinSource5,GPIO_AF_0);//功能复用配置
GPIO_PinAFConfig(SPI_MISO_GPIO_PORT,GPIO_PinSource6,GPIO_AF_0);
GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT,GPIO_PinSource7,GPIO_AF_0);
/** SPI配置 */
SPI_I2S_DeInit( SPI_PORT ); //复位SPI
if( SPI1 == SPI_PORT )
{
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE ); //SPI1在APB2上,打开相应SPI时钟
}
else
{
RCC_APB1PeriphClockCmd( SPI_PORT_CLK, ENABLE ); //SPI2 3在APB1上
}
SPI_Cmd( SPI_PORT, DISABLE ); //关闭SPI外设,配置前关闭
SpiInitStructer.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线全双工
SpiInitStructer.SPI_Mode = SPI_Mode_Master; //主机模式
SpiInitStructer.SPI_CPOL = SPI_CPOL_Low; //空闲状态为低电平
SpiInitStructer.SPI_CPHA = SPI_CPHA_1Edge; //第一个边沿采集数据
SpiInitStructer.SPI_DataSize = SPI_DataSize_8b; //8位数据
SpiInitStructer.SPI_NSS = SPI_NSS_Soft; //从机软件管理
SpiInitStructer.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //8分频 SPI2外设总线为36M,spi1为48M
SpiInitStructer.SPI_FirstBit = SPI_FirstBit_MSB; //最高位先发送
SpiInitStructer.SPI_CRCPolynomial = 7; //CRC多项式,默认不使用SPI自带CRC
SPI_Init( SPI_PORT, &SpiInitStructer );
SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);
//NSS配置为推挽输出
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_OUT;
SpiGpioInitStructer.GPIO_OType= GPIO_OType_PP; //推挽输出
SpiGpioInitStructer.GPIO_PuPd =GPIO_PuPd_UP;;
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_50MHz;
SpiGpioInitStructer.GPIO_Pin = SPI_NSS_GPIO_PIN;
GPIO_Init( SPI_NSS_GPIO_PORT, &SpiGpioInitStructer ); //初始化NSS
GPIO_SetBits( SPI_NSS_GPIO_PORT, SPI_NSS_GPIO_PIN ); //置高
SPI_Cmd( SPI_PORT, ENABLE );
// drv_spi_read_write_byte(0XFF);
}
uint8_t spi_read_write_byte( uint8_t TxByte )
{
uint8_t l_Data = 0;
uint16_t l_WaitTime = 0;
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_TXE ) ) //等待发送缓冲区为空
{
}
SPI_SendData8(SPI1,TxByte);
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_RXNE ) ) //等待接收缓冲区非空
{
}
l_Data = SPI_ReceiveData8(SPI1);
return l_Data; //返回
}
在第一批设备中使用的是STM32F103产品,后来为了节省一点成本,打起了F030的主意,毕竟2块钱的东西,速度也可以,于是画板子,调程序,发现NRF2401就是不通讯,也是反复测试,抓波形,就是没有数据,后来在发现时钟是16位的,在回过来去网上找资料,发现SPI的发送数据默认是16位,所以找到库函数,发现有SPI_SendData8(SPI1,TxByte);这个,说明F030和103不一样,于是将发送函数改为这个,(SPI_PORT->DR = TxByte; //发送数据)括号里是103的发送,改为8位发送之后,立马通讯成功,附代码如下,有需要的拿走,已经成功
void spi_init( void )
{
GPIO_InitTypeDef SpiGpioInitStructer;
SPI_InitTypeDef SpiInitStructer;
/** SPI引脚配置 */
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA , ENABLE ); //打开端口时钟
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE );
SpiGpioInitStructer.GPIO_Pin = SPI_CLK_GPIO_PIN;
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_AF;//复用模式
SpiGpioInitStructer.GPIO_OType= GPIO_OType_PP; //推挽输出
SpiGpioInitStructer.GPIO_PuPd =GPIO_PuPd_UP;; //设置为上拉模式
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_10MHz;
// SpiGpioInitStructer.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init( SPI_CLK_GPIO_PORT, &SpiGpioInitStructer ); //初始化SCK
//SCK MOSI MISO 配置为复用
SpiGpioInitStructer.GPIO_Pin = SPI_MISO_GPIO_PIN;
GPIO_Init( SPI_MISO_GPIO_PORT, &SpiGpioInitStructer ); //初始化MISO
//
SpiGpioInitStructer.GPIO_Pin = SPI_MOSI_GPIO_PIN;
GPIO_Init( SPI_MOSI_GPIO_PORT, &SpiGpioInitStructer ); //初始化MOSI
GPIO_PinAFConfig(SPI_CLK_GPIO_PORT,GPIO_PinSource5,GPIO_AF_0);//功能复用配置
GPIO_PinAFConfig(SPI_MISO_GPIO_PORT,GPIO_PinSource6,GPIO_AF_0);
GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT,GPIO_PinSource7,GPIO_AF_0);
/** SPI配置 */
SPI_I2S_DeInit( SPI_PORT ); //复位SPI
if( SPI1 == SPI_PORT )
{
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE ); //SPI1在APB2上,打开相应SPI时钟
}
else
{
RCC_APB1PeriphClockCmd( SPI_PORT_CLK, ENABLE ); //SPI2 3在APB1上
}
SPI_Cmd( SPI_PORT, DISABLE ); //关闭SPI外设,配置前关闭
SpiInitStructer.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //双线全双工
SpiInitStructer.SPI_Mode = SPI_Mode_Master; //主机模式
SpiInitStructer.SPI_CPOL = SPI_CPOL_Low; //空闲状态为低电平
SpiInitStructer.SPI_CPHA = SPI_CPHA_1Edge; //第一个边沿采集数据
SpiInitStructer.SPI_DataSize = SPI_DataSize_8b; //8位数据
SpiInitStructer.SPI_NSS = SPI_NSS_Soft; //从机软件管理
SpiInitStructer.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //8分频 SPI2外设总线为36M,spi1为48M
SpiInitStructer.SPI_FirstBit = SPI_FirstBit_MSB; //最高位先发送
SpiInitStructer.SPI_CRCPolynomial = 7; //CRC多项式,默认不使用SPI自带CRC
SPI_Init( SPI_PORT, &SpiInitStructer );
SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);
//NSS配置为推挽输出
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_OUT;
SpiGpioInitStructer.GPIO_OType= GPIO_OType_PP; //推挽输出
SpiGpioInitStructer.GPIO_PuPd =GPIO_PuPd_UP;;
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_50MHz;
SpiGpioInitStructer.GPIO_Pin = SPI_NSS_GPIO_PIN;
GPIO_Init( SPI_NSS_GPIO_PORT, &SpiGpioInitStructer ); //初始化NSS
GPIO_SetBits( SPI_NSS_GPIO_PORT, SPI_NSS_GPIO_PIN ); //置高
SPI_Cmd( SPI_PORT, ENABLE );
// drv_spi_read_write_byte(0XFF);
}
uint8_t spi_read_write_byte( uint8_t TxByte )
{
uint8_t l_Data = 0;
uint16_t l_WaitTime = 0;
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_TXE ) ) //等待发送缓冲区为空
{
}
SPI_SendData8(SPI1,TxByte);
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_RXNE ) ) //等待接收缓冲区非空
{
}
l_Data = SPI_ReceiveData8(SPI1);
return l_Data; //返回
}
举报