微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机+315M无线射频模块发射程序

51单片机+315M无线射频模块发射程序

时间:11-30 来源:互联网 点击:

#define PULS_0_WIDTH 8//低电平脉宽
#define PULS_1_WIDTH 16//高电平脉宽
#define PULS_HEAD_WIDTH 8//前导信号脉宽
#define PULS_START_WIDTH 24//引导信号脉宽
#define PULS_STOP_WIDTH 8//结束码脉宽

#define PULS_HEAD_COUNTS 16//前导信号长度

unsigned char send_buf[16];
#define send_byts send_buf[0]//剩余字节数,发送完后为0
unsigned char sending_byte;//当前正在发送的字节
unsigned char send_byte_p;//已发送字节数(含正在发送的字节)
unsigned char send_bit_p;//当前正在发送的字节已发送位数(含正在发送的位)

#define SEND_PROGRESSING 0x41
#define SEND_FAILED 0x21
#define SEND_SUCCESS 0x31
unsigned char send_stat = 0;//发送程序当前状态,为0时正常
unsigned char head_counts;//前导信号计数器(包括引导信号)

void start_send(){
send_byte_p = 0;
send_bit_p = 0;
send_stat = SEND_PROGRESSING;
head_counts = 16;
}

#define START_SEND(byts) send_buf[0]=byts;send_byts=byts;start_send()

//发送前应清除send_byte_p,send_bit_p,send_stat,并设置send_byts
//发送过程中可根据send_byts的值得到剩余字节数,根据send_byte_p的值得到已发送字节数.注意,将正在发送的字节当作已发送完毕.
//发送过程中可根据send_stat的值得到发送状态.
//发送完成后,send_byts和send_bit_p的值都为0.
#define SEND_PULS(x) w_data=send_busy=(x>>1)
void send(){
//下面两行在wirless()中已经执行过了,所以无需写上
//if (send_stat != SEND_PROGRESSING)
//return;

if (!send_busy){
if(send_byts || send_bit_p){
if (head_counts){
head_counts--;
if(head_counts)
SEND_PULS(PULS_HEAD_WIDTH);
else
SEND_PULS(PULS_START_WIDTH);
}else{
if (send_bit_p == 0){
sending_byte = send_buf[send_byte_p];
send_byte_p++;
send_byts--;
send_bit_p = 8;
}

if(sending_byte & 0x80){
SEND_PULS(PULS_1_WIDTH);
}else{
SEND_PULS(PULS_0_WIDTH);
}

sending_byte <= 1;
send_bit_p--;
}
}else{
SEND_PULS(PULS_STOP_WIDTH);
send_stat = SEND_SUCCESS;
}
}
return;
}
//=============================================================
//发送程序 结束
//=============================================================

//=============================================================
//接收程序 开始
//=============================================================

unsigned char recv_buf[16];
#define recv_byts recv_buf[0]//应收到字节数,由每个包的第一个字节指定
unsigned char recving_byte;//当前正在接收的字节
unsigned char recv_byte_p;//已收到字节数(不含正在接收的字节)
unsigned char recv_bit_p;//当前正在接收的字节等待接收位数(不含正在接收的位)

#define RECV_FAILED 0x31
#define RECV_SUCCESS 0x41

unsigned char recv_stat = 0; //接收程序当前状态,为0时正常
unsigned char recv_step = 0;//引导脉冲标志,为0时等待引导,为1时等待数据

#define TEST_PULS(puls_in, puls_type) (puls_in > puls_type - PULS_0_WIDTH / 2 && puls_in < puls_type + PULS_0_WIDTH / 2)

#define HEAD_NEED_RECIVED 8
void recv(){
unsigned puls_width;

if ((recv_stat == RECV_SUCCESS) || !w_data)
return;

puls_width = w_data;
w_data = 0;

#if 0//输出脉宽
serial_out(puls_width);
//printhex(puls_width);
#endif

if (recv_step < HEAD_NEED_RECIVED){
if(TEST_PULS(puls_width, PULS_HEAD_WIDTH)){
recv_step++;
}else{
recv_step = 0;
}
}else if (recv_step == HEAD_NEED_RECIVED){
if(TEST_PULS(puls_width, PULS_START_WIDTH)){
serial_out(0xbb);
recv_byte_p = 0;
recv_bit_p = 8;
recv_stat = 0;
recv_step++;
}else{
if(!TEST_PULS(puls_width, PULS_HEAD_WIDTH)){
recv_step = 0;
}
}
}else{
//serial_out(puls_width);
recving_byte <= 1;
if(TEST_PULS(puls_width, PULS_0_WIDTH)){
recving_byte &= 0xfe;
}else if(TEST_PULS(puls_width, PULS_1_WIDTH)){
recving_byte |= 1;
}else{
serial_out(puls_width);
recv_step = 0;
serial_out(0xaa);
return;
}
recv_bit_p--;
if(recv_bit_p == 0){//接收完一字节
recv_bit_p = 8;
//serial_out(recving_byte);//输出接收到的字符
recv_buf[recv_byte_p] = recving_byte;
recv_byte_p++;
if(recv_byte_p == recv_byts){
recv_step = 0;
recv_stat = RECV_SUCCESS;
}
}
}
}

//========================================

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top