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

📄 nrf_perf.c

📁 Nrf9E5无线收发协议
💻 C
字号:
/*= nRF_Perf.c ==================================================================
 *
 * Development By www.51kaifa.com
 * Copyright (C) 2005
 *
 * 无忧无线SOC开发平台专用测试程序
 * 版本:1.1
 *
 * 2006年2月14日
 *
 *===============================================================================
*/

#include <Nordic\reg9e5.h>

#define HFREQ 0                     // 0=433MHz, 1=868/915MHz
#define POWER 3                     // 0=min power...3 = max power

bit rx_rqst;
char rcv_data;
unsigned int syn_count;

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

SpiWrite(unsigned char b)
{
	while((EXIF & 0x20) == 0x00)    // Wait until SPI hs finished transmitting
        ;
    EXIF &= ~0x20;                  // Clear SPI interrupt
    SPI_DATA = b;                   // Move byte to send to SPI data register
}

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

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


void Init(void)
{
    unsigned char tmp;

	//SPI 设置
	
    SPICLK = 0;                     // Max SPI clock
    SPI_CTRL = 0x02;                // Connect intewrnal SPI controller to Radio

    // Configure Radio:
    RACSN = 0;
    SpiReadWrite(WRC | 0x03);       // Write to RF config Rx_deepess 3 (RX payload)
    SpiReadWrite(0x01);             // One byte RX payload
    SpiReadWrite(0x01);             // One byte TX payload
    RACSN = 1;

    RACSN = 0;
    SpiReadWrite(RRC | 0x01);       // Read RF config Rx_deepess 1
    tmp = SpiReadWrite(0) & 0xf1;   // Clear the power and frequency setting bits
    RACSN = 1;

    RACSN = 0;
    SpiReadWrite(WRC | 0x01);      // Write RF config Rx_deepess 1
    // Change power defined by POWER and to 433 or 868/915MHz defined by HFREQ above:
    SpiReadWrite(tmp | (POWER <<2) | (HFREQ << 1));
    RACSN = 1;

    RACSN = 0;
    SpiReadWrite(RRC | 0x09);       // Read RF config Rx_deepess 1
    tmp = SpiReadWrite(0) | 0x04;   // Clear the power and frequency setting bits
    RACSN = 1;


    RACSN = 0;
    SpiReadWrite(WRC | 0x09);       // Write to RF config Rx_deepess 9 (cpu clk)
    SpiReadWrite(tmp);             // One byte RX cpu clk
    RACSN = 1;

}

void uart_init(void)
{
    TH1 = 0xE6;                      // 9600@16MHz (when T1M=1 and SMOD=1)
    CKCON |= 0x30;                  // T1M=1 (/4 timer clock)
    PCON = 0x80;                    // SMOD=1 (double baud rate)
    SCON = 0x52;                    // Serial mode1, enable receiver
    TMOD = 0x20;                    // Timer1 8bit auto reload 
    TR1 = 1;                        // Start timer1
    P0_ALT = 0x06;                 // Select alternate functions on pins P0.1 and P0.2
    P0_DIR = 0xAA;                 // P0.1 (RxD) is input

}

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

void Transmitstring()
{

	RACSN = 0;
	SpiWrite(WTP);
	SpiWrite(0x23);
	RACSN = 1;

	TRX_CE = 1;	
	Delay(5);
	TRX_CE = 0;
}

void DR_ISR(void) interrupt 10
{

   	EXIF = EXIF & 0xBF;

    RACSN = 0;
    SpiReadWrite(RRP);
	rcv_data = SpiReadWrite(0);
    RACSN = 1;

	if (rcv_data == 0x23)
		{
		rx_rqst = 1;
		P06 = ~P06;
		syn_count = 16384;
		}

}

void main(void)
{

    Init();
	uart_init();

	EX4 = 1;
	PX4 = 1;

	EA = 1;

	PutString("hello world!\n");

    while(1)
		{

		syn_count++;

		if (syn_count==32000)
		{
		TXEN = 1;
		}
		if (syn_count==32767)
			{
			Transmitstring();
			P04 = ~P04;
			syn_count=0;
			TXEN = 0;
			TRX_CE = 1;
			}

		if (rx_rqst==1)
			{
			rx_rqst=0;
			PutChar(rcv_data);							
			}

		}
}


// End

⌨️ 快捷键说明

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