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

📄 24e1.c

📁 Nordic 公司 nRF24E1 芯片程序
💻 C
字号:
/*=====================================================================
 * 
 * nRF24E1-Quick-Dev快速开发系统源代码
 * 2003.7.1
 * 功能:
 * 1.nRF24E1的初始化控制
 * 2.nRF24E1的数据发射与数据接收
 * 3.串口通信 
 * 4.由 Keil C51 V6.10 and V7.05 编译通过
 *
 * 迅通科技保留版权  
 * 版本: 1.1 
 * 
 *==============================================================================
*/



#include <reg24e1.h>

/*  S1-S4  */
sbit S1       = P0^3;
sbit S2       = P0^0;
sbit S3       = P1^1;
sbit S4       = P1^0;


/*  LED1-LED4  */
sbit LED1      = P0^7;
sbit LED2      = P0^6;
sbit LED3      = P0^5;
sbit LED4      = P0^4;


unsigned char bdata KeyByte;

sbit L1        = KeyByte^0;
sbit L2        = KeyByte^1;
sbit L3        = KeyByte^2;
sbit L4        = KeyByte^3;


unsigned char flag, t0_point, t2_point;


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,
    0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6f, 0x04
};


const RFConfig rconf =
{
    15,
    0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6f, 0x05
};


unsigned char RxBuf[24];
unsigned char TxBuf[24];


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)
{
    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 init_T0(void)
{
	TR0 = 0;                        // Stop Timer 0
    TMOD &= ~0x03;                  // 
    TMOD |= 0x01;                   // External Interrupt 0, 
	CKCON |= 0x08; 
    TL0 = 0x60;                  
    TL0 = 0xF8;                  
	TF0 = 0; 
	ET0 = 1; 
	TR0=1;
}


void TransmitPacket(void)
{
	unsigned char i;
	
	CE = 1;
	Delay100us(0);
	
	for(i = 0; i < ADDR_COUNT; i++)
        SpiReadWrite(tconf.buf[ADDR_INDEX + i]);

	for(i = 0; i < 24; i++)
	{
		SpiReadWrite(TxBuf[i]);
	}
    CE = 0;
    Delay100us(3);                  // Wait ~300us   
}


void Init_Receiver(void)
{
    unsigned char b;

    CS = 1;
    Delay100us(0);
    
    for(b = 0; b < rconf.n; b++)
    {
        SpiReadWrite(rconf.buf[b]);
    }
    CS = 0;    
    CE = 1;
}	



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


void KeyScan(void)
{
    if (S1==0)                      //switch 1 haveing been pushed 
    {
    		L1=0; 
    		LED1=0;				 // ON LED1
		while(S1==0);
    }    	
    if (S2==0)                           //switch 1 haveing been pushed 
    {
    		L2=0; 
    		LED2=0;				 // ON LED2
		while(S2==0);
    }    	
    if (S3==0)                           //switch 1 haveing been pushed 
    {
    		L3=0; 
    		LED3=0;				 // ON LED3
		while(S3==0);
    }    	
    if (S4==0)                           //switch 1 haveing been pushed 
    {
    		L4=0; 
    		LED4=0;				 // ON LED4
		while(S4==0);
    }                
}	

void Init(void)
{
    flag=t0_point=t2_point=0;
//  Port ini
    P0_ALT = 0x06;  // Select alternate functions on pins P0.1 and P0.2,  TXD RXD 
    P0_DIR = 0x09;                  // P0.0, P0.3 is input(S1, S2), the rest output
    P0 = 0xF0;                      // P0.7-P0.4 = 1 for OFF LED1-LED4    
    P1_DIR = 0x03;		   			// P0.0, P0.3 is input(S3, S4),

	LED1=1;
	LED2=1;
	LED3=1;
	LED4=1;
    
    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

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



void main(void)
{
     Init();
	init_T0();

    KeyByte=0xff;

    Init_Receiver();

    LED1=0;
    Delayms(20);  
    LED1=1;

    LED2=0;
    Delayms(20);  
    LED2=1;

    LED3=0;
    Delayms(20);  
    LED3=1;
 
    LED4=0;
    Delayms(20);  
    LED4=1;

	EIE=0x04;
	PX4=1;
	TI=0;
	IE=0x92;
    	while(1)
    	{
		if(flag)
		{
			flag=0;
    			Delayms(20);    	    	
        		KeyByte=0xff;
    			LED2=1;
    			LED3=1;
    			LED4=1;

			SBUF=RxBuf[0];
		}
    		KeyScan();    	
    		if (KeyByte!=0xff)
    		{
    			Transmitter();
    			Delayms(20);    	    	
	        	KeyByte=0xff;
	    		Init_Receiver();	    	   		    		
	    		LED1=1;
	    		LED2=1;
	    		LED3=1;
	    		LED4=1; 
    		}
		if(t0_point>=100)
		{
			t0_point=0;
			LED2=~LED2;
		}	    	
    }     
}


void ISR_dr(void) interrupt 10
{
	unsigned char i;
  	EXIF &= ~0x40;                  // Clear DR1 interrupt

   	for(i=0;i<24;i++)
	{
		RxBuf[i]=SpiReadWrite(0);
	}
    KeyByte = RxBuf[0];

   	if (L1==0)                           //switch 1 haveing been pushed 
    {
    	LED1=0;			     // ON LED1
    }    	
    if (L2==0)                           //switch 1 haveing been pushed 
    {
    	LED2=0;			     // ON LED2
    }    	
    if (L3==0)                           //switch 1 haveing been pushed 
    {
    	LED3=0;			     // ON LED3
    }    	
    if (L4==0)                           //switch 1 haveing been pushed 
    {
    	LED4=0;			     // ON LED4
    } 
	flag=1;   		
   	KeyByte=0xff;
}


void ISR_uart(void) interrupt 4 
{
	TI = 0;
	LED1  = ~LED1;

	if(RI == 1)
    {
    	RI = 0;
        TxBuf[0] = SBUF;
		TxBuf[1] = TxBuf[0];
		RI = 0;
	}
}  


void Timer0ISR (void) interrupt 1
{
	TR0 = 0;
    TF0 = 0;                        // Clear Timer0 interrupt
    TL0 = 0x60;                  
    TL0 = 0xF8;                    // Reload Timer0 low byte
	t0_point++;
	TR0 = 1;
}

⌨️ 快捷键说明

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