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

📄 24e1.c

📁 nRF24E1作为 RF处理芯片
💻 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;

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

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

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 InitADC(void)
{
    ADCCON = 0x20;                  // Channel 0, NPD=1, ADCRUN=0, EXTREF=0
    ADCSTATIC &= 0x1c;
    ADCSTATIC |= 0x03;              // 12bit
    ADCCON &= ~0x80;                // Start..
    ADCCON |= 0x80;                 // ..new conversion
}

void InitPWM(void)
{
    P0_ALT = 0x80;                  // Enable PWM output
    PWMCON = 0xc0;                  // Enable 8 bit PWM with minimum prescaler
}

unsigned char ReceivePacket()
{
    unsigned char b;
    CE = 1;
    while(DR1 == 0)
        ;
    b = SpiReadWrite(0);
    CE = 0;
    return b;
}

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]);
    SpiReadWrite(b);
    CE = 0;
    Delay100us(3);                  // Wait ~300us   
}

unsigned char ReadADC(void)
{
    unsigned char b;

    while((EXIF & 0x10) == 0)       // Wait until ADC conversion complete
        ;
    EXIF &= ~0x10;                  // Clear ADC completion bit
    b = ADCDATAH;                   // Read ADC data
    ADCCON &= ~0x80;                // Start..
    ADCCON |= 0x80;                 // ..new conversion
    return b;
}

void WritePWM(unsigned char b)
{
    PWMDUTY = b;
}

void PutChar(char c)
{
    while(!TI)
        ;
    TI = 0;
    SBUF = c;
}

void PutString(const char *s)
{
    while(*s != 0)
        PutChar(*s++);
}

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 Receiver(void)
{
    unsigned char b;
    CS = 1;
    Delay100us(0);
    for(b=0;b<rconf.n;b++)
    {
        SpiReadWrite(rconf.buf[b]);
    }
    CS = 0;
    for(;;)
    {
        b = ReceivePacket();
        PutChar(b);
//      WritePWM(b);
    }
}

void Transmitter(void)
{
    unsigned char b;
    
    CS = 1;
    Delay100us(0);
    for(b=0;b<tconf.n;b++)
    {
        SpiReadWrite(tconf.buf[b]);
    }
    CS = 0;
//  for(;;)
//  {
//      b = ReadADC();              // Read ADC
	b= KeyByte;
        PutChar(b);		    // To serial port	
        TransmitPacket(b);          // Transmit data
//   }
}

void KeyScan(void)
{

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

void Init(void)
{

//  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    
    
}    


void main(void)
{
    Init();
    KeyByte=0xff;
//  PutString("Hello World!\n"); 
//  Receiver();  
//  InitADC();

    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;

    while(1)
    {
    	if (DR1 == 1)
    	{
    		KeyByte = SpiReadWrite(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
    		}    	
    		
    		PutChar(KeyByte);		     // To serial port	
    		Delayms(20);    	    	
        	KeyByte=0xff;
    		LED1=1;
    		LED2=1;
    		LED3=1;
    		LED4=1;    	    	
    	}
    	
    	KeyScan();    	
    	if (KeyByte!=0xff)
    	{
    		Transmitter();
    		Delayms(20);    	    	
        	KeyByte=0xff;
    		Init_Receiver();	    	   		    		
    		LED1=1;
    		LED2=1;
    		LED3=1;
    		LED4=1;    
    	}    	    	
    }       
 
}

⌨️ 快捷键说明

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