STM32驱动nRF905模块
时间:10-02
整理:3721RD
点击:
我最近在用STM32驱动一个nRF905无线模块,但死活都读不出来数据,连时钟信号都读不出,发送端的时序没什么问题,可接收端是什么都测不出来,望大神们指点指点,能上传一份测试过的程序就感激不尽了。调了好多天了都找不到问题出在哪里!急用!非常感谢!
接收端程序如下
void SPI_RF905_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//时钟配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK ,MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽式输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TRX_CE->PA1,TXEN->PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_CS_H();
SPI_Cmd(SPI1, DISABLE); //配置模式之前必须禁止SPI
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode =SPI_Mode_Master; //SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
spi_readwriteByte(0xff);
}
//--------------------nRF905初始化函数--------------------------------\\
void nRF905_Init(void)
{
SPI_CS_H(); //置高CS
SPI_SCK_L();//置低SPI时钟
DR_H();
AM_H();
CD_H();
PWR_H();
TRX_CE_L();
TXEN_L();
}
//----------------------寄存器配置函数------------------------------------\\
void SPI_RFCofig(void)
{
SPI_CS_L();
SPI_SendByte(0x00);
SPI_SendByte(0x4C); //第0个字节开始写截止到第九个字节
SPI_SendByte(0x0C);
SPI_SendByte(0x44);
SPI_SendByte(0x04);
SPI_SendByte(0x04);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0x58); //允许8位校验,8MHz,没有外部时钟,输出频率为1MHz
SPI_CS_H();
}
//------------------nRF905接收模式配置--------------------------------\\
void set_nRF905_RX(void)
{
TXEN_L();
TRX_CE_H();
delay_10us(650); //延时650us
}
//----------------------------------------RxPacket()-----------------------------------------//
void RxPacket(void)
{
u8 i;
delay_10us(700);
TRX_CE_L();
delay_10us(700);
SPI_CS_L();
delay_10us(10);
SPI_SendByte(RRP); //写入读指令
for(i=0;i<2;i++) //读出数据给buff
{
buff[i]=SPI_ReadByte();
}
delay_10us(10);
SPI_CS_H();
delay_10us(10);
TRX_CE_H(); //允许数据读取
delay_10us(650);
TRX_CE_L(); //禁止数据读取
}
//------------------------Rx_shuju(void)--------------------\\
void Rx_shuju(void)
{
u8 i;
set_nRF905_RX();
while(CheckDR()==0);
RxPacket();
delay_10us(5);
for(i=0;i<2;i++)
{
printf("%c\n",buff[i]);
}
delay_10us(50);
}
//---------------------SPI_RF905_SendByte()--------------------//
void SPI_SendByte(u8 byte)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
i++;
if(i>100) break;
}
SPI_I2S_SendData(SPI1, byte);
}
//--------------------------------SPI_RF905_ReadByte()---------------------\\
u8 SPI_ReadByte(void)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
i++;
if(i>100) return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
//-----------------------读写程序-------------------------------------------------\\
u8 spi_readwriteByte(u8 Txdata)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
i++;
if(i>200) return 0;
}
SPI_I2S_SendData(SPI1, Txdata);
i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
i++;
if(i>200) return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
//-------------------------CheckDR()--------------------------//
u8 CheckDR(void) //检查是否有新数据传入 Data Ready
{
if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0)&&GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)&&GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1))
{
return 1;
}
else
return 0;
}
接收端程序如下
void SPI_RF905_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//时钟配置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK ,MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽式输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TRX_CE->PA1,TXEN->PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_CS_H();
SPI_Cmd(SPI1, DISABLE); //配置模式之前必须禁止SPI
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode =SPI_Mode_Master; //SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
spi_readwriteByte(0xff);
}
//--------------------nRF905初始化函数--------------------------------\\
void nRF905_Init(void)
{
SPI_CS_H(); //置高CS
SPI_SCK_L();//置低SPI时钟
DR_H();
AM_H();
CD_H();
PWR_H();
TRX_CE_L();
TXEN_L();
}
//----------------------寄存器配置函数------------------------------------\\
void SPI_RFCofig(void)
{
SPI_CS_L();
SPI_SendByte(0x00);
SPI_SendByte(0x4C); //第0个字节开始写截止到第九个字节
SPI_SendByte(0x0C);
SPI_SendByte(0x44);
SPI_SendByte(0x04);
SPI_SendByte(0x04);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0xE7);
SPI_SendByte(0x58); //允许8位校验,8MHz,没有外部时钟,输出频率为1MHz
SPI_CS_H();
}
//------------------nRF905接收模式配置--------------------------------\\
void set_nRF905_RX(void)
{
TXEN_L();
TRX_CE_H();
delay_10us(650); //延时650us
}
//----------------------------------------RxPacket()-----------------------------------------//
void RxPacket(void)
{
u8 i;
delay_10us(700);
TRX_CE_L();
delay_10us(700);
SPI_CS_L();
delay_10us(10);
SPI_SendByte(RRP); //写入读指令
for(i=0;i<2;i++) //读出数据给buff
{
buff[i]=SPI_ReadByte();
}
delay_10us(10);
SPI_CS_H();
delay_10us(10);
TRX_CE_H(); //允许数据读取
delay_10us(650);
TRX_CE_L(); //禁止数据读取
}
//------------------------Rx_shuju(void)--------------------\\
void Rx_shuju(void)
{
u8 i;
set_nRF905_RX();
while(CheckDR()==0);
RxPacket();
delay_10us(5);
for(i=0;i<2;i++)
{
printf("%c\n",buff[i]);
}
delay_10us(50);
}
//---------------------SPI_RF905_SendByte()--------------------//
void SPI_SendByte(u8 byte)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
i++;
if(i>100) break;
}
SPI_I2S_SendData(SPI1, byte);
}
//--------------------------------SPI_RF905_ReadByte()---------------------\\
u8 SPI_ReadByte(void)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
i++;
if(i>100) return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
//-----------------------读写程序-------------------------------------------------\\
u8 spi_readwriteByte(u8 Txdata)
{
u8 i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
{
i++;
if(i>200) return 0;
}
SPI_I2S_SendData(SPI1, Txdata);
i=0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
{
i++;
if(i>200) return 0;
}
return SPI_I2S_ReceiveData(SPI1);
}
//-------------------------CheckDR()--------------------------//
u8 CheckDR(void) //检查是否有新数据传入 Data Ready
{
if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0)&&GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)&&GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1))
{
return 1;
}
else
return 0;
}
那个延时函数时1us的延时
对STM不怎么了解
牛人供电电路
终端设备采用5V锂电池供电,