📄 radio.c
字号:
/*= radio.c ========================================================================================
*
* Copyright (C) 2004 Nordic Semiconductor
*
* This file is distributed in the hope that it will be useful, but WITHOUT WARRANTY OF ANY KIND.
*
* Author(s): Ole Saether
*
* DESCRIPTION:
*
* Packet format:
*
* [4 byte address][1 byte msg ID][0-25 bytes msg data][16 bit CRC]
*
*
* COMPILER:
*
* This program has been tested with Keil C51 V7.08
* 01/06/2005 change to send 2time on one time
*
* $Revision: 6 $
*
*==================================================================================================
*/
#include "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);
extern unsigned char idata buf[];
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
};
unsigned char seq, hopIdx, nBytes,PXX,ch;
static unsigned rxTout, txTout;
static void SwitchChannel(unsigned char iii)
{
RACSN = 0;
SpiReadWrite(CC | 0x0e);
SpiReadWrite(rnd_tab[iii]);
RACSN = 1;
}
//
static void TransmitBytes(unsigned char *pBuf)
{
// Transmit one packet with the format specified in the header of this file:
unsigned char i;
PXX=pBuf[19];
SwitchChannel(0);;//在将程序写入到RX1和RX2的时候需要改变该函数的输入值
//在这里我们将RX2的频道放在2上,所以在向RX2写入程序的时候将其改为
//SwitchChannel(2);
TXEN = 1;
RACSN = 0;
SpiReadWrite(WTP);
//SpiReadWrite(msgid);
for (i=0;i<nBytes;i++)
{
SpiReadWrite(pBuf[i]);
}
RACSN = 1;
TRX_CE = 1;
Delay100us(1);
TRX_CE = 0;
while(DR == 0)
;
}
static unsigned char ReceiveBytes(unsigned char *pBuf)
{
unsigned char i;
while(1)
{
SwitchChannel(0);
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);
i = 0;
while(DR)
{
pBuf[i++] = SpiReadWrite(0);
if (i == nBytes) // Play safe
break;
}
RACSN = 1;
return 1;
};
}
unsigned char TransmitPacket(unsigned char *pBuf)
{
// unsigned int i;
ResetTimer(1);
TransmitBytes(pBuf);
if ((txTout != 0) && (GetTimer(1) > txTout))
return 0;
}
unsigned char ReceivePacket(unsigned char *pBuf)
{
unsigned char cmd;
while(1)
{
cmd = ReceiveBytes(pBuf);
if (cmd == 0)
return 0;
else if(cmd == 1)
return 1;
}
}
static void InitAddress(unsigned char spiAddr, unsigned char *pAddr)
{
unsigned char i;
RACSN = 0;
SpiReadWrite(spiAddr);
for(i=0;i<4;i++)
SpiReadWrite(pAddr[i]);
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 = 50; // Default 3s timeout
nBytes = n; // Number of data bytes (0-25)
InitCommon(n, 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(0, n);
if (pAddr != 0)
InitAddress(WTA, pAddr); // Configure TX address
}
//---------------------END RADIO----------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -