?? 24e1_repeater.c
字號:
#include <Nordic\reg24e1.h>
//include "ISD51.H"
sbit LEDR = P0^4;
sbit LEDG = P0^5;
sbit KEYR = P0^3;
sbit KEYT = P0^6;
#define int8u unsigned char
#define int16u unsigned int
#define FRESELE 0x28
#define ADDR_INDEX 8 // Index to default address bytes in RxTxConf.buf
#define ADDR_COUNT 4 // Number of address bytes
struct UARTBUF
{
int8u Buf[20];
int8u Counter;
int8u NewData;
}Rx;
int8u RFConfig[15] ={
0x00, //接收頻道二有效數據長度
40, //接收頻道一有效數據長度
0x00,0x00,0x00,0x00,0x00, //接收頻道二地址
0x00,0xcd,0xef,0x12,0xaa, //接收頻道一地址
0x83, //32位地址,16位CRC,使能CRC
//bit7~2:ADDR_W,最大40位
//bit1:CRC_L
//Logic 0: 8 bit CRC
//Logic 1: 16 bit CRC
//BIT0:CRC_EN
//Logic 0: On-chip CRC generation/checking disabled
//Logic 1: On-chip CRC generation/checking enabled
0x4f, //ShockBurst模式,250 kbps,16M晶振,0dBm最大功率
//Bit 15:RX2_EN
//Logic 0: One channel receive
//Logic 1: Two channels receive
//Bit 14:
//Communication Mode:
//Logic 0: nRF2401 operates in direct mode.
//Logic 1: nRF2401 operates in ShockBurst. mode
//Bit 13:
//RF Data Rate:
//Logic 0: 250 kbps
//Logic 1: 1 Mbps
//Bit 12-10:Selects the nRF2401 crystal frequency to be used:
// D12 D11 D10 Crystal Frequency [MHz]
// 0 0 0 4
// 0 0 1 8
// 0 1 0 12
// 0 1 1 16
// 1 0 0 20
//Bit 9-8:RF_PWR: Sets nRF2401 RF output power in transmit mode:
// D9 D8 P [dBm]
// 0 0 -20
// 0 1 -10
// 1 0 -5
// 1 1 0
0x04 //2400+4*1=2404MHZ=2.404G,發送模式
//Bit 7 – 1:RF_CH#: Sets the frequency channel the nRF2401 operates on.
//Channelrf = 2400MHZ + RF_CH# * 1.0MHZ
//Bit 0:Set active mode:
//Logic 0: transmit mode
//Logic 1: receive mode
};
int8u RFConfig2[15] ={
0x08, //接收頻道二有效數據長度
0x08, //接收頻道一有效數據長度
0x00,0x00,0x00,0x00,0x00, //接收頻道二地址
0x00,0xdc,0xef,0x12,0xaa, //接收頻道一地址
0x83, //32位地址,16位CRC,使能CRC
//bit7~2:ADDR_W,最大40位
//bit1:CRC_L
//Logic 0: 8 bit CRC
//Logic 1: 16 bit CRC
//BIT0:CRC_EN
//Logic 0: On-chip CRC generation/checking disabled
//Logic 1: On-chip CRC generation/checking enabled
0x4f, //ShockBurst模式,250 kbps,16M晶振,0dBm最大功率
//Bit 15:RX2_EN
//Logic 0: One channel receive
//Logic 1: Two channels receive
//Bit 14:
//Communication Mode:
//Logic 0: nRF2401 operates in direct mode.
//Logic 1: nRF2401 operates in ShockBurst. mode
//Bit 13:
//RF Data Rate:
//Logic 0: 250 kbps
//Logic 1: 1 Mbps
//Bit 12-10:Selects the nRF2401 crystal frequency to be used:
// D12 D11 D10 Crystal Frequency [MHz]
// 0 0 0 4
// 0 0 1 8
// 0 1 0 12
// 0 1 1 16
// 1 0 0 20
//Bit 9-8:RF_PWR: Sets nRF2401 RF output power in transmit mode:
// D9 D8 P [dBm]
// 0 0 -20
// 0 1 -10
// 1 0 -5
// 1 1 0
0x18 //2400+4*1=2404MHZ=2.404G,發送模式
//Bit 7 – 1:RF_CH#: Sets the frequency channel the nRF2401 operates on.
//Channelrf = 2400MHZ + RF_CH# * 1.0MHZ
//Bit 0:Set active mode:
//Logic 0: transmit mode
//Logic 1: receive mode
};
int8u rfAddress[4] = { 0xcd, 0xef, 0x12, 0xaa };
int8u rfAddress2[4] = { 0xdc, 0xef, 0x12, 0xaa };
int8u timer[2]; // Two utility timers used in radio.c
int8u t0lrel, t0hrel;
int8u buf[8];
/*****************************************************************************************
//函數名:void Delay100us(int8u n)
//輸入:時間
//輸出:無
//功能描述:廷時100us
/*****************************************************************************************/
void Delay100us(int8u n)
{
int8u i;
while(n--)
for(i=0;i<35;i++);
}
/*****************************************************************************************
//函數名:delay(unsigned int s)
//輸入:時間
//輸出:無
//功能描述:普通廷時
/*****************************************************************************************/
delay(unsigned int s)
{
unsigned int i;
for(i=0; i<s; i++);
for(i=0; i<s; i++);
for(i=0; i<s; i++);
}
/*****************************************************************************************
//函數名:int8u SpiReadWrite(int8u b)
//輸入:發送的數據
//輸出:收到的數據
//功能描述:SPI發送接收一個字節
/*****************************************************************************************/
int8u SpiReadWrite(int8u 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;
}
/*****************************************************************************************
//函數名:TimerInit()
//輸入:無
//輸出:無
//功能描述:定時器0初始化
/*****************************************************************************************/
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
}
/*****************************************************************************************
//函數名:ResetTimer(int8u n)
//輸入:要復位的計時器
//輸出:無
//功能描述:復位計時器
/*****************************************************************************************/
void ResetTimer(int8u n)
{
ET0 = 0; // Disable Timer0 interrupt
timer[n & 0x01] = 0; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
}
/*****************************************************************************************
//函數名:ReadTimer()
//輸入:要讀的計時器
//輸出:讀出值
//功能描述:讀計時器
/*****************************************************************************************/
unsigned ReadTimer(int8u n)
{
unsigned tmp;
ET0 = 0; // Disable Timer0 interrupt
tmp = timer[n]; // Clear timer[n]
ET0 = 1; // Enable Timer0 interrupt
return tmp;
}
/*****************************************************************************************
//函數名:SciInit()
//輸入:無
//輸出:無
//功能描述:串口初始化
/*****************************************************************************************/
void SciInit(void)
{
TH1 = 0xe6; // 9600@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 &= 0x0F; //led p0.4 p0.5 output
P0_DIR |= 0x48; // P0.3 is input p0.6 input
ES = 0;
EA=1;
}
/*****************************************************************************************
//函數名:SetRxMode()
//輸入:無
//輸出:無
//功能描述:轉為接收模式
/*****************************************************************************************/
void SetRxMode(void)
{
int8u ch;
CS = 1;
Delay100us(0);
ch = FRESELE << 1;
SpiReadWrite(ch | 0x01);
CS = 0;
}
/*****************************************************************************************
//函數名:SetTxMode()
//輸入:無
//輸出:無
//功能描述:轉為發送模式
/*****************************************************************************************/
void SetTxMode(void)
{
int8u ch;
CS = 1;
Delay100us(0);
ch = FRESELE << 1;
SpiReadWrite(ch);
CS = 0;
}
/*****************************************************************************************
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -