📄 nrf9e5 test.txt
字号:
接收数据程序模块(unsigned char ReceivePacket(unsigned char *pBuf);)
发送数据程序模块(unsigned char TransmitPacket(unsigned char *pBuf);)
初始化接收函数(void InitReceiver(unsigned char n, unsigned char *pAddr);)
初始化发送函数(void InitTransmitter(unsigned char n, unsigned char *pAddr);)
发送验证码函数(void SendAck(unsigned char msgid))
接收验证码函数(unsigned char WaitAck(void))
信道选择函数(void SwitchChannel(void))
#include <Nordic\reg9e5.h>
#include "util.h"
#include "uart.h"
#include "radio.h"
void TEST(void);
void Receiver(void);
void send_data(unsigned char num);
void Transmitter(void);
extern INT8U ReviceCh(INT8U *ch);
extern INT8U recive_the_command();
extern xdata INT8U Buf[];
const char ID[] = "9E5DEMO1234567907654";
static unsigned char idata buf[25];
sbit LEDG =P0^6;
sbit LEDR =P0^5;
sbit PWM =P0^7;
sbit KEYR =P0^3;
sbit KEYT =P0^4;
unsigned char aaa,SQC;
unsigned char bbb;
unsigned char ccc;
unsigned char ddd,EEE;
unsigned char conter; //how many pack is have
unsigned int bh;
void Init(void)
{
unsigned char cklf;
SPICLK = 0; // Max SPI clock
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
// Switch to 16MHz clock:
RACSN = 0;
SpiReadWrite(RRC | 0x09);
cklf = SpiReadWrite(0) | 0x04;
RACSN = 1;
RACSN = 0;
SpiReadWrite(WRC | 0x09);
SpiReadWrite(cklf);
RACSN = 1;
P0_DIR=P0_DIR&0x1F; //led p0.6 p0.5 P0.7 output
LEDR=0;
LEDG=0;
P0_DIR |= 0x18; // P0.4 /3 is input
InitTimer();
EA = 1;
}
void main(void)
{
Init();
LEDR=0;
LEDG=0;
for (aaa=0;aaa<100;aaa++)
{Delay100us(50);}
LEDR=1;
LEDG=1;
for (aaa=0;aaa<100;aaa++)
{Delay100us(100);}
LEDR=0;
LEDG=0;
InitUart();
while(1)
{
// case 'R':
if (KEYR==0)
{
LEDR=1;
LEDG=0;
ReceiveFile();
}
//case 'T':
if (KEYT==0)
{
LEDR=0;
LEDG=1;
TransmitFile();
}
}
}
//----------------------------------UTAR OUT one byte-------------------
void send_data(unsigned char num)
{
int j;
TI=0;
SBUF=num;
for (j=0;j<10000;j++)
{if (TI)
{TI=0;
break;
}
}
}
//---------------------------------RECVIER-----------------------------
void Receiver(void)
{
unsigned char i2, n,j1;
InitReceiver(20, 0); //TEST IS 20BYTE
while(1)
{
if (ReceivePacket(Buf) != 0)
{
LEDG=0;
LEDR=1;
for (i2=0;i2<20;i2++)
{ PutChar(buf[i2]);}
for (j1=0;j1<105;j1++)
{Delay100us(200);} //LEDG 400mS
LEDG=1;
LEDR=1;
}
else
{
LEDR=0;
LEDG=1; // Indicates an error
}
n = 1;
}
}
//----------------------------------------------------------------------------------------------
void Transmitter(void)
{
unsigned int temp,j;
unsigned char n;
n=0;
InitTransmitter(20, 0);
while(1)
{
for (aaa=0;aaa<20;aaa++)
{buf[aaa]=ID[aaa];}
n=n+1;
buf[19]=n;
temp= ( TransmitPacket(Buf));
if (temp==1)
{
LEDR=0;
}
else {LEDG=0; //Y LED OFF
}
if (temp==1)
{ for (j=0;j<25;j++)
{Delay100us(200);} //LEDG 20mS
LEDR=1;
}
else {
for (j=0;j<25;j++)
{Delay100us(200);} //LEDG 400mS
LEDG=1;
}
for (ccc=0;ccc<3;ccc++) //2s
{
for (ddd=0;ddd<100;ddd++) //delay 1s
{Delay100us(100);} //delay10ms
}
}
}
//-------------------------end main.c---------------------------------------
#include <Nordic\reg9e5.h>
#include "util.h"
#include "radio.h"
#define HFREQ 1 // 0=433MHz, 1=868/915MHz 19/08/2004 set 915mhz
#define POWER 3 // 0=min power...3 = max power
unsigned char TransmitPacket1(unsigned char *pBuf,unsigned char *pBuf1);
unsigned char ReceivePacket1(unsigned char *pBuf,unsigned char *pBuf1);
const unsigned char code rnd_tab[256] =
{
// This table contains four each of the numbers 2 to 65 (0x02-0x41) randomly distributed.
// If you need longer random cycles than this you could use an 8-bit pseudo random generator
// and use it to index this table.
0x28, 0x29, 0x38, 0x07, 0x19, 0x17, 0x2F, 0x1F, 0x14, 0x16, 0x1F, 0x06, 0x38, 0x1B, 0x3B, 0x33,
0x33, 0x2F, 0x37, 0x1A, 0x32, 0x15, 0x19, 0x34, 0x1B, 0x2D, 0x13, 0x02, 0x3F, 0x18, 0x10, 0x10,
0x0A, 0x1D, 0x0D, 0x37, 0x3D, 0x0B, 0x2A, 0x25, 0x1F, 0x20, 0x41, 0x21, 0x2F, 0x2A, 0x36, 0x09,
0x1C, 0x2C, 0x0F, 0x1E, 0x39, 0x03, 0x06, 0x03, 0x2B, 0x0A, 0x1D, 0x02, 0x24, 0x3A, 0x32, 0x10,
0x16, 0x16, 0x13, 0x12, 0x20, 0x06, 0x13, 0x1F, 0x0D, 0x41, 0x1E, 0x08, 0x3D, 0x31, 0x09, 0x16,
0x23, 0x12, 0x2D, 0x12, 0x31, 0x1C, 0x36, 0x0B, 0x11, 0x21, 0x37, 0x13, 0x38, 0x41, 0x0B, 0x32,
0x1B, 0x33, 0x20, 0x29, 0x0E, 0x05, 0x41, 0x1E, 0x05, 0x0C, 0x38, 0x1D, 0x20, 0x2F, 0x29, 0x39,
0x3D, 0x04, 0x26, 0x10, 0x12, 0x36, 0x3B, 0x08, 0x3D, 0x3C, 0x1A, 0x24,
0x08, 0x07, 0x35, 0x15,
0x2A, 0x40, 0x2C, 0x11, 0x31, 0x28, 0x2A, 0x1E, 0x02, 0x07, 0x19, 0x32, 0x28, 0x39, 0x1A, 0x28,
0x19, 0x22, 0x03, 0x21, 0x39, 0x09, 0x03, 0x0F, 0x3E, 0x2D, 0x0D, 0x37, 0x18, 0x3E, 0x09, 0x25,
0x26, 0x11, 0x36, 0x1A, 0x18, 0x25, 0x3E, 0x29, 0x08, 0x3C, 0x2B, 0x33, 0x02, 0x0A, 0x05, 0x0A,
0x1C, 0x2C, 0x07, 0x23, 0x3E, 0x1B, 0x3F, 0x2E, 0x3B, 0x31, 0x2B, 0x24, 0x35, 0x26, 0x1D, 0x2E,
0x11, 0x3C, 0x15, 0x2B, 0x15, 0x24, 0x2C, 0x27, 0x0D, 0x34, 0x14, 0x23, 0x21, 0x04, 0x3A, 0x27,
0x0F, 0x40, 0x27, 0x0B, 0x30, 0x2E, 0x17, 0x25, 0x3C, 0x34, 0x40, 0x34, 0x0C, 0x2D, 0x30, 0x18,
0x40, 0x0F, 0x22, 0x26, 0x27, 0x3F, 0x30, 0x22, 0x30, 0x3F, 0x23, 0x14, 0x3A, 0x14, 0x3B, 0x0C,
0x06, 0x04, 0x05, 0x3A, 0x35, 0x0E, 0x0E, 0x0C, 0x17, 0x1C, 0x35, 0x0E, 0x04, 0x2E, 0x17, 0x22
};
static unsigned char seq, hopIdx, nBytes,PXX;
static unsigned rxTout, txTout;
static void SwitchChannel(void)
{
RACSN = 0;
SpiReadWrite(CC | (POWER << 2) | (HFREQ << 1));
SpiReadWrite(rnd_tab[hopIdx]);
RACSN = 1;
}
static void TransmitBytes(unsigned char *pBuf, unsigned char msgid)
{
// Transmit one packet with the format specified in the header of this file:
unsigned char i;
PXX=pBuf[19];
SwitchChannel();
TXEN = 1;
RACSN = 0;
SpiReadWrite(WTP);
SpiReadWrite(msgid);
for (i=0;i<nBytes;i++)
{
SpiReadWrite(pBuf);
}
RACSN = 1;
TRX_CE = 1;
Delay100us(1);
TRX_CE = 0;
while(DR == 0)
;
}
static void SendAck(unsigned char msgid)
{
// Send a one byte acknowledge
SwitchChannel();
TXEN = 1;
RACSN = 0;
SpiReadWrite(WTP);
SpiReadWrite(msgid);
RACSN = 1;
TRX_CE = 1;
Delay100us(1);
TRX_CE = 0;
while(DR == 0)
;
}
static unsigned char ReceiveBytes(unsigned char *pBuf)
{
unsigned char i, cmd;
SwitchChannel();
TXEN = 0;
TRX_CE = 1;
ResetTimer(1);
while(DR == 0)
{
if ((rxTout != 0) && (GetTimer(1) > rxTout))
{
TRX_CE = 0;
return 0;
}
}
TRX_CE = 0;
RACSN = 0;
SpiReadWrite(RRP);
cmd = SpiReadWrite(0);
i = 0;
while(DR)
{
pBuf[i++] = SpiReadWrite(0);
if (i == nBytes) // Play safe
break;
}
RACSN = 1;
return cmd;
}
static unsigned char WaitAck(void)
{
unsigned char ack;
SwitchChannel();
TXEN = 0;
TRX_CE = 1;
ResetTimer(0);
while(DR == 0)
{
if (GetTimer(0) > 5) // 5ms time-out
{
TRX_CE = 0;
return 0;
}
}
TRX_CE = 0;
RACSN = 0;
SpiReadWrite(RRP);
ack = SpiReadWrite(0);
RACSN = 1;
return ack;
}
unsigned char TransmitPacket(unsigned char *pBuf)
{
unsigned int i;
unsigned char cmdt, cmdr, j;
seq++;
cmdt = 0x10 | (seq & 0x0f);
cmdr = 0;
ResetTimer(1);
for(;;)
{
for (i=0;i<2;i++) //01/06/2005 256 to 2
{
// Repeat two times on all channels if no ACK is received
for (j=0;j<2;j++)
{
TransmitBytes(pBuf, cmdt);
cmdr = WaitAck();
//if ((cmdr != 0) && ((cmdr & 0x0f) == (cmdt & 0x0f)))
if (cmdr != 0)
{
// hopIdx++; not hop
return 1;
}
}
// hopIdx++; not hop
if ((txTout != 0) && (GetTimer(1) > txTout))
return 0;
}
}
return 0;
}
unsigned char ReceivePacket(unsigned char *pBuf)
{
unsigned char cmd;
while(1)
{
cmd = ReceiveBytes(pBuf);
if (cmd == 0)
return 0;
cmd = (cmd & 0x0f) | 0x20;
SendAck(cmd);
Delay100us(1);
SendAck(cmd);
Delay100us(10);
SendAck(0X20);
// hopIdx++; not hop
if ((cmd & 0x0f) != seq)
{
seq = cmd & 0x0f;
break;
}
}
return 1;
}
//void ReceiverTimeout(unsigned tOut)
//{
// Set the receiver timeout in ms.
// 1ms = min timeout, 65536ms is max, 0 means wait forever
// rxTout = tOut;
//}
//void TransmitterTimeout(unsigned tOut)
//{
// Set the transmitter timeout in ms.
// 1ms is min timeout, 65536ms is max, 0 means wait forever
// txTout = tOut;
//}
static void InitAddress(unsigned char spiAddr, unsigned char *pAddr)
{
unsigned char i;
RACSN = 0;
SpiReadWrite(spiAddr);
for(i=0;i<4;i++)
SpiReadWrite(pAddr);
RACSN = 1;
}
static void InitCommon(unsigned char nRx, unsigned char nTx)
{
hopIdx = 0; // Start at index #0 in the channel table above
RACSN = 0;
SpiReadWrite(WRC | 0x03); // Write to RF config address 3 (RX payload)
SpiReadWrite(nRx);
SpiReadWrite(nTx);
RACSN = 1;
}
void InitReceiver(unsigned char n, unsigned char *pAddr)
{
seq = 15;
rxTout = 3000; // Default 3s timeout
nBytes = n; // Number of data bytes (0-25)
InitCommon(n+1, 1);
if (pAddr != 0)
InitAddress(WRC | 0x05, pAddr); // Configure RX address
}
void InitTransmitter(unsigned char n, unsigned char *pAddr)
{
seq = 0;
// txTout = 3000; // Default 3s timeout
txTout = 300; // Default 3s timeout
nBytes = n; // Number of data bytes (0-25)
InitCommon(1, n+1);
if (pAddr != 0)
InitAddress(WTA, pAddr); // Configure TX address
}
//---------------------END RADIO .c----------------------------------------------------------
#include <Nordic\reg9e5.h>
#include "uart.h"
#include "util.h"
const char hex_tab[] = "0123456789ABCDEF"; // ASCII-hex table used by hex-output routines
static volatile unsigned idata timer[2]; // Two utility timers used in radio.c
static volatile unsigned char t0lrel, t0hrel;
void Delay100us(unsigned char n)
{
unsigned char i;
while(n--)
for(i=0;i<35;i++)
;
}
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;
}
//-------------------------------end init-------------------------------------------------
#include <Nordic\reg9e5.h>
#include "uart.h"
xdata INT8U Buf[20]="dgwedajdada";
#define UARTLENGTH 150
struct UARTBUFFER{ //UART0数据队列
INT8U Buffer[UARTLENGTH];
INT8U Counter;
INT8U In;
INT8U Out;
}xdata Rx,Tx;
unsigned char Addr = 0x03;
void InitUart(void)
{
Rx.Counter = 0;
Rx.In = 0;
Rx.Out = 0;
Tx.Counter = 0;
Tx.In = 0;
Tx.Out = 0;
TH1 = 243; // 19200@16MHz (when T1M=1 and SMOD=1)
// TH1 = 247; // 28800 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 &= ~0x30;
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 |= 0x02; // P0.1 (RxD) is input
ES = 1;
}
void PutChar(char c)
{
TI = 0;
SBUF = c;
while(!TI)
;
}
//----------------------串口中断处理程序
void UART0_Init(void) interrupt 4
{
if(RI){
RI = 0;
if(Rx.Counter < (UARTLENGTH - 1)){
Rx.Counter += 1;
Rx.Buffer[Rx.In] = SBUF;
Rx.In += 1;
if(Rx.In == (UARTLENGTH - 1)){
Rx.In = 0;
}
}
}
//if(TI){
//TI = 0;
// }
}
//-------------------串行数据接收程序
INT8U ReviceCh(INT8U *ch){
if(Rx.Counter > 0){
*ch = Rx.Buffer[Rx.Out];
Rx.Counter -= 1;
Rx.Out += 1;
if(Rx.Out == (UARTLENGTH - 1)){
Rx.Out = 0;
}
return 1;
}
return 0;
}
//--------------检测命令信息
INT8U recive_the_command()
{
INT8U Length,temp,ii;
INT16U i=0;
while(1)
{
if(Rx.Counter > 3)
{
ReviceCh(&temp);
if(temp==0x40)
{
ReviceCh(&temp);
Length = temp;
for(ii = 0 ; ii < Length;ii++)//接收数据包
{
while(ReviceCh(&temp) == 0);
Buf[ii+2] = temp;
}
if(Buf[Length-1]==0x0d)
{
Buf[0] = 0x40;
Buf[1] = Length;
return 0x55;//返回值正确,操作成功;
break;
}
}
}
};
// return 0x00 ; //请求重法
}
//------------------------------------------end uart.c--------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -