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

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

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

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

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;
}
}
}
}

//=============================================================
//接收程序 结束
//=============================================================

#define SEND_DELAY_TIME 20//防碰撞延迟(单位:毫秒)
unsigned char send_delay = SEND_DELAY_TIME;
#define VALID_PULS_COUNT 10
unsigned char valid_puls_counts = VALID_PULS_COUNT;

//碰撞检测在该函数中实现.由于无线模块本身的限制,碰撞检测为非完全检测,只能在发送前
//检测是否有其它单元在使用信道,在发送过程中受到的碰撞干扰是无法检测到的.经测试,效果还行
void wirless(){
if (send_stat == SEND_PROGRESSING && send_delay == 0){
send();
if (send_stat == SEND_SUCCESS)
send_delay = SEND_DELAY_TIME;
}else{
recv();
if (recv_step > HEAD_NEED_RECIVED)//如果检测到当前有其它单元正在发送,继续等待
send_delay = SEND_DELAY_TIME;
else{
if(TEST_PULS(w_data, PULS_0_WIDTH)//如果检测到当前有其它单元正在发送,继续等待
|| TEST_PULS(w_data, PULS_1_WIDTH)
//|| TEST_PULS(w_data, PULS_HEAD_WIDTH)
|| TEST_PULS(w_data, PULS_START_WIDTH)
//|| TEST_PULS(w_data, PULS_STOP_WIDTH)
){
if (valid_puls_counts)
valid_puls_counts--;
else
send_delay = SEND_DELAY_TIME;
}else{
valid_puls_counts = VALID_PULS_COUNT;
}
}
}
}


void Display_T()
{
DisplayOneChar(9,1,recv_buf[5]);
DisplayOneChar(8,1,recv_buf[4]);
DisplayOneChar(7,1,recv_buf[3]);
DisplayOneChar(6,1,recv_buf[2]);
DisplayOneChar(5,1,recv_buf[1]);
DisplayOneChar(0,0,0x54);
}

#define SCMD_SET_PORT 0x51
#define SCMD_GET_PORT 0x52

#define SCMD_RESET 0x61

#define SCMD_LCD_OUTSTR 0x71
#define SCMD_LCD_RESET 0x72
#define SCMD_LCD_SETXY 0x73
#define SCMD_LCD_SETLINE 0x74
#define SCMD_LCD_CLEAR 0x75

#define SRESP_GET_PORT_ERROR 0x41
#define SRESP_SET_PORT_ERROR 0x42

#define HEART_BEAT_INTERVAL 200//心跳间隔 X / 1000 秒
unsigned int heart_beat_timer = HEART_BEAT_INTERVAL;
unsigned int last_jiffies=0;

void main(){
Delay400Ms(); //启动等待,等LCM讲入工作状态
LCMInit(); //LCM初始化
Delay5Ms(); //延时片刻(可不要)
mychar(10,1); //显示 自定义字符
init_serialcomm();
P0=0x0ff;
P2=0x00;
sys_init();

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

网站地图

Top