315m无线模块怎么接收程序以及程序代码

下面给大家整理下315M无线模块的接收程序

315m无线模块怎么接收程序以及程序代码_第1张图片

#include 



#include "string.h"



//液晶块



#define LCM_RW P3_6 //定义引脚



#define LCM_RSP3_5



#define LCM_E P3_7



#define LCM_Data P1



#define Busy 0x80 //用于检测LCM状态字中的Busy标识



typedef int byte;



typedef unsigned int word;



void Read_Temp(void);



void mychar(char,char);



void WriteDataLCM(unsigned char WDLCM);



void WriteCommandLCM(unsigned char WCLCM,BuysC);



unsigned char ReadDataLCM(void);



unsigned char ReadStatusLCM(void);



void LCMInit(void);



void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);



void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);



void Delay5Ms(void);



void Delay400Ms(void);



void delay(word useconds)



{



for(;useconds>0;useconds--);



}



void mychar(char xx,char yy)



{



DisplayOneChar(0,0,0x54);



DisplayOneChar(1,0,0x65);



DisplayOneChar(2,0,0x6D);



DisplayOneChar(3,0,0x70);



DisplayOneChar(4,0,0x65);



DisplayOneChar(5,0,0x72);



DisplayOneChar(6,0,0x61);



DisplayOneChar(7,0,0x74);



DisplayOneChar(8,0,0x75);



DisplayOneChar(9,0,0x72);



DisplayOneChar(10,0,0x65);



DisplayOneChar(11,0,0x3A);



///自定义字符



WriteCommandLCM(0x48, 0); //第一行



WriteDataLCM(0x06);



WriteCommandLCM(0x49, 0); //第2行



WriteDataLCM(0x09);



WriteCommandLCM(0x4a, 0); //第3



WriteDataLCM(0x09);



WriteCommandLCM(0x4b, 0); //第4



WriteDataLCM(0x06);



WriteCommandLCM(0x4c, 0); //第5



WriteDataLCM(0x00);



WriteCommandLCM(0x4d, 0); //第6



WriteDataLCM(0x00);



WriteCommandLCM(0x4e, 0); //第7



WriteDataLCM(0x00);



WriteCommandLCM(0x4f, 0); //第8



WriteDataLCM(0x00);



DisplayOneChar(xx,yy,0x01);



DisplayOneChar(xx+1,yy,0x43);



}



//写数据



void WriteDataLCM(unsigned char WDLCM)



{



ReadStatusLCM(); //检测忙



LCM_Data = WDLCM;



LCM_RS = 1;



LCM_RW = 0;



LCM_E = 0; //若晶振速度太高可以在这后加小的延时



LCM_E = 0; //延时



LCM_E = 1;



}



//写指令



void WriteCommandLCM(unsigned char WCLCM,BuysC) //BuysC为0时忽略忙检测



{



if (BuysC) ReadStatusLCM(); //根据需要检测忙



LCM_Data = WCLCM;



LCM_RS = 0;



LCM_RW = 0;



LCM_E = 0;



LCM_E = 0;



LCM_E = 1;



}



//读状态



unsigned char ReadStatusLCM(void)



{



LCM_Data = 0xFF;



LCM_RS = 0;



LCM_RW = 1;



LCM_E = 0;



LCM_E = 0;



LCM_E = 1;



while (LCM_Data & Busy); //检测忙信号



return(LCM_Data);



}



void LCMInit(void) //LCM初始化



{



LCM_Data = 0;



WriteCommandLCM(0x38,0); //三次显示模式设置,不检测忙信号



Delay5Ms();



WriteCommandLCM(0x38,0);



Delay5Ms();



WriteCommandLCM(0x38,0);



Delay5Ms();



WriteCommandLCM(0x38,1); //显示模式设置,开始要求每次检测忙信号



WriteCommandLCM(0x08,1); //关闭显示



WriteCommandLCM(0x01,1); //显示清屏



WriteCommandLCM(0x06,1); // 显示光标移动设置



WriteCommandLCM(0x0C,1); // 显示开及光标设置



}



//按指定位置显示一个字符



void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)



{



Y &= 0x1;



X &= 0xF; //限制X不能大于15,Y不能大于1



if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;



X |= 0x80; //算出指令码



WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码



WriteDataLCM(DData);



}



//按指定位置显示一串字符 ***原来的遇到空格0x20就不显示***



void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)



{



unsigned char ListLength,j;



ListLength = strlen(DData);



Y &= 0x1;



X &= 0xF; //限制X不能大于15,Y不能大于1



if (X <= 0xF) //X坐标应小于0xF



{



for(j=0;j>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;



}



}



}



}



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



//接收程序 结束



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



#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();



//send_string_com("ok",2);



//lcd_reset();



//lcd_power_on();



//lcd_self_test();



while(1){



if (jiffies - last_jiffies >= 20){//每次中断为50us,所以要20次才为1ms



last_jiffies = jiffies;



heart_beat_timer--;



if (send_delay)



send_delay--;



}



if (heart_beat_timer == 0){



heart_beat_timer = HEART_BEAT_INTERVAL;



while(1) {recv();if (recv_stat == RECV_SUCCESS)



{



P0_0=~P0_0;



send_string_com(recv_buf,strlen(recv_buf));



Display_T();



recv_stat = 0;break;}



recv_stat = 0;



}



}



/*#if 0



if (send_stat == 0){



//碰撞测试



START_SEND(8);



send_buf[1]='T';



send_buf[2]='e';



send_buf[3]='s';



send_buf[4]='t';



send_buf[5]='O';



send_buf[6]='k';



send_buf[7]='!';



lcd_out_string("sending:");



//send_string_com(send_buf,strlen(send_buf));



printhex(send_buf[0]);



printhex(send_buf[1]);



}



#endif



*/



wirless();



if (send_stat == SEND_FAILED){



send_stat = 0;



}else if (send_stat == SEND_SUCCESS){



send_stat = 0;



P2_3=~P2_3;



}



//recv();



//send_string_com("start",5);



/*while(1) {recv();if (recv_stat == RECV_SUCCESS)



{



P0_0=~P0_0;



send_string_com(recv_buf,strlen(recv_buf));} }*/



/* if (recv_stat == RECV_SUCCESS){



//send_string_com("test1",5);



#if 1



//unsigned int i;



//for(i=0; i

你可能感兴趣的:(科普,单片机,嵌入式硬件,51单片机,物联网)