板子是众想的大黄蜂,以下是程序,总是检测不到NRF24L01,就大神指教这个是主程序: 现在主要是为了检测是否有NRF24L01
#include"pbdata.h" //¹«¹²º¯Êýµ÷ÓÃ
/*ÉùÃ÷º¯Êý*/
void RCC_Configura
tion(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(); //ϵͳʱÖÓ³õʼ»¯
GPIO_Configuration(); //¶Ë¿Ú³õʼ»¯
SPI2_Configuration(); //nrf2401 ½Ó¿Ú³õʼ»¯
NRF24L01_Init(); //nrf2401 ³õʼ»¯
NVIC_Configuration();
USART_Configuration();
while(NRF24L01_Check())
{
printf("OK");
delay_ms(2000);
delay_ms(2000);
delay_ms(2000);
}
}
void RCC_Configuration(void) //void ²»¿ÉÊ¡ÂÔ ³õʼ»¯ÏµÍ³Ê±ÖÓ£¬¹Ò½Ó ¶Ë¿Úµ½×ÜÏßÉÏ
{
SystemInit(); //ϵͳʱÖÓ³õʼ»¯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); //¹Ü½Å¸´ÓÃ
}
void GPIO_Configuration(void) //void ²»¿ÉÊ¡ÂÔ
{
GPIO_InitTypeDef GPIO_InitStructure;
//LED³õʼ»¯
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TXD
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»¯¶Ë¿Ú±ØÐë¸úÔڶ˿ÚÉèÖúó
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //RXD
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë
GPIO_Init(GPIOA,&GPIO_InitStructure); //³õʼ»¯¶Ë¿Ú±ØÐë¸úÔڶ˿ÚÉèÖúó
}
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}; //½ÓÊÕ»º³åÇø
u8 tx_buf[32]={0}; //·¢ËÍ»º³åÇø
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); //ʹÄÜSPI2ÍâÉèʱÖÓ
/* ÅäÖà SPI2 Òý½Å: 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; //¸´Óù¦ÄÜ£¨ÍÆÍ죩Êä³ö SPI2
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ÅäÖÃSPI2 NRF24L01+ƬѡCSN PB12 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Êä³öģʽ×î´óËÙ¶È50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ͨÓÃÍÆÍìÊä³öģʽ
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; //Êä³öģʽ×î´óËÙ¶È50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ͨÓÃÍÆÍìÊä³öģʽ
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ÅäÖÃNRF24L01+ ÖжÏÐźŲúÉúÁ¬½Óµ½ PA0 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //NRF24L01 IRQ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈëģʽ
GPIO_Init(GPIOA, &GPIO_InitStructure);
//½ûÖ¹SPI2 NRF24L01+µÄƬѡ¡£
NotSelect_NRF();
/* SPI2 ÅäÖÃ */
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; //ʱÖÓ¼«ÐÔ ¿ÕÏÐ״̬ʱ£¬SCK±£³ÖµÍµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //ʱÖÓÏàλ Êý¾Ý²ÉÑù´ÓµÚÒ»¸öʱÖÓ±ßÑØ¿ªÊ¼
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //Èí¼þ²úÉúNSS
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //²¨ÌØÂÊ¿ØÖÆ SYSCLK/16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾Ý¸ßλÔÚÇ°
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC¶àÏîʽ¼Ä´æÆ÷³õʼֵΪ7
SPI_Init(SPI2, &SPI_InitStructure);
/* ʹÄÜSPI2 */
SPI_Cmd(SPI2, ENABLE);
}
/****************************************************************************
* Ãû ³Æ£ºunsigned char SPI2_NRF_SendByte(unsigned char byte)
* ¹¦ ÄÜ£ºÍ¨¹ýSPI2 ·¢ËÍÒ»¸ö×Ö½ÚµÄÊý¾Ý¡£
* Èë¿Ú²ÎÊý£ºbyte£º ·¢Ë͵ÄÊý¾Ý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºSPI2_NRF_SendByte(data1);
****************************************************************************/
u8 SPI2_SendByte(u8 byte)
{
/* Ñ»·¼ì²â·¢ËÍ»º³åÇøÊÇ·ñÊÇ¿Õ */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* ͨ¹ýSPI2ÍâÉè·¢³öÊý¾Ý */
SPI_I2S_SendData(SPI2, byte);
/* µÈ´ý½ÓÊÕÊý¾Ý£¬Ñ»·¼ì²é½ÓÊÕÊý¾Ý»º³åÇø */
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* ·µ»Ø¶Á³öµÄÊý¾Ý */
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;
}
/****************************************************************************
* Ãû ³Æ£ºvoid MODE_CE(unsigned char a)
* ¹¦ ÄÜ£ºNRF24L01 ÊÕ/·¢Ä£Ê½ÓÐЧѡÔñ
* Èë¿Ú²ÎÊý£ºa: 1£ºNRF24L01 ÊÕ/·¢ÓÐЧ 0£º¹Ø
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºMODE_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
}
/****************************************************************************
* Ãû ³Æ£ºunsigned char SPI_RW_Reg(unsigned char data1,unsigned char data2)
* ¹¦ ÄÜ£ºÍ¨¹ýSPI2 ½«µ¥×Ö½ÚдÈëµ½NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¡£
* Èë¿Ú²ÎÊý£ºdata1£º NRF24L01¼Ä´æÆ÷
data2: µ¥×Ö½ÚÊý¾Ý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºSPI_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); //дÈëÊý¾Ý
NotSelect_NRF(); //½ûÖ¹NRF24L01Ƭѡ
return(Data); //·µ»ØNRF24L01 д¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
}
/****************************************************************************
* Ãû ³Æ£ºunsigned char SPI_Write_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* ¹¦ ÄÜ£ºÍ¨¹ýSPI2 ½«Êý×éÀïµÄÊý¾ÝдÈëµ½NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¡£
* Èë¿Ú²ÎÊý£ºreg£º NRF24L01¼Ä´æÆ÷
pBuf: Êý×é
bytes£º дÈëµÄ×Ö½ÚÊý
* ³ö¿Ú²ÎÊý£º½ÓÊÕµ½µÄ×Ö½Ú
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºSPI_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++) //дÈëÖ¸¶¨³¤¶ÈµÄÊý¾Ý
{
SPI2_SendByte(*pBuf++);
}
NotSelect_NRF(); //½ûÖ¹NRF24L01Ƭѡ
return(status); //·µ»ØNRF24L01 д¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
}
/****************************************************************************
* Ãû ³Æ£ºunsigned char SPI_Read(BYTE reg)
* ¹¦ ÄÜ£ºÍ¨¹ýSPI2 ½«NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷Àï¶Á³öÒ»¸ö×Ö½Ú¡£
* Èë¿Ú²ÎÊý£ºreg£º NRF24L01¼Ä´æÆ÷
* ³ö¿Ú²ÎÊý£ºÖ¸¶¨NRF24L01¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºstatus=SPI_Read(READ_REG1+STATUS);
****************************************************************************/
u8 SPI_Read(u8 reg)
{
u8 Data;
Select_NRF(); //Ñ¡ÔñNRF24L01Ƭѡ
SPI2_SendByte(reg); //Ö¸¶¨NRF24L01¼Ä´æÆ÷
Data=SPI2_SendByte(0); //¶Á³öÊý¾Ý
NotSelect_NRF(); //½ûÖ¹NRF24L01Ƭѡ
return (Data);
}
/****************************************************************************
* Ãû ³Æ£ºunsigned char SPI_Read_Buf(BYTE reg, BYTE *pBuf, BYTE bytes)
* ¹¦ ÄÜ£ºÍ¨¹ýSPI2 ½«NRF24L01+Ö¸¶¨µÄ¼Ä´æÆ÷ÀïµÄÊý¾Ý¶Á³öÖ¸¶¨³¤¶Èµ½Ö¸¶¨µÄÊý×éÀï¡£
* Èë¿Ú²ÎÊý£ºreg£º NRF24L01¼Ä´æÆ÷
pBuf£º Êý×é
bytes£º ³¤¶È
* ³ö¿Ú²ÎÊý£ºÖ¸¶¨NRF24L01¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
* ˵ Ã÷£º
* µ÷Ó÷½·¨£ºSPI_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); //¶Á³öÖ¸¶¨NRF24L01¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
for(i=0; i<bytes; i++) //¶Á³öÖ¸¶¨³¤¶ÈµÄÊý¾Ý
{
pBuf[i]=SPI2_SendByte(0);
}
NotSelect_NRF(); //½ûÖ¹NRF24L01Ƭѡ
return(status); //·µ»ØÖ¸¶¨NRF24L01¼Ä´æÆ÷µÄ״̬ÐÅÏ¢
}
/****************************************************************************
* Ãû ³Æ£ºRX_Mode(void)
* ¹¦ ÄÜ£ºÉèÖÃNRF24L01+µÄ½ÓÊÕģʽ
* Èë¿Ú²ÎÊý£ºÎÞ
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£ºÉèÖÃÁË6¸ö½ÓÊÕͨµÀµØÖ·£¬Êý¾Ý¿í¶È32¡¢½ÓÊÕ×Ô¶¯Ó¦´ð¡¢6¸ö½ÓÊÕͨµÀʹÄÜ¡¢
* ÉäƵƵµÀ0¡¢16λCRC¡¢ÊÕ·¢Öжϡ¢ÔöÒæ0dBµÈµÈ
* µ÷Ó÷½·¨£ºRX_Mode();
****************************************************************************/
void RX_Mode(void)
{
MODE_CE(0);
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); //Êý¾ÝͨµÀ0½ÓÊÕµØÖ·£¬×î´ó5¸ö×Ö½Ú£¬ ´Ë´¦½ÓÊÕµØÖ·ºÍ·¢Ë͵ØÖ·Ïàͬ
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P1, TX_ADDRESS1, TX_ADR_WIDTH); //Êý¾ÝͨµÀ1½ÓÊÕµØÖ·£¬×î´ó5¸ö×Ö½Ú£¬ ´Ë´¦½ÓÊÕµØÖ·ºÍ·¢Ë͵ØÖ·Ïàͬ
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P2, TX_ADDRESS2, 1); //Êý¾ÝͨµÀ2½ÓÊÕµØÖ·£¬5¸ö×Ö½Ú£¬ ¸ß×Ö½ÚÓëTX_ADDRESS1[39:8]Ïàͬ£¬µÍ×Ö½ÚͬTX_ADDRESS2[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P3, TX_ADDRESS3, 1); //Êý¾ÝͨµÀ3½ÓÊÕµØÖ·£¬5¸ö×Ö½Ú£¬ ¸ß×Ö½ÚÓëTX_ADDRESS1[39:8]Ïàͬ£¬µÍ×Ö½ÚͬTX_ADDRESS3[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P4, TX_ADDRESS4, 1); //Êý¾ÝͨµÀ4½ÓÊÕµØÖ·£¬5¸ö×Ö½Ú£¬ ¸ß×Ö½ÚÓëTX_ADDRESS1[39:8]Ïàͬ£¬µÍ×Ö½ÚͬTX_ADDRESS4[0]
SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P5, TX_ADDRESS5, 1); //Êý¾ÝͨµÀ5½ÓÊÕµØÖ·£¬5¸ö×Ö½Ú£¬ ¸ß×Ö½ÚÓëTX_ADDRESS1[39:8]Ïàͬ£¬µÍ×Ö½ÚͬTX_ADDRESS5[0]
SPI_RW_Reg(WRITE_REG1 + RX_PW_P0, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ0ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P1, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ1ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P2, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ2ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P3, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ3ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P4, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ4ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + RX_PW_P5, TX_PLOAD_WIDTH); // ½ÓÊÕÊý¾ÝͨµÀ5ÓÐЧÊý¾Ý¿í¶È32 ·¶Î§1-32
SPI_RW_Reg(WRITE_REG1 + EN_AA, 0x3f); // ʹÄÜͨµÀ0-ͨµÀ5½ÓÊÕ×Ô¶¯Ó¦´ð
SPI_RW_Reg(WRITE_REG1 + EN_RXADDR, 0x3f); // ½ÓÊÕͨµÀ0-5 ʹÄÜ
SPI_RW_Reg(WRITE_REG1 + RF_CH, 0); // Ñ¡ÔñÉäƵ¹¤×÷ƵµÀ0 ·¶Î§0-127
if(nrf_baud==0) SPI_RW_Reg(WRITE_REG1 + RF_SETUP, 0x0f); // 0db, 2M BPS ÉäƵ¼Ä´æÆ÷ ÎÞÏßËÙÂÊbit5:bit3 ·¢É书ÂÊbit2-bit1
// 00: 1M BPS 00:-18dB
// 01: 2M BPS 01:-12dB
// 10: 250K BPS 10:-6dB
// 11£º±£Áô 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 ½ÓÊÕÖжϲúÉúʱ£¬IRQÒý½Å²úÉúµÍµçƽ
// bit5 ·¢ËÍÖжϲúÉúʱ£¬IRQÒý½Å²úÉúµÍµçƽ
// bit4 ×î´óÖظ´·¢ËÍ´ÎÊýÍê³Éʱ IRQÒý½Å²úÉúµÍµçƽ
// bit3 CRCУÑéÔÊÐí
// bit2 16λCRC
// bit1 Éϵç
// bit0 ½ÓÊÕģʽ
MODE_CE(1); // ʹÄܽÓÊÕģʽ
}
/****************************************************************************
* Ãû ³Æ£ºTX_Mode(void)
* ¹¦ ÄÜ£ºÉèÖÃNRF24L01+µÄ·¢ËÍģʽ
* Èë¿Ú²ÎÊý£ºÎÞ
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£ºÉèÖÃÁË6¸ö·¢ÉäͨµÀµØÖ·¡¢ÉäƵƵµÀ0¡¢16λCRC¡¢ÊÕ·¢Öжϡ¢ÔöÒæ0dBµÈµÈ
* µ÷Ó÷½·¨£ºTX_Mode();
****************************************************************************/
void TX_Mode(void)
{
MODE_CE(0);
SPI_RW_Reg(WRITE_REG1 + SETUP_RETR, 0x1a); // ×Ô¶¯ÖØ·¢ÑÓʱ500us + 86us, ×Ô¶¯ÖØ·¢¼ÆÊý10´Î
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS0, TX_ADR_WIDTH); //Êý¾ÝͨµÀ0·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS1, TX_ADR_WIDTH); //Êý¾ÝͨµÀ1·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS2, TX_ADR_WIDTH); //Êý¾ÝͨµÀ2·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS3, TX_ADR_WIDTH); //Êý¾ÝͨµÀ3·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS4, TX_ADR_WIDTH); //Êý¾ÝͨµÀ4·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + TX_ADDR, TX_ADDRESS5, TX_ADR_WIDTH); //Êý¾ÝͨµÀ5·¢Ë͵ØÖ·£¬×î´ó5¸ö×Ö½Ú
if(nrf_Pipe==0) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS0, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 0ͨµÀµÄ·¢ÉäµØÖ·
else if(nrf_Pipe==1) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS1, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 1ͨµÀµÄ·¢ÉäµØÖ·
else if(nrf_Pipe==2) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS2, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 2ͨµÀµÄ·¢ÉäµØÖ·
else if(nrf_Pipe==3) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS3, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 3ͨµÀµÄ·¢ÉäµØÖ·
else if(nrf_Pipe==4) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS4, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 4ͨµÀµÄ·¢ÉäµØÖ·
else if(nrf_Pipe==5) SPI_Write_Buf(WRITE_REG1 + RX_ADDR_P0, TX_ADDRESS5, TX_ADR_WIDTH); // ½«0ͨµÀµÄ½ÓÊÕµØÖ·ÉèÖÃΪ 5ͨµÀµÄ·¢ÉäµØÖ·
SPI_RW_Reg(WRITE_REG1 + CONFIG, 0x0e); // bit6 ½ÓÊÕÖжϲúÉúʱ£¬IRQÒý½Å²úÉúµÍµçƽ
// bit5 ·¢ËÍÖжϲúÉúʱ£¬IRQÒý½Å²úÉúµÍµçƽ
// bit4 ×î´óÖظ´·¢ËÍ´ÎÊýÍê³Éʱ IRQÒý½Å²úÉúµÍµçƽ
// bit3 CRCУÑéÔÊÐí
// bit2 16λCRC
// bit1 Éϵç
// bit0 ·¢ËÍģʽ
MODE_CE(1); // ʹÄÜ·¢ËÍģʽ
delay_us(20);
}
/****************************************************************************
* Ãû ³Æ£ºUSB_To_NRF_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes)
* ¹¦ ÄÜ£º½«±£´æÔÚUSB½ÓÊÕ»º´æÇøµÄ32×Ö½ÚµÄÊý¾Ýͨ¹ýNRF24L01+·¢ËͳöÈ¥
* Èë¿Ú²ÎÊý£ºdata_buffer USB½ÓÊÕ»º´æÇø
Nb_bytes USB»º´æ½ÓÊÕµ½µÄ×Ö½ÚÊý
* ³ö¿Ú²ÎÊý£ºÎÞ
* ˵ Ã÷£ºµ±½ÓÊÕµ½µÄUSBÐéÄâ´®¿ÚÊý¾ÝСÓÚ32£¬°ÑÓÐЧÊý¾ÝÍâµÄ¿Õ¼äÓÃ0ÌîÂú
* µ÷Ó÷½·¨£ºRX_Mode();
****************************************************************************/
void NRF24L01_TXBUF(u8* data_buffer, u8 Nb_bytes)
{
u8 i=0;
MODE_CE(0); //NRF ģʽ¿ØÖÆ
SPI_RW_Reg(WRITE_REG1+STATUS,0xff); //ÉèÖÃ״̬¼Ä´æÆ÷³õʼ»¯
SPI_RW_Reg(0xe1,0); //Çå³ýTX FIFO¼Ä´æÆ÷
SPI_RW_Reg(0xe2,0); //Çå³ýRX FIFO¼Ä´æÆ÷
TX_Mode(); //ÉèÖÃΪ·¢ËÍģʽ
delay_ms(10);
if(Nb_bytes<32){ //µ±·¢Ë͵ÄÊý¾Ý³¤¶ÈСÓÚ32£¬°ÑÓÐЧÊý¾ÝÍâµÄ¿Õ¼äÓÃ0ÌîÂú
for(i=Nb_bytes;i<32;i++) data_buffer[i]=0;
}
SPI_Write_Buf(WR_TX_PLOAD, data_buffer, TX_PLOAD_WIDTH); //·¢ËÍ32×ֽڵĻº´æÇøÊý¾Ýµ½NRF24L01
MODE_CE(1); //±£³Ö10usÒÔÉÏ£¬½«Êý¾Ý·¢ËͳöÈ¥
}
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;
}
一周热门 更多>