?? receiver.c
字號:
/*=====================================================================
*
*由 Keil C51 V7.50 編譯通過
*==============================================================================
*/
#include <reg24e1.h>
struct RFConfig
{
unsigned char n;
unsigned char buf[15];
};
typedef struct RFConfig RFConfig;
#define ADDR_INDEX 8 // Index to address bytes in RFConfig.buf
#define ADDR_COUNT 4 // Number of address bytes
const RFConfig rconf =
{
15, //If the nRF2401 subsystem is to be configured for 2 channel RX in ShockBurst, a
//total of 120 bits must be shifted in during the first configuration
// after VDD is applied. 是不是不要120--143bit(Reserved for testing) 了
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 數據包是8位8位的傳,地址是32位,需要CRC,是16位,
0xaa, 0xbb, 0x12, 0x34, 0x83, 0x6f, 0x05 //1 Mbps的 RF Data Rate,16MHz的時鐘頻率,調制于2404MHz.
}; // The MSB bit should be loaded first into the configuration register
void Delay100us(volatile unsigned char n)
{
unsigned char i;
while(n--)
for(i=0;i<35;i++)
;
}
unsigned char SpiReadWrite(unsigned char b)
// Output data is shifted on negedge SCK, and input data is read on posedge SCK
{
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;
}
unsigned char ReceivePacket()
{
unsigned char b;
CE = 1;
while(DR1 == 0)
;
b = SpiReadWrite(0); // Firsrt write dummy byte to SPI data register,when SPI data register have
// received data,evaluate to b
CE = 0;
return b;
}
void PutChar(char c)
{
while(!TI)
;
TI = 0;
SBUF = c;
}
void Receiver(void)
{
unsigned char b;
CS = 1;
Delay100us(0);
for(b=0;b<rconf.n;b++)
{
SpiReadWrite(rconf.buf[b]);
}
CS = 0;
for(;;)
{
b = ReceivePacket();
PutChar(b);
}
}
void Init(void)
{
// Port ini
P0_ALT = 0x06; // Select alternate functions on pins P0.1 and P0.2, TXD RXD
PWR_UP = 1; // Turn on Radio
Delay100us(30); // Wait > 3ms
SPICLK = 0; // Max SPI clock (XTAL/8)
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio CH1
// serial communication ini
TH1 = 243; // 19200@16MHz (when T1M=1 and SMOD=1) 19200=(2/32)*{(16M/4)*[1/(256-243)]}
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
TCON = 0x40; // Start timer1
}
void main(void)
{
Init();
while(1)
{
Receiver();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -