首先,我用的是手柄做的无线模块的检测,通过写入一个地址然后读出,来检测无线收发模块是否存在。
spi配置:
void SPI_Configuration()
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2|
RCC_APB2Periph_GPIOA|
RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15|GPIO_Pin_13; //PB13-SCK PB15-MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//PB14-MISO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;//PC13-CSN PC14-CE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PC15-IPU
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* 配置SPI1模块 初始化*/
SPI_I2S_DeInit(SPI2);
SPI_Cmd(SPI2, DISABLE); //必须先禁用,才能改变MODE
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //两线全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //CPOL=0 时钟悬空低
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CPHA=0 数据捕获第1个
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件NSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4 ; //256分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC7
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
//SPI_ReadWriteByte(0xff); //启动传输
}
nrf init
//24L01片选信号 #define CE_H() GPIO_SetBits(GPIOC, GPIO_Pin_14) #define CE_L() GPIO_ResetBits(GPIOC, GPIO_Pin_14) //SPI片选信号 #define CSN_H() GPIO_SetBits(GPIOC, GPIO_Pin_13) #define CSN_L() GPIO_ResetBits(GPIOC, GPIO_Pin_13)
void NRF24L01_Init(void)
{
CE_L(); //使能24L01
CSN_H(); //SPI片选取消
}
spi 字节收发
u8 SPI_ReadWriteByte(u8 data)
{
u8 temp;
printf(" Enter SPIRW%d\n ",data);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI2 data */
SPI_I2S_SendData(SPI2, data);
printf(" send ok ");
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* Read SPI2 received data */
temp = SPI_I2S_ReceiveData(SPI2);
printf(" Leave SPIRW%d\n ",temp);
return temp; //返回收到的数据
}
每次都会死在这一句
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
mian 函数相关部分
SPI_Configuration();
NRF24L01_Init();
//printf("aaaUART SUCCESS!!!");Delay(0x1EFF);
if(NRF24L01_Check()==0)
printf(" nrf check success");
while (1)
{}
其中NRF24L01_Check:
每次spi发送就会死机,求大神解答!!!
u8 NRF24L01_Check(void)
{
u8 buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
u8 i;
SPI2_SetSpeed(SPI_SPEED_8); //spi速度为9Mhz(24L01的最大SPI时钟为10Mhz)
NRF24L01_Write_Buf(WRITE_REG+TX_ADDR,buf,5);//写入5个字节的地址.
NRF24L01_Read_Buf(TX_ADDR,buf,5); //读出写入的地址
for(i=0;i<5;i++)if(buf[i]!=0XA5)break;
if(i!=5)return 1;//检测24L01错误
return 0; //检测到24L01
}
我要赚赏金
