📄 9e5quick.c
字号:
/*===============================================================================================
*
* Copyright (C) 2004
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* WARRANTY OF ANY KIND.
*
* Author(s):
*
* DESCRIPTION: for nRF9E5
*
* nRF9E5/nRF24E1 range demo. Select receiver by shorting P03 and P05 and transmitter by shorting
* P05 and P06. Uncomment/comment the appropriate #define below to select nRF9E5 or nRF24E1.
*
* The transmitter continously send one byte packets. Each time the receiver receives a packet the
* P00 pin is set low (LED1 is turned on on the 9E5 eval board). At the same time a 20ms timer is
* started and if a new packets is not received before the 20ms time-out the P00 pin is set high
* (LED1 is turned off). If a new packet is received before the time-out a new 20ms time-out period
* is started.
*
* Please remember to turn off the RS232 switch on the receiver and transmitter boards. On the
* nRF9E5 board turn off all dip-switches on the transmitter and turn on only the LED1 switch on
* the receiver.
*
* COMPILER:
*
* This program has been tested with Keil C51 V7.09
*
* $Revision: 1 $
*
*==================================================================================================
*/
// Comment out the following line for nRF24E1
#include <reg9e5.h>
#define POWER 3 // 0=min power...3 = max power
#define HFREQ 0 // 0=433MHz, 1=868/915MHz
#define CHANNEL 351 // Channel number: f(MHz) = (422.4+CHANNEL/10)*(1+HFREQ)
#define TIMEOUT 20 // 20ms time-out on LED
#define ON 1
#define OFF 0
static volatile unsigned char timer;
static volatile unsigned char t0lrel, t0hrel;
unsigned char bdata KeyValue;
sbit L0 = KeyValue^4;
sbit L1 = KeyValue^5;
sbit L2 = KeyValue^6;
sbit S0 =P0^5;
sbit S1 =P0^6;
sbit S2 =P0^7;
sbit LED0 =P0^0;
sbit LED1 =P0^3;
sbit LED2 =P0^4;
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 i;
while(n--)
for(i=0;i<10;i++)
Delay100us(10);
}
void PutChar(char c)
{
while(!TI)
;
TI = 0;
SBUF = c;
}
void PutString(const char *s)
{
while(*s != 0)
PutChar(*s++);
}
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 ReceivePacket()
//unsigned char ReceivePacket()
{
unsigned char b;
TRX_CE = 1;
if(DR)
{
RACSN = 0;
SpiReadWrite(RRP);
b = SpiReadWrite(0);
RACSN = 1;
TRX_CE = 0;
KeyValue=b;
PutChar(b);
if(L0) { LED0=0; }
if(L1) { LED1=0; }
if(L2) { LED2=0; }
Delayms(10);
LED0=1; // LED OFF
LED1=1;
LED2=1;
}
// return b;
}
void TransmitPacket(unsigned char b)
{
RACSN=0; // Spi enable for write a spi command
SpiReadWrite(WTA); // Write address command
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
RACSN=1; // Spi disable
Delay100us(1);
RACSN = 0;
SpiReadWrite(WTP);
SpiReadWrite(b);
RACSN = 1;
TRX_CE = 1;
Delay100us(10);
TRX_CE = 0;
while(DR == 0)
;
}
void ScanKey()
{
KeyValue=0x00;
if(S0==0)
{
L0=1;
LED0=0;
Delayms(10);
LED0=1;
}
if(S1==0)
{
L1=1;
LED1=0;
Delayms(10);
LED1=1;
}
if(S2==0)
{
L2=1;
LED2=0;
Delayms(10);
LED2=1;
}
if(KeyValue!=0x00)
{
// PutChar(KeyValue);
TXEN = 1;
TransmitPacket(KeyValue);
TXEN=0;
// Delayms(10);
// LED0=1;
// LED1=1;
// LED2=1;
}
}
void Led(unsigned char on)
{
if (on)
{
P0 &= ~0x01;
timer = 0;
TR0 = 1; // Start Timer0
} else
P0 |= 0x01;
}
void InitTimer(void)
{
TR0 = 0;
TMOD &= ~0x03;
TMOD |= 0x01; // mode 1
CKCON |= 0x08; // T0M = 1 (/4 timer clock)
t0lrel = 0x60; // 1KHz tick...
t0hrel = 0xF0; // ... = 65536-16e6/(4*1e3) = F060h
TF0 = 0; // Clear any pending Timer0 interrupts
ET0 = 1; // Enable Timer0 interrupt
}
void Timer0ISR (void) interrupt 1
{
TF0 = 0; // Clear Timer0 interrupt
TH0 = t0hrel; // Reload Timer0 high byte
TL0 = t0lrel; // Reload Timer0 low byte
timer++;
if (timer == TIMEOUT)
{
// P0 |= 0x01; // Led off
TR0 = 0; // Stop timer
}
}
void Receiver(void)
{
// unsigned char b;
unsigned char bo, err;
TXEN = 0;
bo = err = 0;
for(;;)
{
// b = ReceivePacket();
Led(ON);
}
}
void Transmitter(void)
{
unsigned char b;
TXEN = 1;
b = 0;
for(;;)
{
TransmitPacket(b++); // Transmit data
}
}
void Init(void)
{
unsigned char tmp;
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
TR1 = 1; // Start timer1
P0_ALT |= 0x06; // Select alternate functions on pins P0.1 and P0.2
P0_DIR = 0xE2; // P0.0 out,P0.1 input(RXD),P0.2 P0.3 P0.4 output,P0.5 P0.6 P0.7 input
// 11100010
// P0_ALT |= 0x00;
// P0_DIR=0xaa;
SPICLK = 1;
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
LED0 = 1;
LED1 = 1;
LED2 = 1;
// Switch to 16MHz clock:
RACSN = 0;
SpiReadWrite(RRC | 0x09);
// tmp = SpiReadWrite(0) | 0x04;
tmp = SpiReadWrite(0) | 0x0C;
RACSN = 1;
RACSN = 0;
SpiReadWrite(WRC | 0x09);
SpiReadWrite(tmp);
RACSN = 1;
// Configure Radio:
RACSN = 0;
SpiReadWrite(WRC | 0x03); // Write to RF config address 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 address 1
tmp = SpiReadWrite(0) & 0xf0; // Clear the power and frequency setting bits
RACSN = 1;
RACSN = 0;
SpiReadWrite(WRC); // Write RF config address 0
SpiReadWrite(CHANNEL & 0xff); // CHANNEL bit7..0
// Change power defined by POWER above, to 433 or 868/915MHz defined by HFREQ and
// bit8 of CHANNEL:
SpiReadWrite(tmp | (POWER<<2) | (HFREQ << 1) | ((CHANNEL >> 8) & 0x01));
SpiReadWrite(0x44); //2004.11.13
SpiReadWrite(0x01); // One byte RX payload
SpiReadWrite(0x01); // One byte TX payload
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
SpiReadWrite(0xE7);
RACSN = 1;
InitTimer();
EA = 1;
}
void main(void)
{
Init();
LED0 = 0;
Delayms(10);
LED1 = 0;
Delayms(10);
LED2 = 0;
Delayms(10);
// PutString("Hello World!\n");
LED0 = 1; //off led
LED1 = 1;
LED2 = 1;
while(1)
{
ScanKey();
ReceivePacket();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -