📄 ex1c.c
字号:
/*= ex1c.c =====================================================================
*
* Copyright (C) 2003, 2004 2005 WWW.C51RF.COM
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* WARRANTY OF ANY KIND.
*
* Author(s): WL ------ WWW. C51RF.COM
*
* DESCRIPTION:
*
* demo program.
*
* The functioN demo how to debug 24e1,
*
* COMPILER:
*
* This program has been tested with Keil V7.06
*
* $Revision: 1.12
*
*==============================================================================
*/
#include <Nordic\reg24e1.h>
#include "ISD51.H"
sbit LEDR =P0^4;
sbit LEDG =P0^5;
sbit KEYR =P0^3;
sbit KEYT =P0^6;
unsigned char aaa,bbb;
static unsigned char idata buf[32];
const char ID[] = "24E1DEMO1234567907654321";
const unsigned char code rnd_tab[256] =
{
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
};
#define ADDR_INDEX 8 // Index to default address bytes in RxTxConf.buf
#define ADDR_COUNT 4 // Number of address bytes
static unsigned char idata rfAddress[ADDR_COUNT];
struct RFConfig
{
unsigned char n;
unsigned char buf[15];
};
typedef struct RFConfig RFConfig;
const RFConfig RxTxConf =
{
15,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xcd, 0xef, 0x12, 0xaa, 0x83, 0x4f, 0x04 //6f to 4f 250k 08/08/2004
};
static unsigned char seq, hopIdx, nBytes;
static unsigned rxTout, txTout;
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;
}
//void PutString(const char *s)
//{
// while(*s != 0)
// PutChara(*s++);
//}
void TimerInit(void)
{
timer[0] = timer[1] = 0;
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
TR0 = 1; // Start Timer0
ET0 = 1; // Enable Timer0 interrupt
}
void Timer0ISR (void) interrupt 1 using 1
{
TF0 = 0; // Clear Timer0 interrupt
TH0 = t0hrel; // Reload Timer0 high byte
TL0 = t0lrel; // Reload Timer0 low byte
timer[0]++; // Increment timer[0]
timer[1]++; // Increment timer[1
}
void ResetTimer(unsigned char n)
{
ET0 = 0; // Disable Timer0 interrupt
timer[n & 0x01] = 0; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
}
unsigned GetTimer(unsigned char n)
{
unsigned tmp;
ET0 = 0; // Disable Timer0 interrupt
tmp = timer[n]; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
return tmp;
}
void Init(void)
{
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_DIR |= 0x02; // P0.1 (RxD) is an input
P0_ALT |= 0x06; // Select alternate functions on pins P0.1 and P0.2
P0_DIR=P0_DIR&0x1F; //led p0.6 p0.5 P0.7 output
P0_DIR=P0_DIR&0xCF; //led p0.4 p0.5 output
P0_DIR=P0_DIR |= 0x48; // P0.3 is input p0.6 input
ES = 0;
EA=1;
TimerInit();
}
//------------------------------------------------------------
//------------------------------------------------------------
static void SetRxMode(void)
{
// Put the transceiver into receive mode on current hopping frequency:
unsigned char ch;
CS = 1;
Delay100us(0);
ch = rnd_tab[hopIdx] << 1;
SpiReadWrite(ch | 0x01);
CS = 0;
}
static void SetTxMode(void)
{
// Put the transceiver into transmit mode on current hopping frequency:
unsigned char ch;
CS = 1;
Delay100us(0);
ch = rnd_tab[hopIdx] << 1;
SpiReadWrite(ch);
CS = 0;
}
static void InitCommon(unsigned char *pAddr)
{
unsigned char i;
hopIdx = 0; // Start at index #0 in the channel table above
if (pAddr != 0)
{
for(i=2;i<ADDR_INDEX;i++)
SpiReadWrite(RxTxConf.buf[i]);
for(i=0;i<ADDR_COUNT;i++)
{
rfAddress[i] = pAddr[i];
SpiReadWrite(pAddr[i]);
}
for(i=0;i<3;i++)
SpiReadWrite(RxTxConf.buf[RxTxConf.n-3+i]);
} else
{
for (i=0;i<ADDR_COUNT;i++)
{
rfAddress[i] = RxTxConf.buf[ADDR_INDEX+i];
}
for(i=2;i<RxTxConf.n;i++)
{
SpiReadWrite(RxTxConf.buf[i]);
}
}
CS = 0;
PWR_UP = 1; // Turn on power on radio
Delay100us(30); // Wait > 3ms
}
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;
SetTxMode();
CE = 1;
Delay100us(1);
for (i=0;i<ADDR_COUNT;i++)
{
SpiReadWrite(rfAddress[i]);
}
SpiReadWrite(msgid); // All packets start with a one byte ID
for (i=0;i<nBytes;i++)
{
SpiReadWrite(pBuf[i]);
}
CE = 0;
Delay100us(13); // Wait until finished transmitting (200us + 256us)//delay(5)
} // 250k to delay100 (13)
static void SendAck(unsigned char msgid)
{
// Send a one byte acknowledge
unsigned char i;
SetTxMode();
CE = 1;
Delay100us(1);
for (i=0;i<ADDR_COUNT;i++)
{
SpiReadWrite(rfAddress[i]);
}
SpiReadWrite(msgid); // An ACK contains only the ID and no data
CE = 0;
Delay100us(5); // Wait until finished transmitting delay100(3)
// 250K TO Delay delay100 (5) 08/08/2004
}
static unsigned char ReceiveBytes(unsigned char *pBuf)
{
unsigned char i, cmd;
SetRxMode();
CE = 1;
ResetTimer(1);
while(DR1 == 0)
{
if ((rxTout != 0) && (GetTimer(1) > rxTout))
{
CE = 0;
return 0;
}
}
cmd = SpiReadWrite(0);
i = 0;
while(DR1)
{
pBuf[i++] = SpiReadWrite(0);
if (i == nBytes)
break;
}
while(DR1)
{
SpiReadWrite(0);
}
CE = 0;
return cmd;
}
static unsigned char WaitAck(void)
{
unsigned char ack;
SetRxMode();
CE = 1;
ResetTimer(0);
while(DR1 == 0)
{
if (GetTimer(0) > 10) // 3ms time-out/10MS
{
CE = 0;
return 0;
}
}
ack = SpiReadWrite(0);
while(DR1)
{
SpiReadWrite(0);
}
CE = 0;
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<3;i++) //JUST 6 TIME
{
// 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)))
{
//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;
//done = 0;
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++;
if ((cmd & 0x0f) != seq)
{
seq = cmd & 0x0f;
//done = 1;
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;
//}
void InitReceiver(unsigned char n, unsigned char *pAddr)
{
seq = 15;
rxTout = 1000; // Default 3s timeout
nBytes = n; // Number of data bytes (0-25)
SPICLK = 0; // Max SPI clock
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
CE = 0;
CS = 1; // RF SPI CS = 1
Delay100us(0);
SpiReadWrite(0x00); // We are not using the DuoCeiver functionallity
SpiReadWrite((n+1)*8); // Number of Rx data bits
InitCommon(pAddr);
}
void InitTransmitter(unsigned char n, unsigned char *pAddr)
{
seq = 0;
txTout = 1000; // Default 3s timeout
nBytes = n; // Number of data bytes (0-25)
SPICLK = 0; // Max SPI clock
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
CE = 0;
CS = 1; // RF SPI CS = 1
Delay100us(0);
SpiReadWrite(0x00); // We are not using the DuoCeiver functionallity
SpiReadWrite(8); // Number of Rx data bits is 8 for the transmitter
InitCommon(pAddr);
}
void ReceiveFile(void)
{
// unsigned char xdata i;
InitReceiver(25, 0);
while(1)
{
if (ReceivePacket(buf) != 0)
{
LEDG=0;
LEDR=1;
//for (i=0;i<25;i++) //if use ISD debug ,dont use this one
// { PutChara(buf[i]);}
//for (j=0;j<20;j++)
//{Delay100us(200);} //LEDG 400mS
}
else
{
LEDR=0;
LEDG=1; // Indicates an error
//for (j=0;j<20;j++)
//{Delay100us(200);} //LEDG 400mS
}
//PutChar(buf[n++]);
}
}
void TransmitFile(void)
{
unsigned int xdata temp,j;
unsigned char xdata n;
n=0;
InitTransmitter(25, 0);
while(1)
{
for (aaa=0;aaa<25;aaa++)
{buf[aaa]=ID[aaa];}
n=n+1;
buf[24]=n;
temp= ( TransmitPacket(buf));
if (temp==1)
{ LEDR=0;
//s=s-1;
// buf[19]=s;
// speeker();
}
else {LEDG=0; //Y LED OFF
// i=4;
// s=4;
// speeker1();
}
if (temp==1)
{ for (j=0;j<25;j++)
{Delay100us(200);} //LEDG 20mS
//LEDR=0;
LEDR=1;
}
else {
for (j=0;j<25;j++)
{Delay100us(200);} //LEDG 400mS
//LEDR=0;
LEDG=1;
}
for (bbb=0;bbb<2;bbb++) //2s
{
for (aaa=0;aaa<100;aaa++) //delay 1s
{Delay100us(100);} //delay10ms
}
}
}
int main(void)
{ unsigned char aaa;
Init();
ISDwait();
EA=1;
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;
while(1)
{
// case 'R':
if (KEYR==0)
{
LEDR=1;
LEDG=0;
ReceiveFile();
}
//case 'T':
if (KEYT==0)
{
LEDR=0;
LEDG=1;
TransmitFile();
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -