/* wireless.c ================================================================================================
*=============================================================================================================
*/
#include <reg24e1.h>
#define uchar unsigned char
#define uint unsigned int
#define ADDR_INDEX 8 // 在 RFConfig.buf 地址字节的指针
#define ADDR_COUNT 4 // 地址字节的数目
#define num_buf 24 // 缓冲区的大小
#define num_RTcon 15
//定义io端口
sbit p00=P0^0;
sbit p01=P0^1;
sbit p02=P0^2;
sbit p03=P0^3;
sbit p04=P0^4;
sbit p05=P0^5;
sbit p06=P0^6;
sbit p07=P0^7;
bit flag_int1=0;//extern interrupte 1
bit flag_mORs=1; //1:master,transmitter;
//0:slave,reciever
volatile unsigned char idata RxBuf[num_buf]; //rec buf
volatile unsigned char idata TxBuf[num_buf]; //tran buf
volatile unsigned char idata Rxcheck[num_buf]; //rec buf
const RxTxConf[15] = //发射接收配置
{
0xc0, 0xc0, 0x00, 0xab, 0xbc, 0x13, 0x35, 0x00,
0xaa, 0xbb, 0x12, 0x34, 0x80, 0xef, 0x04
};
void Delay100us(volatile uint n)//delay 100US
{
unsigned char i;
while(n--)
for(i=0;i<35;i++)
;
}
//--------------------------------led flash
void LED(void)
{
unsigned char i;
for(i=1;i<3;i++)
{
p07=1;
Delay100us(2540);
p07=0;
Delay100us(2540);
}
}
//----------------------------------------------SPI 读写函数
unsigned char SpiReadWrite(unsigned char b)
{
EXIF &= ~0x20; // 清除 SPI 中断
SPI_DATA = b; // 发送字节到SPI数据寄存器
while((EXIF & 0x20) == 0x00) // 一直等到SPI完成通信
;
return SPI_DATA;
}
//---------------------------------------------------set 24e1 to RX
void SetRxMode(void)
{
CS = 1;
Delay100us(0);
SpiReadWrite(RxTxConf[num_RTcon-1] | 0x01);
CS = 0;
}
void SetTxMode(void) //set 24e1 to TX
{
CS = 1;
Delay100us(0);
SpiReadWrite(RxTxConf[num_RTcon-1]);
CS = 0;
}
//--------------------------------------------------------//初始化函数
void Init(void)
{
unsigned char i;
for(i=0;i<num_buf;i++)
{
TxBuf[i]=i;
Rxcheck[i]=i;
}
P0_DIR = 0x00;
P0_ALT = 0x00;
//************ SPI:
SPICLK = 0x00; // 最大SPI时钟 (=CLK/8)
SPI_CTRL = 0x02; // 连接SPI到射频CH1
//************ INT1:
P0_DIR |= 0x10;
P0_ALT |= 0x10;
// IT1 = 0;//低电平有效
EX1 = 1;//外部中断允许
// PX1=1;//高优先级
flag_int1=0;
// **********RADIO:
PWR_UP = 1;
Delay100us(40); // 等待大于3ms
CE = 0;
CS = 1; // RF SPI CE=0 CS = 1 配置使用
Delay100us(0);
for(i=0;i<num_RTcon;i++)
SpiReadWrite(RxTxConf[i]);
CS = 0;
//*************
// IE = 0x95;
EA = 1; // 开启全局中断
}
//-----------------------------------------------------uart send ont byte
void tbyte(uchar temp_T_byte)
{
SBUF = temp_T_byte;
while(TI==0);
TI=0;
}
//-----------------------------------------------------rf send ont packet
void TxPacket(void)//发送包
{
unsigned char i;
CE = 1;
Delay100us(0);
// 所有包开始地址
for (i=0;i<ADDR_COUNT;i++)
{
SpiReadWrite(RxTxConf[ADDR_INDEX+i]);
}
/*从发送缓冲区读取包和通过SPI在线指令写到射频*/
for (i=0;i<num_buf;i++)
{
EXIF &= ~0x20; // 清除SPI中断
SPI_DATA = TxBuf[i]; // 发送数据到SPI数据寄存器
while((EXIF & 0x20) == 0x00) // 一直等到SPI完成通信
;
}
CE = 0;
Delay100us(30);
}
//-----------------------------------------------------rf rec packet
void RxPacket(void)
{
unsigned char index_Rbuf=0;
while(DR1)
{
EXIF &= ~0x20; // 清除SPI中断
SPI_DATA = 0; // 写空字节到SPI数据寄存器
while((EXIF & 0x20) == 0x00) // 一直等到SPI完成接收
;
RxBuf[index_Rbuf] = SPI_DATA;//save to RX buf
tbyte( RxBuf[index_Rbuf]); //send to pc by uart
index_Rbuf+=1;
}
index_Rbuf=0;
}
uchar checkrec(void)
{
unsigned char i,j;
for(i=0,j=0;i<num_buf;i++)
if(Rxcheck[i]=RxBuf[i])
j+=1;
if(j==23)
return 1;
else return 0;
}
//=----------------------------------------------- interrupt service
void service_int1() interrupt 2
{
EX1=0;
flag_int1=1;
}
//---------------------------------------------------main function
void main(void)
{
unsigned char i;
Init(); // 初始化
LED();
SetRxMode();
P0=0;
CE = 1;
Delay100us(30);
while(1)
{
if(flag_int1)
{
SetTxMode();
//while(1)
TxPacket();
SetRxMode();
CE = 1;
Delay100us(30);
flag_int1=0;
Delay100us(9250);
EX1=1;
}
if(DR1)
{
RxPacket();
if(checkrec())
{
i+=1;
P0=0x40|(i/10);
P0=0x20|(i%10);
if(i>98)
i=0;
}
}
}
}