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

📄 improve.txt

📁 nrf905开发板:AT89S52单片机与NRF905实现无线通信
💻 TXT
字号:
#include<reg51.h> 
#include <ABSACC.h> 
#include <intrins.h> 
#include <stdio.h> 

#define  uchar unsigned char  
#define uint unsigned int  

bdata unsigned  char DATA_BUF; 
#define DATA7        ((DATA_BUF&BYTE_BIT7) != 0) 
#define DATA0   ((DATA_BUF&BYTE_BIT0) != 0) 
sbit        flag        =DATA_BUF^7; 
sbit        flag1        =DATA_BUF^0; 

#define TxRxBuf_Len 4 
unsigned char TxRxBuf[TxRxBuf_Len]= 
{ 
0x29,0x30,0x31,0x32, 
}; 

sbit        TXEN=P2^6; 
sbit        TRX_CE=P2^5; 
sbit        PWR=P2^4; 
sbit        MISO=P2^3; 
sbit        MOSI=P2^2; 
sbit        SCK=P2^1; 
sbit        CSN=P2^0; 
sbit        AM=P3^7; 
sbit        DR=P2^7; 
sbit        CD=P3^5; 
#define BYTE_BIT0        0x01 
#define BYTE_BIT1        0x02 
#define BYTE_BIT2        0x04 
#define BYTE_BIT3        0x08 
#define BYTE_BIT4        0x10 
#define BYTE_BIT5        0x20 
#define BYTE_BIT6        0x40 
#define BYTE_BIT7        0x80 


#define WC                 0x00 
#define RC                 0x10 
#define WTP                0x20 
#define RTP                0x21 
#define WTA                0x22 
#define RTA                0x23 
#define RRP                0x24 

unsigned char idata RFConf[10]= 
{ 
  0x6a,                             //CH_NO,配置频段在423MHZ 
  0x0e,                             //输出功率为10db,不重发,节电为正常模式 
  0x44,                             //地址宽度设置,为4字节 
  0x03,0x03,                        //接收发送有效数据长度为32字节 
  0xaa,0xaa,0xaa,0xaa,              //接收地址 
  0xd8,                              //CRC充许,8位CRC校验,外部时钟信号不使能,16M晶振 
}; 

code TxAddress[4]={0xaa,0xaa,0xaa,0xaa}; 


static void Delay(uchar n)
{
	uint i;
	while(n--)
	for(i=0;i<80;i++);	
}


void SpiWrite(unsigned char send) 
{ 
        unsigned char i; 
        DATA_BUF=send; 
        for (i=0;i<8;i++) 
        { 
                if (flag)        //总是发送最高位 
                 {
                        MOSI=1; 
                }
                else 
                {
                        MOSI=0; 
                }
                SCK=1; 
                DATA_BUF=DATA_BUF<<1; 
                SCK=0; 
        } 
} 

void nRF905Init(void) 
{ 
    CSN=1;                                                // Spi         disable 
    SCK=0;                                                // Spi clock line init low 
    DR=0;                                                // Init DR for input 
    AM=0;                                                // Init AM for input 
    CD=0;                                                // Init CD for input 
    PWR=1;                                        // nRF905 power on 
    TRX_CE=0;                                        // Set nRF905 in standby mode 
    TXEN=0;                                        // set radio in Rx mode 
} 

void Config905(void) 
{ 
        uchar i; 
        CSN=0;                                                // Spi enable for write a spi command 
        SpiWrite(WC);                                // Write config command写放配置命令 
        for (i=0;i<10;i++)        // Write configration words  写放配置字 
        { 
           SpiWrite(RFConf[i]); 
        } 
        CSN=1;                                        // Disable Spi 
} 


void TxPacket(uchar *TxRxBuf) 
{ 
        uchar i; 
        //Config905(); 
        CSN=0; 
        SpiWrite(WTP);                                // Write payload command 
        for (i=0;i<4;i++) 
        { 
                SpiWrite(TxRxBuf[i]);                // Write 32 bytes Tx data 
        }// Spi enable for write a spi command 
        CSN=1; 
        Delay(1);                                          // Spi disable 
        CSN=0;                                                // Spi enable for write a spi command 
        SpiWrite(WTA);                                // Write address command 
        for (i=0;i<4;i++)                        // Write 4 bytes address 
        { 
                SpiWrite(TxAddress[i]); 
        } 
        CSN=1;                                                // Spi disable 
        TRX_CE=1;                                        // Set TRX_CE high,start Tx data transmission 
        Delay(1);                                        // while (DR!=1); 
        TRX_CE=0;                                        // Set TRX_CE low 
} 


void SetTxMode(void) 
{ 
        PWR=1;
        TRX_CE=1; 
        TXEN=1; 
        Delay(1);                                         // delay for mode change(>=650us) 
} 

main()
{
    nRF905Init();//初始化nRF905 
    Config905();//初始化nRF905寄存器 
    while(1)
    {
      TxRxBuf[0]=0x29;
      TxRxBuf[1]=0x30;
      TxRxBuf[2]=0x31;
      TxRxBuf[3]=0x32; 
      for(i=0;i<2;i++)
      {
      SetTxMode();                                // Set nRF905 in Tx mod 
      TxPacket(TxRxBuf);                          // Send data by nRF905
      }
      Delay(250);
    }
}





⌨️ 快捷键说明

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