关于STM32F103 检测 NRF24L01 的问题
时间:10-02
整理:3721RD
点击:
板子是众想的大黄蜂,以下是程序,总是检测不到NRF24L01,就大神指教这个是主程序: 现在主要是为了检测是否有NRF24L01
#include"pbdata.h" //1?12oˉêyμ÷ó?
/*éù?÷oˉêy*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
int fputc(int ch,FILE *F)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
int main(void)
{
RCC_Configuration(); //?μí3ê±?ó3?ê??ˉ
GPIO_Configuration(); //???ú3?ê??ˉ
SPI2_Configuration(); //nrf2401 ?ó?ú3?ê??ˉ
NRF24L01_Init(); //nrf2401 3?ê??ˉ
NVIC_Configuration();
USART_Configuration();
while(NRF24L01_Check())
{
printf("OK");
delay_ms(2000);
delay_ms(2000);
delay_ms(2000);
}
}
void RCC_Configuration(void) //void 2??éê??? 3?ê??ˉ?μí3ê±?ó£?1ò?ó ???úμ?×ü??é?
{
SystemInit(); //?μí3ê±?ó3?ê??ˉ72 MHZ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE); //1ü???′ó?
}
void GPIO_Configuration(void) //void 2??éê???
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED3?ê??ˉ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TXD
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ???ú±?D??ú?ú???úéè??oó
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //RXD
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //????ê?è?
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ???ú±?D??ú?ú???úéè??oó
}
void NVIC_Configuration(void) //′??úó??è??????
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
这个是NRF24L01的配置文件,顺带的有SPI2的配置
#include "pbdata.h"
u8 TX_ADDRESS0[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS1[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS2[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS3[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS4[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS5[TX_ADR_WIDTH]; // Define a static TX address
u8 rx_buf[32]={5}; //?óê??o3???
u8 tx_buf[32]={0}; //·¢?í?o3???
u8 nrf_baud,nrf_Pipe,nrf_Pipe_r,Rx_Succ;
u8 test=4;
void SPI2_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE); //ê1?üSPI2íaéèê±?ó
/* ???? SPI2 òy??: SCK, MISO and MOSI£¨PB13, PB14, PB15) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?1|?ü£¨í?íì£?ê?3? SPI2
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????SPI2 NRF24L01+????CSN PB12 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ê?3??£ê?×?′ó?ù?è50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //í¨ó?í?íìê?3??£ê?
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????NRF24L01+ ?£ê????? CE PB1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //NRF24L01 MODE-CE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ê?3??£ê?×?′ó?ù?è50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //í¨ó?í?íìê?3??£ê?
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????NRF24L01+ ?D??D?o?2úéúá??óμ? PA0 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //NRF24L01 IRQ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //é?à-ê?è??£ê?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//???1SPI2 NRF24L01+μ??????£
NotSelect_NRF();
/* SPI2 ???? */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //è???1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?÷?£ê?
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8??
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //ê±?ó??D? ???D×′ì?ê±£?SCK±£3?μíμ???
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //ê±?ó?à?? êy?Y2é?ù′óμúò???ê±?ó±????aê?
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //èí?t2úéúNSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //2¨ì??ê???? SYSCLK/16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //êy?Y?????ú?°
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?à??ê???′??÷3?ê??μ?a7
SPI_Init(SPI2, &SPI_InitStructure);
/* ê1?üSPI2 */
SPI_Cmd(SPI2, ENABLE);
}
/****************************************************************************
* ?? 3?£ounsigned char SPI2_NRF_SendByte(unsigned char byte)
* 1| ?ü£oí¨1ySPI2 ·¢?íò???×??úμ?êy?Y?£
* è??ú2?êy£obyte£o ·¢?íμ?êy?Y
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI2_NRF_SendByte(data1);
****************************************************************************/
u8 SPI2_SendByte(u8 byte)
{
/* ?-?·?ì2a·¢?í?o3???ê?·?ê??? */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* í¨1ySPI2íaéè·¢3?êy?Y */
SPI_I2S_SendData(SPI2, byte);
/* μè′y?óê?êy?Y£??-?·?ì2é?óê?êy?Y?o3??? */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* ·μ???á3?μ?êy?Y */
return SPI_I2S_ReceiveData(SPI2);
}
void NRF24L01_Init(void)
{
TX_ADDRESS0[0]=0x34; //í¨μà0 ·¢é?μ??·
TX_ADDRESS0[1]=0x43;
TX_ADDRESS0[2]=0x10;
TX_ADDRESS0[3]=0x10;
TX_ADDRESS0[4]=0x01;
TX_ADDRESS1[0]=0x01; //í¨μà1 ·¢é?μ??·
TX_ADDRESS1[1]=0xE1;
TX_ADDRESS1[2]=0xE2;
TX_ADDRESS1[3]=0xE3;
TX_ADDRESS1[4]=0x02;
TX_ADDRESS2[0]=0x02; //í¨μà2 ·¢é?μ??·
TX_ADDRESS2[1]=0xE1;
TX_ADDRESS2[2]=0xE2;
TX_ADDRESS2[3]=0xE3;
TX_ADDRESS2[4]=0x02;
TX_ADDRESS3[0]=0x03; //í¨μà3 ·¢é?μ??·
TX_ADDRESS3[1]=0xE1;
TX_ADDRESS3[2]=0xE2;
TX_ADDRESS3[3]=0xE3;
TX_ADDRESS3[4]=0x02;
TX_ADDRESS4[0]=0x04; //í¨μà4 ·¢é?μ??·
TX_ADDRESS4[1]=0xE1;
TX_ADDRESS4[2]=0xE2;
TX_ADDRESS4[3]=0xE3;
TX_ADDRESS4[4]=0x02;
TX_ADDRESS5[0]=0x05; //í¨μà5 ·¢é?μ??·
TX_ADDRESS5[1]=0xE1;
TX_ADDRESS5[2]=0xE2;
TX_ADDRESS5[3]=0xE3;
TX_ADDRESS5[4]=0x02;
}
/****************************************************************************
* ?? 3?£ovoid MODE_CE(unsigned char a)
* 1| ?ü£oNRF24L01 ê?/·¢?£ê?óDD§????
* è??ú2?êy£oa: 1£oNRF24L01 ê?/·¢óDD§ 0£o1?
* 3??ú2?êy£o?T
* ?μ ?÷£o
* μ÷ó?·?·¨£oMODE_CE(1);
****************************************************************************/
void MODE_CE(u8 a)
{ //NRF24L01 MODE-CE
if(a==1) GPIO_SetBits(GPIOB, GPIO_Pin_1); //On
else GPIO_ResetBits(GPIOB, GPIO_Pin_1); //Off
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_RW_Reg(unsigned char data1,unsigned char data2)
* 1| ?ü£oí¨1ySPI2 ??μ¥×??úD′è?μ?NRF24L01+???¨μ???′??÷à??£
* è??ú2?êy£odata1£o NRF24L01??′??÷
data2: μ¥×??úêy?Y
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f);
****************************************************************************/
u8 SPI_RW_Reg(u8 reg,u8 value)
{
unsigned int Data = 0;
Select_NRF(); //????NRF24L01????
Data=SPI2_SendByte(reg); //???¨NRF24L01??′??÷
SPI2_SendByte(value); //D′è?êy?Y
NotSelect_NRF(); //???1NRF24L01????
return(Data); //·μ??NRF24L01 D′??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* 1| ?ü£oí¨1ySPI2 ??êy×éà?μ?êy?YD′è?μ?NRF24L01+???¨μ???′??÷à??£
* è??ú2?êy£oreg£o NRF24L01??′??÷
pBuf: êy×é
bytes£o D′è?μ?×??úêy
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH);
****************************************************************************/
u8 SPI_Write_Buf(u8 reg, u8 *pBuf, u8 bytes)
{
u8 status,byte_ctr;
Select_NRF(); //????NRF24L01????
status=SPI2_SendByte(reg); //???¨NRF24L01??′??÷
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) //D′è????¨3¤?èμ?êy?Y
{
SPI2_SendByte(*pBuf++);
}
NotSelect_NRF(); //???1NRF24L01????
return(status); //·μ??NRF24L01 D′??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Read(BYTE reg)
* 1| ?ü£oí¨1ySPI2 ??NRF24L01+???¨μ???′??÷à??á3?ò???×??ú?£
* è??ú2?êy£oreg£o NRF24L01??′??÷
* 3??ú2?êy£o???¨NRF24L01??′??÷μ?×′ì?D??¢
* ?μ ?÷£o
* μ÷ó?·?·¨£ostatus=SPI_Read(READ_REG1+STATUS);
****************************************************************************/
u8 SPI_Read(u8 reg)
{
u8 Data;
Select_NRF(); //????NRF24L01????
SPI2_SendByte(reg); //???¨NRF24L01??′??÷
Data=SPI2_SendByte(0); //?á3?êy?Y
NotSelect_NRF(); //???1NRF24L01????
return (Data);
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* 1| ?ü£oí¨1ySPI2 ??NRF24L01+???¨μ???′??÷à?μ?êy?Y?á3????¨3¤?èμ????¨μ?êy×éà??£
* è??ú2?êy£oreg£o NRF24L01??′??÷
pBuf£o êy×é
bytes£o 3¤?è
* 3??ú2?êy£o???¨NRF24L01??′??÷μ?×′ì?D??¢
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
****************************************************************************/
u8 SPI_Read_Buf(u8 reg, u8 *pBuf, u8 bytes)
{
u8 status,i;
Select_NRF(); //????NRF24L01????
status=SPI2_SendByte(reg); //?á3????¨NRF24L01??′??÷μ?×′ì?D??¢
for(i=0; i<bytes; i++) //?á3????¨3¤?èμ?êy?Y
{
pBuf[i]=SPI2_SendByte(0);
}
NotSelect_NRF(); //???1NRF24L01????
return(status); //·μ?????¨NRF24L01??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£oRX_Mode(void)
* 1| ?ü£oéè??NRF24L01+μ??óê??£ê?
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
* ?μ ?÷£oéè??á?6???óê?í¨μàμ??·£?êy?Y?í?è32?¢?óê?×??ˉó|′e?¢6???óê?í¨μàê1?ü?¢
* é??μ?μμà0?¢16??CRC?¢ê?·¢?D???¢??ò?0dBμèμè
* μ÷ó?·?·¨£oRX_Mode();
****************************************************************************/
void RX_Mode(void)
{
MODE_CE(0);
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); //êy?Yí¨μà0?óê?μ??·£?×?′ó5??×??ú£? ′?′|?óê?μ??·oí·¢?íμ??·?àí?
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P1, TX_ADDRESS1, TX_ADR_WIDTH); //êy?Yí¨μà1?óê?μ??·£?×?′ó5??×??ú£? ′?′|?óê?μ??·oí·¢?íμ??·?àí?
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P2, TX_ADDRESS2, 1); //êy?Yí¨μà2?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS2[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P3, TX_ADDRESS3, 1); //êy?Yí¨μà3?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS3[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P4, TX_ADDRESS4, 1); //êy?Yí¨μà4?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS4[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P5, TX_ADDRESS5, 1); //êy?Yí¨μà5?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS5[0]
SPI_RW_Reg(WRITE_REG1 + RX_PW_P0, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà0óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P1, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà1óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P2, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà2óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P3, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà3óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P4, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà4óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P5, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà5óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f); // ê1?üí¨μà0-í¨μà5?óê?×??ˉó|′e
SPI_RW_Reg(WRITE_REG1 + EN_RXADDR, 0x3f); // ?óê?í¨μà0-5 ê1?ü
SPI_RW_Reg(WRITE_REG1 + RF_CH, 0); // ????é??μ1¤×÷?μμà0 ·??§0-127
if(nrf_baud==0) SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x0f); // 0db, 2M BPS é??μ??′??÷ ?T???ù?êbit5:bit3 ·¢é?1|?êbit2-bit1
// 00: 1M BPS 00:-18dB
// 01: 2M BPS 01:-12dB
// 10: 250K BPS 10:-6dB
// 11£o±£á? 11:0dB
else if(nrf_baud==1) SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x07); // 0db, 1M BPS
else SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x27); // 0db, 250K BPS
SPI_RW_Reg(WRITE_REG1 + CONFIG, 0x0f); // bit6 ?óê??D??2úéúê±£?IRQòy??2úéúμíμ???
// bit5 ·¢?í?D??2úéúê±£?IRQòy??2úéúμíμ???
// bit4 ×?′ó???′·¢?í′?êyíê3éê± IRQòy??2úéúμíμ???
// bit3 CRCD£?é?êDí
// bit2 16??CRC
// bit1 é?μ?
// bit0 ?óê??£ê?
MODE_CE(1); // ê1?ü?óê??£ê?
}
/****************************************************************************
* ?? 3?£oTX_Mode(void)
* 1| ?ü£oéè??NRF24L01+μ?·¢?í?£ê?
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
* ?μ ?÷£oéè??á?6??·¢é?í¨μàμ??·?¢é??μ?μμà0?¢16??CRC?¢ê?·¢?D???¢??ò?0dBμèμè
* μ÷ó?·?·¨£oTX_Mode();
****************************************************************************/
void TX_Mode(void)
{
MODE_CE(0);
SPI_RW_Reg(WRITE_REG1 + SETUP_RETR, 0x1a); // ×??ˉ??·¢?óê±500us + 86us, ×??ˉ??·¢??êy10′?
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS0, TX_ADR_WIDTH); //êy?Yí¨μà0·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS1, TX_ADR_WIDTH); //êy?Yí¨μà1·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS2, TX_ADR_WIDTH); //êy?Yí¨μà2·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS3, TX_ADR_WIDTH); //êy?Yí¨μà3·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS4, TX_ADR_WIDTH); //êy?Yí¨μà4·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS5, TX_ADR_WIDTH); //êy?Yí¨μà5·¢?íμ??·£?×?′ó5??×??ú
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 0í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS1, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 1í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS2, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 2í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS3, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 3í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS4, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 4í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS5, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 5í¨μàμ?·¢é?μ??·
SPI_RW_Reg(WRITE_REG1 + CONFIG, 0x0e); // bit6 ?óê??D??2úéúê±£?IRQòy??2úéúμíμ???
// bit5 ·¢?í?D??2úéúê±£?IRQòy??2úéúμíμ???
// bit4 ×?′ó???′·¢?í′?êyíê3éê± IRQòy??2úéúμíμ???
// bit3 CRCD£?é?êDí
// bit2 16??CRC
// bit1 é?μ?
// bit0 ·¢?í?£ê?
MODE_CE(1); // ê1?ü·¢?í?£ê?
delay_us(20);
}
/****************************************************************************
* ?? 3?£oUSB_To_NRF_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
* 1| ?ü£o??±£′??úUSB?óê??o′???μ?32×??úμ?êy?Yí¨1yNRF24L01+·¢?í3?è¥
* è??ú2?êy£odata_buffer USB?óê??o′???
Nb_bytes USB?o′??óê?μ?μ?×??úêy
* 3??ú2?êy£o?T
* ?μ ?÷£oμ±?óê?μ?μ?USBDé?a′??úêy?YD?óú32£?°?óDD§êy?Yíaμ?????ó?0ì??ú
* μ÷ó?·?·¨£oRX_Mode();
****************************************************************************/
void NRF24L01_TXBUF(u8* data_buffer, u8 Nb_bytes)
{
u8 i=0;
MODE_CE(0); //NRF ?£ê?????
SPI_RW_Reg(WRITE_REG1+STATUS,0xff); //éè??×′ì???′??÷3?ê??ˉ
SPI_RW_Reg(0xe1,0); //??3yTX FIFO??′??÷
SPI_RW_Reg(0xe2,0); //??3yRX FIFO??′??÷
TX_Mode(); //éè???a·¢?í?£ê?
delay_ms(10);
if(Nb_bytes<32){ //μ±·¢?íμ?êy?Y3¤?èD?óú32£?°?óDD§êy?Yíaμ?????ó?0ì??ú
for(i=Nb_bytes;i<32;i++) data_buffer[i]=0;
}
SPI_Write_Buf(WR_TX_PLOAD, data_buffer, TX_PLOAD_WIDTH); //·¢?í32×??úμ??o′???êy?Yμ?NRF24L01
MODE_CE(1); //±£3?10usò?é?£???êy?Y·¢?í3?è¥
}
u8 NRF24L01_Check(void)
{
u8 buf[5]={0xc2,0xc2,0xc2,0xc2,0xc2};
u8 buf1[5];
u8 i;
SPI_Write_Buf(WRITE_REG1 + TX_ADDR,buf,5);
SPI_Read_Buf(TX_ADDR,buf1,5);
for(i=0;i<5;i++)
{
if(buf1[i]!=0xc2)
break;
}
if(i==5)
return 1;
else
return 0;
}
#include"pbdata.h" //1?12oˉêyμ÷ó?
/*éù?÷oˉêy*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
int fputc(int ch,FILE *F)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
int main(void)
{
RCC_Configuration(); //?μí3ê±?ó3?ê??ˉ
GPIO_Configuration(); //???ú3?ê??ˉ
SPI2_Configuration(); //nrf2401 ?ó?ú3?ê??ˉ
NRF24L01_Init(); //nrf2401 3?ê??ˉ
NVIC_Configuration();
USART_Configuration();
while(NRF24L01_Check())
{
printf("OK");
delay_ms(2000);
delay_ms(2000);
delay_ms(2000);
}
}
void RCC_Configuration(void) //void 2??éê??? 3?ê??ˉ?μí3ê±?ó£?1ò?ó ???úμ?×ü??é?
{
SystemInit(); //?μí3ê±?ó3?ê??ˉ72 MHZ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE); //1ü???′ó?
}
void GPIO_Configuration(void) //void 2??éê???
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED3?ê??ˉ
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TXD
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ???ú±?D??ú?ú???úéè??oó
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //RXD
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //????ê?è?
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ???ú±?D??ú?ú???úéè??oó
}
void NVIC_Configuration(void) //′??úó??è??????
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
这个是NRF24L01的配置文件,顺带的有SPI2的配置
#include "pbdata.h"
u8 TX_ADDRESS0[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS1[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS2[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS3[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS4[TX_ADR_WIDTH]; // Define a static TX address
u8 TX_ADDRESS5[TX_ADR_WIDTH]; // Define a static TX address
u8 rx_buf[32]={5}; //?óê??o3???
u8 tx_buf[32]={0}; //·¢?í?o3???
u8 nrf_baud,nrf_Pipe,nrf_Pipe_r,Rx_Succ;
u8 test=4;
void SPI2_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 ,ENABLE); //ê1?üSPI2íaéèê±?ó
/* ???? SPI2 òy??: SCK, MISO and MOSI£¨PB13, PB14, PB15) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?1|?ü£¨í?íì£?ê?3? SPI2
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????SPI2 NRF24L01+????CSN PB12 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ê?3??£ê?×?′ó?ù?è50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //í¨ó?í?íìê?3??£ê?
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????NRF24L01+ ?£ê????? CE PB1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //NRF24L01 MODE-CE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ê?3??£ê?×?′ó?ù?è50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //í¨ó?í?íìê?3??£ê?
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ????NRF24L01+ ?D??D?o?2úéúá??óμ? PA0 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //NRF24L01 IRQ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //é?à-ê?è??£ê?
GPIO_Init(GPIOA, &GPIO_InitStructure);
//???1SPI2 NRF24L01+μ??????£
NotSelect_NRF();
/* SPI2 ???? */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //è???1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //?÷?£ê?
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8??
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //ê±?ó??D? ???D×′ì?ê±£?SCK±£3?μíμ???
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //ê±?ó?à?? êy?Y2é?ù′óμúò???ê±?ó±????aê?
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //èí?t2úéúNSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //2¨ì??ê???? SYSCLK/16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //êy?Y?????ú?°
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?à??ê???′??÷3?ê??μ?a7
SPI_Init(SPI2, &SPI_InitStructure);
/* ê1?üSPI2 */
SPI_Cmd(SPI2, ENABLE);
}
/****************************************************************************
* ?? 3?£ounsigned char SPI2_NRF_SendByte(unsigned char byte)
* 1| ?ü£oí¨1ySPI2 ·¢?íò???×??úμ?êy?Y?£
* è??ú2?êy£obyte£o ·¢?íμ?êy?Y
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI2_NRF_SendByte(data1);
****************************************************************************/
u8 SPI2_SendByte(u8 byte)
{
/* ?-?·?ì2a·¢?í?o3???ê?·?ê??? */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* í¨1ySPI2íaéè·¢3?êy?Y */
SPI_I2S_SendData(SPI2, byte);
/* μè′y?óê?êy?Y£??-?·?ì2é?óê?êy?Y?o3??? */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* ·μ???á3?μ?êy?Y */
return SPI_I2S_ReceiveData(SPI2);
}
void NRF24L01_Init(void)
{
TX_ADDRESS0[0]=0x34; //í¨μà0 ·¢é?μ??·
TX_ADDRESS0[1]=0x43;
TX_ADDRESS0[2]=0x10;
TX_ADDRESS0[3]=0x10;
TX_ADDRESS0[4]=0x01;
TX_ADDRESS1[0]=0x01; //í¨μà1 ·¢é?μ??·
TX_ADDRESS1[1]=0xE1;
TX_ADDRESS1[2]=0xE2;
TX_ADDRESS1[3]=0xE3;
TX_ADDRESS1[4]=0x02;
TX_ADDRESS2[0]=0x02; //í¨μà2 ·¢é?μ??·
TX_ADDRESS2[1]=0xE1;
TX_ADDRESS2[2]=0xE2;
TX_ADDRESS2[3]=0xE3;
TX_ADDRESS2[4]=0x02;
TX_ADDRESS3[0]=0x03; //í¨μà3 ·¢é?μ??·
TX_ADDRESS3[1]=0xE1;
TX_ADDRESS3[2]=0xE2;
TX_ADDRESS3[3]=0xE3;
TX_ADDRESS3[4]=0x02;
TX_ADDRESS4[0]=0x04; //í¨μà4 ·¢é?μ??·
TX_ADDRESS4[1]=0xE1;
TX_ADDRESS4[2]=0xE2;
TX_ADDRESS4[3]=0xE3;
TX_ADDRESS4[4]=0x02;
TX_ADDRESS5[0]=0x05; //í¨μà5 ·¢é?μ??·
TX_ADDRESS5[1]=0xE1;
TX_ADDRESS5[2]=0xE2;
TX_ADDRESS5[3]=0xE3;
TX_ADDRESS5[4]=0x02;
}
/****************************************************************************
* ?? 3?£ovoid MODE_CE(unsigned char a)
* 1| ?ü£oNRF24L01 ê?/·¢?£ê?óDD§????
* è??ú2?êy£oa: 1£oNRF24L01 ê?/·¢óDD§ 0£o1?
* 3??ú2?êy£o?T
* ?μ ?÷£o
* μ÷ó?·?·¨£oMODE_CE(1);
****************************************************************************/
void MODE_CE(u8 a)
{ //NRF24L01 MODE-CE
if(a==1) GPIO_SetBits(GPIOB, GPIO_Pin_1); //On
else GPIO_ResetBits(GPIOB, GPIO_Pin_1); //Off
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_RW_Reg(unsigned char data1,unsigned char data2)
* 1| ?ü£oí¨1ySPI2 ??μ¥×??úD′è?μ?NRF24L01+???¨μ???′??÷à??£
* è??ú2?êy£odata1£o NRF24L01??′??÷
data2: μ¥×??úêy?Y
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f);
****************************************************************************/
u8 SPI_RW_Reg(u8 reg,u8 value)
{
unsigned int Data = 0;
Select_NRF(); //????NRF24L01????
Data=SPI2_SendByte(reg); //???¨NRF24L01??′??÷
SPI2_SendByte(value); //D′è?êy?Y
NotSelect_NRF(); //???1NRF24L01????
return(Data); //·μ??NRF24L01 D′??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* 1| ?ü£oí¨1ySPI2 ??êy×éà?μ?êy?YD′è?μ?NRF24L01+???¨μ???′??÷à??£
* è??ú2?êy£oreg£o NRF24L01??′??÷
pBuf: êy×é
bytes£o D′è?μ?×??úêy
* 3??ú2?êy£o?óê?μ?μ?×??ú
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH);
****************************************************************************/
u8 SPI_Write_Buf(u8 reg, u8 *pBuf, u8 bytes)
{
u8 status,byte_ctr;
Select_NRF(); //????NRF24L01????
status=SPI2_SendByte(reg); //???¨NRF24L01??′??÷
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) //D′è????¨3¤?èμ?êy?Y
{
SPI2_SendByte(*pBuf++);
}
NotSelect_NRF(); //???1NRF24L01????
return(status); //·μ??NRF24L01 D′??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Read(BYTE reg)
* 1| ?ü£oí¨1ySPI2 ??NRF24L01+???¨μ???′??÷à??á3?ò???×??ú?£
* è??ú2?êy£oreg£o NRF24L01??′??÷
* 3??ú2?êy£o???¨NRF24L01??′??÷μ?×′ì?D??¢
* ?μ ?÷£o
* μ÷ó?·?·¨£ostatus=SPI_Read(READ_REG1+STATUS);
****************************************************************************/
u8 SPI_Read(u8 reg)
{
u8 Data;
Select_NRF(); //????NRF24L01????
SPI2_SendByte(reg); //???¨NRF24L01??′??÷
Data=SPI2_SendByte(0); //?á3?êy?Y
NotSelect_NRF(); //???1NRF24L01????
return (Data);
}
/****************************************************************************
* ?? 3?£ounsigned char SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* 1| ?ü£oí¨1ySPI2 ??NRF24L01+???¨μ???′??÷à?μ?êy?Y?á3????¨3¤?èμ????¨μ?êy×éà??£
* è??ú2?êy£oreg£o NRF24L01??′??÷
pBuf£o êy×é
bytes£o 3¤?è
* 3??ú2?êy£o???¨NRF24L01??′??÷μ?×′ì?D??¢
* ?μ ?÷£o
* μ÷ó?·?·¨£oSPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
****************************************************************************/
u8 SPI_Read_Buf(u8 reg, u8 *pBuf, u8 bytes)
{
u8 status,i;
Select_NRF(); //????NRF24L01????
status=SPI2_SendByte(reg); //?á3????¨NRF24L01??′??÷μ?×′ì?D??¢
for(i=0; i<bytes; i++) //?á3????¨3¤?èμ?êy?Y
{
pBuf[i]=SPI2_SendByte(0);
}
NotSelect_NRF(); //???1NRF24L01????
return(status); //·μ?????¨NRF24L01??′??÷μ?×′ì?D??¢
}
/****************************************************************************
* ?? 3?£oRX_Mode(void)
* 1| ?ü£oéè??NRF24L01+μ??óê??£ê?
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
* ?μ ?÷£oéè??á?6???óê?í¨μàμ??·£?êy?Y?í?è32?¢?óê?×??ˉó|′e?¢6???óê?í¨μàê1?ü?¢
* é??μ?μμà0?¢16??CRC?¢ê?·¢?D???¢??ò?0dBμèμè
* μ÷ó?·?·¨£oRX_Mode();
****************************************************************************/
void RX_Mode(void)
{
MODE_CE(0);
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); //êy?Yí¨μà0?óê?μ??·£?×?′ó5??×??ú£? ′?′|?óê?μ??·oí·¢?íμ??·?àí?
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P1, TX_ADDRESS1, TX_ADR_WIDTH); //êy?Yí¨μà1?óê?μ??·£?×?′ó5??×??ú£? ′?′|?óê?μ??·oí·¢?íμ??·?àí?
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P2, TX_ADDRESS2, 1); //êy?Yí¨μà2?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS2[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P3, TX_ADDRESS3, 1); //êy?Yí¨μà3?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS3[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P4, TX_ADDRESS4, 1); //êy?Yí¨μà4?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS4[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P5, TX_ADDRESS5, 1); //êy?Yí¨μà5?óê?μ??·£?5??×??ú£? ??×??úó?TX_ADDRESS1[39:8]?àí?£?μí×??úí?TX_ADDRESS5[0]
SPI_RW_Reg(WRITE_REG1 + RX_PW_P0, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà0óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P1, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà1óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P2, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà2óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P3, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà3óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P4, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà4óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P5, TX_PLOAD_WIDTH); // ?óê?êy?Yí¨μà5óDD§êy?Y?í?è32 ·??§1-32
SPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f); // ê1?üí¨μà0-í¨μà5?óê?×??ˉó|′e
SPI_RW_Reg(WRITE_REG1 + EN_RXADDR, 0x3f); // ?óê?í¨μà0-5 ê1?ü
SPI_RW_Reg(WRITE_REG1 + RF_CH, 0); // ????é??μ1¤×÷?μμà0 ·??§0-127
if(nrf_baud==0) SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x0f); // 0db, 2M BPS é??μ??′??÷ ?T???ù?êbit5:bit3 ·¢é?1|?êbit2-bit1
// 00: 1M BPS 00:-18dB
// 01: 2M BPS 01:-12dB
// 10: 250K BPS 10:-6dB
// 11£o±£á? 11:0dB
else if(nrf_baud==1) SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x07); // 0db, 1M BPS
else SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x27); // 0db, 250K BPS
SPI_RW_Reg(WRITE_REG1 + CONFIG, 0x0f); // bit6 ?óê??D??2úéúê±£?IRQòy??2úéúμíμ???
// bit5 ·¢?í?D??2úéúê±£?IRQòy??2úéúμíμ???
// bit4 ×?′ó???′·¢?í′?êyíê3éê± IRQòy??2úéúμíμ???
// bit3 CRCD£?é?êDí
// bit2 16??CRC
// bit1 é?μ?
// bit0 ?óê??£ê?
MODE_CE(1); // ê1?ü?óê??£ê?
}
/****************************************************************************
* ?? 3?£oTX_Mode(void)
* 1| ?ü£oéè??NRF24L01+μ?·¢?í?£ê?
* è??ú2?êy£o?T
* 3??ú2?êy£o?T
* ?μ ?÷£oéè??á?6??·¢é?í¨μàμ??·?¢é??μ?μμà0?¢16??CRC?¢ê?·¢?D???¢??ò?0dBμèμè
* μ÷ó?·?·¨£oTX_Mode();
****************************************************************************/
void TX_Mode(void)
{
MODE_CE(0);
SPI_RW_Reg(WRITE_REG1 + SETUP_RETR, 0x1a); // ×??ˉ??·¢?óê±500us + 86us, ×??ˉ??·¢??êy10′?
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS0, TX_ADR_WIDTH); //êy?Yí¨μà0·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS1, TX_ADR_WIDTH); //êy?Yí¨μà1·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS2, TX_ADR_WIDTH); //êy?Yí¨μà2·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS3, TX_ADR_WIDTH); //êy?Yí¨μà3·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS4, TX_ADR_WIDTH); //êy?Yí¨μà4·¢?íμ??·£?×?′ó5??×??ú
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS5, TX_ADR_WIDTH); //êy?Yí¨μà5·¢?íμ??·£?×?′ó5??×??ú
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 0í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS1, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 1í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS2, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 2í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS3, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 3í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS4, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 4í¨μàμ?·¢é?μ??·
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS5, TX_ADR_WIDTH); // ??0í¨μàμ??óê?μ??·éè???a 5í¨μàμ?·¢é?μ??·
SPI_RW_Reg(WRITE_REG1 + CONFIG, 0x0e); // bit6 ?óê??D??2úéúê±£?IRQòy??2úéúμíμ???
// bit5 ·¢?í?D??2úéúê±£?IRQòy??2úéúμíμ???
// bit4 ×?′ó???′·¢?í′?êyíê3éê± IRQòy??2úéúμíμ???
// bit3 CRCD£?é?êDí
// bit2 16??CRC
// bit1 é?μ?
// bit0 ·¢?í?£ê?
MODE_CE(1); // ê1?ü·¢?í?£ê?
delay_us(20);
}
/****************************************************************************
* ?? 3?£oUSB_To_NRF_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
* 1| ?ü£o??±£′??úUSB?óê??o′???μ?32×??úμ?êy?Yí¨1yNRF24L01+·¢?í3?è¥
* è??ú2?êy£odata_buffer USB?óê??o′???
Nb_bytes USB?o′??óê?μ?μ?×??úêy
* 3??ú2?êy£o?T
* ?μ ?÷£oμ±?óê?μ?μ?USBDé?a′??úêy?YD?óú32£?°?óDD§êy?Yíaμ?????ó?0ì??ú
* μ÷ó?·?·¨£oRX_Mode();
****************************************************************************/
void NRF24L01_TXBUF(u8* data_buffer, u8 Nb_bytes)
{
u8 i=0;
MODE_CE(0); //NRF ?£ê?????
SPI_RW_Reg(WRITE_REG1+STATUS,0xff); //éè??×′ì???′??÷3?ê??ˉ
SPI_RW_Reg(0xe1,0); //??3yTX FIFO??′??÷
SPI_RW_Reg(0xe2,0); //??3yRX FIFO??′??÷
TX_Mode(); //éè???a·¢?í?£ê?
delay_ms(10);
if(Nb_bytes<32){ //μ±·¢?íμ?êy?Y3¤?èD?óú32£?°?óDD§êy?Yíaμ?????ó?0ì??ú
for(i=Nb_bytes;i<32;i++) data_buffer[i]=0;
}
SPI_Write_Buf(WR_TX_PLOAD, data_buffer, TX_PLOAD_WIDTH); //·¢?í32×??úμ??o′???êy?Yμ?NRF24L01
MODE_CE(1); //±£3?10usò?é?£???êy?Y·¢?í3?è¥
}
u8 NRF24L01_Check(void)
{
u8 buf[5]={0xc2,0xc2,0xc2,0xc2,0xc2};
u8 buf1[5];
u8 i;
SPI_Write_Buf(WRITE_REG1 + TX_ADDR,buf,5);
SPI_Read_Buf(TX_ADDR,buf1,5);
for(i=0;i<5;i++)
{
if(buf1[i]!=0xc2)
break;
}
if(i==5)
return 1;
else
return 0;
}
程序看着其实没有多大问题
你试试把主循环部分这样写
while(1)
{
if(1== NRF24L01_Check())
{
printf("OK");
}
else
{
printf("ERROR");
}
}
如果还是不行的话我建议你查查硬件的接线问题。
程序看着其实没有多大问题
你试试把主循环部分这样写
while(1)
{
if(1== NRF24L01_Check())
{
printf("OK");
}
else
{
printf("ERROR");
}
}
如果还是不行的话我建议你查查硬件的接线问题。