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

📄 transmitter.c

📁 关于NORDIC公司的NRF24E1芯片的单片机编程,使其自动载入EPPROM中内容
💻 C
字号:
/*=====================================================================
 * 
 *由 Keil C51 V7.50 编译通过
 *==============================================================================
*/
#include <reg24e1.h>

struct RFConfig
{
    unsigned char n;
    unsigned char buf[15];
};

typedef struct RFConfig RFConfig;

#define ADDR_INDEX   8   // Index to address bytes in RFConfig.buf 
#define ADDR_COUNT  4   // Number of address bytes


const RFConfig tconf =
{
    15,				 //If the nRF2401 subsystem is to be configured for 2 channel RX in ShockBurst, a
                    //total of 120 bits must be shifted in during the first configuration
                   // after VDD is applied.		   是不是不要120--143bit(Reserved for testing) 了
    0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,			//数据包是8位8位的传,地址是32位,需要CRC,是16位,
    0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6f, 0x05			   //1 Mbps的 RF Data Rate,16MHz的时钟频率,调制于2404MHz.
};		        //	The MSB bit should be loaded first into the configuration register


void Delay100us(volatile unsigned char n)
{
    unsigned char i;
    while(n--)
        for(i=0;i<35;i++)
            ;
}

void Delayms(volatile unsigned char n)
{
    unsigned char j;
    while(n--)
        for(j=0;j<10;j++)
	    	Delay100us(10);

}

unsigned char SpiReadWrite(unsigned char b)
                // Output data is shifted on negedge SCK, and input data is read on posedge SCK
{
    EXIF &= ~0x20;                   // Clear SPI interrupt
    SPI_DATA = b;                   // Move byte to send to SPI data register
    while((EXIF & 0x20) == 0x00)   // Wait until SPI hs finished transmitting
        ;
    return SPI_DATA;
}

void TransmitPacket(unsigned char b)
{
    unsigned char i;
    CE = 1;
    Delay100us(0);
    for(i=0;i<ADDR_COUNT;i++)
        SpiReadWrite(tconf.buf[ADDR_INDEX+i]);   //先传4bytes的地址(0xaa, 0xbb, 0x12, 0x34)
    SpiReadWrite(b);
    CE = 0;
    Delay100us(3);                               // Wait 300us   
}

unsigned char GetChar(void)
{
    unsigned char c;
    while(!RI)
        ;
    RI = 0;
    c=SBUF ;
    return c;
}

void Transmitter(void)
{
    unsigned char b;
    
    CS = 1;
    Delay100us(0);
    for(b=0;b<tconf.n;b++)
    {
        SpiReadWrite(tconf.buf[b]);
    }
    CS = 0;
    b=GetChar();                      
    TransmitPacket(b);          // Transmit data
}



void Init(void)
{

//  Port ini
    P0_ALT = 0x06;                    // Select alternate functions on pins P0.1 and P0.2,  TXD RXD 
         
    PWR_UP = 1;                    // Turn on Radio
    Delay100us(30);               // Wait > 3ms 
    SPICLK = 0;                  // Max SPI clock (XTAL/8)
    SPI_CTRL = 0x02;            // Connect internal SPI controller to Radio CH1

//  serial communication ini
    TH1 = 243;           // 19200@16MHz (when T1M=1 and SMOD=1)     19200=(2/32)*{(16M/4)*[1/(256-243)]} 
    CKCON |= 0x10;      // T1M=1 (/4 timer clock)                                            
    PCON = 0x80;       // SMOD=1 (double baud rate)
    SCON = 0x52;      // Serial mode 1, enable receiver
    TMOD = 0x20;     // Timer1 8bit auto reload 		
    TCON = 0x40;    // Start timer1    				
}    


void main(void)
{
    Init();
    while(1)
             {
             Transmitter();
             Delayms(500);   	    	//间隔0.5s发一次
             }       
}

⌨️ 快捷键说明

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