📄 main.c
字号:
//更改发射功率,测试距离
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
uchar sent_data;//RAM区缓存数据
uchar sent_temp;
uchar receive_data;
uchar receive_temp;
uchar i,j;
//定义初始字data120,本机地址32bit,发送字符10Byte,功率-20dB
uchar code sent_init_data[]={0x08,0x08,0x00,0x00,0x00,0x33,0x33,0x00,0x33,0x33,0x33,0x33,0x83,0x6F,0x04};
uchar code receive_init_data[]={0x08,0x08,0x00,0xCC,0xCC,0xCC,0xCC,0x00,0xCC,0xCC,0xCC,0xCC,0x83,0x6F,0x05};
uchar code receive_addr[]={0xCC,0xCC,0xCC,0xCC};
uchar code pic_choose[]={0x00,0x01,0x02,0x03};
//定义接口
//发送接收控制口:为1时发送,为0时接收
sbit SR=P2^2;
//sbit PWR=1;
sbit CE=P1^4;
sbit CS=P1^3;//模式选择位
sbit DATA=P1^0;//数据位
sbit CLK=P1^1;//时钟
sbit DR=P1^2;//接至中断0
//LED
sbit LED3=P0^0;//检测单片机接受主机数据成功
sbit LED4=P0^1;//检测PTR4000初始化成功
sbit LED5=P0^2;//检测PTR4000数据的发送
sbit LED6=P0^3;
sbit pic0=P2^3; //接12
sbit pic1=P2^4; //接13
sbit pic2=P2^5; //接14
sbit pic3=P2^6; //接15
//发送pic选择
sbit cs1=P2^1;
sbit cs0=P2^0;
//sent_function
void sent_total(void);
void sent_MCU_init(void);
void sent_PTR4000init(void);
void MCUsent(void);
void re_send(uchar b);
void PTRsent(uchar c);
void delay(unsigned char c);
//receive_function
void receive_total(void);
void receive_MCU_init(void);
void receive_PTR4000init(void);
void receive(void);
uchar change(void);//串并转换
main()
{
if(SR)
sent_total();
else
receive_total();
}
//modle1_sent_function
void sent_total(void)
{
CS=0;
CE=0;
CLK=0;
DATA=0;
sent_MCU_init();//串口初始化
sent_PTR4000init();//PTR4000初始化
while(1)
{
MCUsent();//主机传送数据到51
}
}
void sent_MCU_init(void)
{
P0=0xff;//关闭LED
SCON=0x50;
PCON&=0xef;
TMOD=0x21;
TH1=0xe8; //1200
TL1=0xe8;
TR1=1;
}
void sent_PTR4000init(void)
{
// PWR=1;//配置模式,也可直接接1为连续工作模式,不必写控制字
CS=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//移入模式字
//第二通道数据长度保留,第二通道地址保留,发射接受数据长度为80bit(10Byte),本机地址0xCC,地址长度为32bit,设定CRC16,使能CRC
//速率1M,发射功率为0d,频道号#02,发射模式
for(i=0;i<15;i++)
{
sent_temp=sent_init_data[i];
for(j=0;j<8;j++)
{
CLK=0;
DATA=(bit)(sent_temp&0x80);//先发送高位
sent_temp=_crol_(sent_temp,1);
CLK=1;
}
}
_nop_();
CLK=0;
CS=0;
}
void MCUsent(void)
{
while(1)
{
// while(!RI);
// RI=0;
// sent_data=SBUF; //接受字符
// re_send(sent_data); //回送字符
if(cs1==0)
{
if(cs0==0)
sent_data=pic_choose[0];
else
sent_data=pic_choose[1];
}
else
{
if(cs0==0)
sent_data=pic_choose[2];
else
sent_data=pic_choose[3];
}
P0=sent_data;
CE=1;//设为工作模式,以上数据移入生效
for(i=0;i<4;i++) //移入地址
PTRsent(receive_addr[i]);
PTRsent(sent_data);
CE=0;
CLK=0;
DATA=0;
for(i=0;i<100;i++);
}
}
void re_send(uchar b) //回传
{
SBUF=b;
while(!TI);
TI=0;
// LED3=0; //检测接收成功
}
void PTRsent(uchar c)
{
for(j=0;j<8;j++)
{
CLK=0;
DATA=(bit)(c&0x80);//先发送高位
c=_crol_(c,1);
CLK=1;
}
}
void delay(unsigned char c)
{
unsigned char i,j;
for(j=0;j<c;j++)
for(i=0;i<200;i++);
}
//modle2_receive_function
void receive_total(void)
{
CS=0;
CE=0;
CLK=0;
DATA=0;
receive_MCU_init();//串口初始化
receive_PTR4000init();//PTR4000初始化
receive();//主机传送数据到51
}
void receive_MCU_init(void)
{
P0=0xff;
SCON=0x50;
PCON&=0xef;
TMOD=0x21;//T1产生波特率,T0产生CLK信号
TH1=0xe8; //1200
TL1=0xe8;
TR1=1;
}
void receive_PTR4000init(void)
{
// PWR=1;//配置模式,也可直接接1为连续工作模式,不必写控制字
CS=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
//移入模式字
//第二通道数据长度保留,第二通道地址保留,发射接受数据长度为80bit(10Byte),本机地址0xCC,地址长度为32bit,设定CRC16,使能CRC
//速率1M,发射功率为0d,频道号#02,接收模式
for(i=0;i<15;i++)
{
receive_temp=receive_init_data[i];
for(j=0;j<8;j++)
{
CLK=0;
DATA=(bit)(receive_temp&0x80);//先发送高位
receive_temp=_crol_(receive_temp,1);
CLK=1;
}
}
_nop_();
CLK=0;
CS=0;
}
void receive(void)
{
CE=1;//设为工作模式,以上数据移入生效
//接收数据
while(1)
{
while(DR)
{
receive_data=change();
CLK=0;
//SBUF=receive_data;
P0=receive_data;
//while(!TI);
//TI=0;
// LED3=0;
}//TR为低时数据接受完毕
switch(receive_data)
{
case 0:
pic3=0;
pic2=0;
pic1=0;
pic0=0;
break;
case 1:
pic3=0;
pic2=0;
pic1=0;
pic0=1;
break;
case 2:
pic3=0;
pic2=0;
pic1=1;
pic0=0;
break;
case 3:
pic3=0;
pic2=0;
pic1=1;
pic0=1;
break;
default:
break;
}
}
}
uchar change(void)//串并转换
{
bit buff;
uchar change;
change=0x00;
for(j=0;j<8;j++)
{
CLK=1;
buff=DATA;
if(j==7)
{
if(buff==1)
change+=1;
}
else
{
if(buff==0)
change=_crol_(change,1);
else
{
change+=1;
change=_crol_(change,1);
}
}
CLK=0;
_nop_();
}
// LED5=0;
return change;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -