⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nrf24l01bak.c

📁 我写的atmega16驱动的nrf24l01程序
💻 C
字号:
//--------------------------------------------------------
//--------------------------------------------------------
void  SPI_SendData(uchar *nrfTxData, uchar *nrfRxData, uchar No)
{
uchar i;

    _ClrNRF_SS;
    for ( i = 0; i < No; i++ )
    {
	    SPDR = nrfTxData[i];
	    while ((SPSR & 0x80) == 0);
	    nrfRxData[i] = SPDR;		                /* Flush the RxFIFO */
    }
    _SetNRF_SS;
}
//--------------------------------------------------------
//--------------------------------------------------------


//--------------------------------------------------------
//--------------------------------------------------------
void    nrf24l01_init0()
{
    IO0CLR = NRF_CE0;                       // 空闲模式,不进行数据传输

    nrfTxData[0] = 0x23;                       // SETUP_AW:设置地址宽度
    nrfTxData[1] = 0x03;                       // 5byte地址宽度
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x2A;                       // RX_ADDR_P0:接收数据通道0的地址40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0x31;                       // RX_PW_P0 :接收数据通道0的有效载荷字节数
    nrfTxData[1] = 20;                         // 32byte
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x27;                       // STATUS:状态寄存器
    nrfTxData[1] = 0x70;                       // 清空各中断
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x21;                       // EN_AA
    nrfTxData[1] = 0x01;                       // 通道0自动应答
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x22;                       // EN_RXADDR:激活接收地址
    nrfTxData[1] = 0x01;                       // 使能接收通道0
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x24;                       // SETUP_RETR
    nrfTxData[1] = 0x0a;                       // 不转发,10次重发
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x25;                       // RF_CH:射频频道
    nrfTxData[1] = 0x40;                       // 频点2
    SPI_SendData(nrfTxData, nrfRxData, 2);


    for (i=0; i<10000; i++);


    nrfTxData[0] = 0x26;                       // RF_SETUP:射频设置寄存器
    nrfTxData[1] = 0x09;                       // 2Mbps, 0 dBm,Setup LNA gain    //******1f
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x30;                       // TX_ADDR:发送数据通道地址,40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0x20;                       // CONFIG:PRx, CRC 1byte
    nrfTxData[1] = 0x0f;
    SPI_SendData(nrfTxData, nrfRxData, 2);
    
    IO0SET = NRF_CE0;                       // 进入发送或接收模式

    nrfTxData[0] = 0xe1;      
    SPI_SendData(nrfTxData, nrfRxData, 1);
    nrfTxData[0] = 0xe2;                 
    SPI_SendData(nrfTxData, nrfRxData, 1);
}
//--------------------------------------------------------
//--------------------------------------------------------


//--------------------------------------------------------
//--------------------------------------------------------
void    nrf24l01_send0(uchar *s, uchar No)
{
    uchar i;

    IO0CLR = NRF_CE0;                        // 空闲模式,不进行数据传输
 
    nrfTxData[0] = 0x20;                       // CONFIG:
    nrfTxData[1] = 0x0e;                       // PTx, CRC 1byte
    SPI_SendData(nrfTxData, nrfRxData, 2);
    
    
	nrfTxData[0] = 0xe1;                       // 清空Tx_FIFO
	SPI_SendData(nrfTxData, nrfRxData, 1);
	
	nrfTxData[0] = 0xa0;                       // 写Tx数据
	for(i=0; i<No; i++)
	{
	    nrfTxData[1+i] = 9;               // No<32byte       
	}    
	SPI_SendData(nrfTxData, nrfRxData, 1+No);   
	
	 IO0SET = NRF_CE0;                        // 进入发送模式
}
//--------------------------------------------------------
//--------------------------------------------------------


//--------------------------------------------------------
//--------------------------------------------------------
void    nrf24l01_test0(void)
{
    nrfTxData[0] = 0x17;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x00;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x01;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x02;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x03;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x04;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x05;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x06;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x07;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x0a;                       // CONFIG
    nrfTxData[1] = 0x00;                       // power down
    nrfTxData[1] = 0x00;                       // power down
    nrfTxData[1] = 0x00;                       // power down
    nrfTxData[1] = 0x00;                       // power down
    nrfTxData[1] = 0x00;                       // power down
    SPI_SendData(nrfTxData, nrfRxData, 6);
}
//--------------------------------------------------------
//--------------------------------------------------------


//--------------------------------------------------------
//--------------------------------------------------------
void    nrf24l01_init()
{
//    UINT i;

    _SetnRF_CE;                            // 空闲模式,不进行数据传输

    nrfTxData[0] = 0x21;                         // EN_AA
    nrfTxData[1] = 0x01;                       // 通道0自动应答
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x22;                       // EN_RXADDR:激活接收地址
    nrfTxData[1] = 0x01;                       // 使能接收通道0
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x23;                       // SETUP_AW:设置地址宽度
    nrfTxData[1] = 0x03;                       // 5byte地址宽度
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x24;                       // SETUP_RETR
    nrfTxData[1] = 0x0a;                       // 不转发
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x25;                       // RF_CH:射频频道
    nrfTxData[1] = 0x40;                       // 频点2
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x26;                       // RF_SETUP:射频设置寄存器
    nrfTxData[1] = 0x09;                       // 2Mbps, 0-18 dBm,Setup LNA gain
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x27;                       // STATUS:状态寄存器
    nrfTxData[1] = 0x70;                       // 清空各中断
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x2A;                       // RX_ADDR_P0:接收数据通道0的地址40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0x30;                       // TX_ADDR:发送数据通道地址,40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0x31;                       // RX_PW_P0 :接收数据通道0的有效载荷字节数
    nrfTxData[1] = 20;                         // 32byte
    SPI_SendData(nrfTxData, nrfRxData, 2);


    nrfTxData[0] = 0x20;                       // CONFIG:PRx, CRC 1byte
    nrfTxData[1] = 0x0f;
    SPI_SendData(nrfTxData, nrfRxData, 2);
    
    IO0SET = nRF_CE;                        // 进入发送或接收模式

    nrfTxData[0] = 0xe1;                       // 
    SPI_SendData(nrfTxData, nrfRxData, 1);   
    nrfTxData[0] = 0xe2;                       // 
    SPI_SendData(nrfTxData, nrfRxData, 1);
}
//--------------------------------------------------------
//--------------------------------------------------------


//--------------------------------------------------------
//--------------------------------------------------------
void    nrf24l01_send(uchar *s, uchar No)
{
    uchar i;
    IO0CLR = nRF_CE; 

    nrfTxData[0] = 0x23;                       // CONFIG:
    nrfTxData[1] = 0x03;                       // PTx, CRC 1byte
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x30;                       // TX_ADDR:发送数据通道地址,40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0x2A;                       // RX_ADDR_P0:接收数据通道0的地址40位
    nrfTxData[1] = 0x01;                       // 接收数据通道0的地址LSB
    nrfTxData[2] = 0x02;
    nrfTxData[3] = 0x03;
    nrfTxData[4] = 0x04;
    nrfTxData[5] = 0x05;                       // 接收数据通道0的地址MSB
    SPI_SendData(nrfTxData, nrfRxData, 6);

    nrfTxData[0] = 0xa0;                       // 写Tx数据
    for(i=1; i<21; i++)
    {
        nrfTxData[i] = 3;                 // No<32byte       
    }    
    SPI_SendData(nrfTxData, nrfRxData, 21);       
    
    nrfTxData[0] = 0x27;                       // STATUS:状态寄存器
    nrfTxData[1] = 0x70;                       // 清空各中断
    SPI_SendData(nrfTxData, nrfRxData, 2);    
     
    nrfTxData[0] = 0x21;                       // EN_AA
    nrfTxData[1] = 0x01;                       // 通道0自动应答
    SPI_SendData(nrfTxData, nrfRxData, 2); 
  
    nrfTxData[0] = 0x22;                       // EN_RXADDR:激活接收地址
    nrfTxData[1] = 0x01;                       // 使能接收通道0
    SPI_SendData(nrfTxData, nrfRxData, 2);
  
    nrfTxData[0] = 0x24;                       // SETUP_RETR
    nrfTxData[1] = 0x0a;                       // 不转发,重发一次
    SPI_SendData(nrfTxData, nrfRxData, 2);

    nrfTxData[0] = 0x25;                       // RF_CH:射频频道
    nrfTxData[1] = 0x40;                         // 频点2
    SPI_SendData(nrfTxData, nrfRxData, 2);
  
    nrfTxData[0] = 0x20;                       // CONFIG:PRx, CRC 1byte
    nrfTxData[1] = 0x0e;
    SPI_SendData(nrfTxData, nrfRxData, 2);  
  
  
    IO0SET = nRF_CE; 
}
//--------------------------------------------------------
//--------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -