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

📄 exp.c

📁 该代码用于2.4G频率无线通信芯片nrf24e1, 是通过51单片机对该芯片进行参数设定和无线传输的一个代码模块。在运用该芯片时可以用该代码测试硬件连接
💻 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>
#include <intrins.h>
#include <absacc.h>
#include "ISD51.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, 0x4f, 0x04
};

const RFConfig rconf =
{
    15,
    0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xaa, 0xbb, 0x12, 0x34, 0x83, 0x4f, 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 PutChar0(char c)
{
    while(!TI)
        ;
    TI = 0;
    SBUF = c;
}

void PutString(const char *s)
{
    while(*s != 0)
        PutChar0(*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();
        PutChar0(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;
        b= 0x55;
        PutChar0(b);		    // To serial port	
        TransmitPacket(b);          // Transmit data
//   }
}
void light(void)
{LED1=0;
 LED2=0;
 LED3=0;
 LED4=0;
}

void KeyScan(void)
{

    if (S1==0)                           //switch 1 haveing been pushed 
    {
    	L1=0; 
    	light();				 // ON LED1
    }    	
    if (S2==0)                           //switch 1 haveing been pushed 
    {
    	L2=0; 
    	light();				 // ON LED2
    }    	
    if (S3==0)                           //switch 1 haveing been pushed 
    {
    	L3=0; 
    	light();				 // ON LED3
    }    	
    if (S4==0)                           //switch 1 haveing been pushed 
    {
    	L4=0; 
    	light();				 // 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
     T2CON   = 0x34;      /* Use Timer 2 as baudrate generator  */

    RCAP2H  = 0xFF;
    RCAP2L  = 0xF7;      //* 57600 baud @ 16MHz  
    CKCON |= 0x10;  // T1M=1 (/4 timer clock)
    SCON = 0x52;    // Serial mode1, enable receiver
   
    EA = 1;              /* Enable global interrupt flag       */
}    



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 1   // init ISD51 only when the uVision2 Debugger tries to connect
        ISDcheck();      // initialize uVision2 Debugger and continue program run
        #endif
    	if (DR1 == 1)
    	{
    		KeyByte = SpiReadWrite(0);
        	if (L1==0)                           //switch 1 haveing been pushed 
    		{
    			light();			     // ON LED1
    		}    	
        	if (L2==0)                           //switch 1 haveing been pushed 
    		{
    			light();			     // ON LED2
    		}    	
        	if (L3==0)                           //switch 1 haveing been pushed 
    		{
    			light();			     // ON LED3
    		}    	
        	if (L4==0)                           //switch 1 haveing been pushed 
    		{
    			light();			     // ON LED4
    		}    	
    		
    		PutChar0(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 + -