×
单片机 > 单片机程序设计 > 详情

AVR M16单片机NRF2401无线通信开发包

发布时间:2020-08-26 发布时间:
|

单片机源程序如下:

//ICC-AVR application builder : 

// Target : M16

// Crystal: 8.0000Mhz

#include

#include

//------------------------------------------------------------------------------

//--------------------------------NRF24L01接口定义--------------------------------

//------------------------------------------------------------------------------

//#define NRF24L01_MISO          PB4                //输入0        

#define   Hign_24L01_MISO    PORTB |= (1 << PB4) 

#define   Low_24L01_MISO     PORTB &= ~(1 << PB4)

#define   Read_24L01_MISO    PINB & (1 << PB4) 


//#define NRF24L01_MOSI         PB2              //输出1

#define   Hign_24L01_MOSI    PORTB |= (1 << PB2)  

#define   Low_24L01_MOSI     PORTB &= ~(1 << PB2)

#define   Read_24L01_MOSI    PINB & (1 << PB2) 


//#define NRF24L01_CSN                     PB7              //输出1

#define   Low_24L01_CSN     PORTB &= ~(1 << PB7)

#define   Hign_24L01_CSN           PORTB |= (1 << PB7)


//#define NRF24L01_SCK                  PB6             //输出1

#define   Hign_24L01_SCK    PORTB |= (1 << PB6)   

#define   Low_24L01_SCK     PORTB &= ~(1 << PB6)                 

#define   Read_24L01_SCK    PINB & (1 << PB6);


//#define NRF24L01_CE                PB5              //输出1

#define   Hign_24L01_CE    PORTB |= (1 << PB5)  

#define   Low_24L01_CE     PORTB &= ~(1 << PB5)                

#define   Read_24L01_CE    PINB & (1 << PB5)


//#define  NRF24L01_IRQ     PB3               //输入

#define   Hign_NRF24L01_IRQ    PORTB |= (1 << PB3)   

#define   Low_NRF24L01_IRQ     PORTB &= ~(1 << PB3)

#define   Read_NRF24L01_IRQ    PINB & (1 << PB3)

  

//------------------------------数码管位选--------------------------------------

//#define Display1   PA1                      //输出1

#define   Hign_Display1     PORTA |= (1 << PA1);   

#define   Low_Display1            PORTA &= ~(1 << PA1); 

//#define Display2      PA2                  //输出1

#define   Hign_Display2     PORTA |= (1 << PA2);   

#define   Low_Display2            PORTA &= ~(1 << PA2); 

//#define Display3     PA3                  //输出1

#define   Hign_Display3     PORTA |= (1 << PA3);   

#define   Low_Display3             PORTA &= ~(1 << PA3); 

//#define Display4      PA4                 //输出1

#define   Hign_Display4     PORTA |= (1 << PA4);   

#define   Low_Display4             PORTA &= ~(1 << PA4);   

//-------------------------------BELL蜂明器-------------------------------------

#define   Hign_BELL                 PORTD |= (1 << PD5)  

#define   Low_BELL                 PORTD &= ~(1 << PD5) 

//-------------------------------JDQ继电器--------------------------------------

#define   Hign_JDQ                  PORTD |= (1 << PD7)

#define   Low_JDQ                    PORTD &= ~(1 << PD7) 

//***********************************数码管0-9编码*******************************************

char seg[10]={0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};         //0~~9段码

char TxBuf[32]=

{

0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,

0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,

0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,

0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,

};

char sta,tf,KEY0;

//*********************************************NRF24L01*************************************

#define TX_ADR_WIDTH    5           // 5 uints TX address width

#define RX_ADR_WIDTH    5           // 5 uints RX address width

#define TX_PLOAD_WIDTH  32          // 20 uints TX payload

#define RX_PLOAD_WIDTH  32          // 20 uints TX payload

char  TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};        //本地地址

char  RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01};        //接收地址

//***************************************NRF24L01寄存器指令*******************************************************

#define READ_REG        0x00          // 读寄存器指令

#define WRITE_REG       0x20         // 写寄存器指令

#define RD_RX_PLOAD     0x61          // 读取接收数据指令

#define WR_TX_PLOAD     0xA0          // 写待发数据指令

#define FLUSH_TX        0xE1         // 冲洗发送 FIFO指令

#define FLUSH_RX        0xE2          // 冲洗接收 FIFO指令

#define REUSE_TX_PL     0xE3          // 定义重复装载数据指令

#define NOP1             0xFF          // 保留

//*************************************SPI(nRF24L01)寄存器地址****************************************************

#define CONFIG          0x00  // 配置收发状态,CRC校验模式以及收发状态响应方式

#define EN_AA           0x01  // 自动应答功能设置

#define EN_RXADDR       0x02  // 可用信道设置

#define SETUP_AW        0x03  // 收发地址宽度设置

#define SETUP_RETR      0x04  // 自动重发功能设置

#define RF_CH           0x05  // 工作频率设置

#define RF_SETUP        0x06  // 发射速率、功耗功能设置

#define STATUS          0x07  // 状态寄存器

#define OBSERVE_TX      0x08  // 发送监测功能

#define CD              0x09  // 地址检测           

#define RX_ADDR_P0      0x0A  // 频道0接收数据地址

#define RX_ADDR_P1      0x0B  // 频道1接收数据地址

#define RX_ADDR_P2      0x0C  // 频道2接收数据地址

#define RX_ADDR_P3      0x0D  // 频道3接收数据地址

#define RX_ADDR_P4      0x0E  // 频道4接收数据地址

#define RX_ADDR_P5      0x0F  // 频道5接收数据地址

#define TX_ADDR         0x10  // 发送地址寄存器

#define RX_PW_P0        0x11  // 接收频道0接收数据长度

#define RX_PW_P1        0x12  // 接收频道0接收数据长度

#define RX_PW_P2        0x13  // 接收频道0接收数据长度

#define RX_PW_P3        0x14  // 接收频道0接收数据长度

#define RX_PW_P4        0x15  // 接收频道0接收数据长度

#define RX_PW_P5        0x16  // 接收频道0接收数据长度

#define FIFO_STATUS     0x17  // FIFO栈入栈出状态寄存器设置

//**************************************************************************************

void Delay(int s);

void inerDelay_us(char n);

void init_NRF24L01(void);

char SPI_RW(char data);

char SPI_Read(char reg);

char SPI_RW_Reg(char reg, char value);

char SPI_Read_Buf(char reg, char *pBuf, char uchars);

char SPI_Write_Buf(char reg, char *pBuf, char uchars);

void SetRX_Mode(void);

char nRF24L01_RxPacket(char* rx_buf);

void nRF24L01_TxPacket(char * tx_buf);

//*****************************************长延时*****************************************

void Delay(int s)

{

        unsigned int i;

        for(i=0; i

        for(i=0; i

}

/******************************************************************************************

/*延时函数

/******************************************************************************************/

void iner



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
AVR熔丝位操作时的要点和需要注意的相关事项