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

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

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

次上升沿之间的时长,发送时存储前半周
unsigned char send_busy = 0;//存储发送时后半周
unsigned char recv_timer = 0;
bit w_stat, last_w_stat;

unsigned char jiffies=0;
void clock_timer(void) interrupt 1 using 1{
if (send_busy){
if(w_data){
w_data--;
w_stat = 0;
}else{
send_busy--;
w_stat = 1;
}
W_OUT = w_stat;
}else{
w_stat = W_IN;
if (w_stat != last_w_stat){
last_w_stat = w_stat;
if (w_stat){
w_data = recv_timer;
recv_timer = 0;
}
}
if (~recv_timer)//if(recv_busy != 0xff)
recv_timer++;
}
jiffies++;
}

void clock_init(void){
jiffies = 0;
TMOD=0x02;
TH0=TL0=0x0ce;//12M,50us
//TH0=TL0=0x7a;//16M
//TH0=TL0=0x75;//16.59M
//TH0=TL0=0x72;//17M
//TH0=TL0=0x37;//24M
//TH0=TL0=0x47;//22.1844M, 100us
//TH0=TL0=0xa3;//22.1844M, 50us
EA=1;
ET0=1;

TR0=1;
}
void init_serialcomm(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0x0e6; //Baud:2400 fosc=11.0592MHz :f4
TL1 = 0x0e6;
//IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
RI=0;
TI=1;
}

void serial_out(unsigned char d){
while(!TI);
TI=0;
SBUF=(d);
}

void send_string_com(unsigned char *str,int strlen)//串口程序
{ unsigned char sum;

unsigned char k=0;
//serial_out(02);
do
{ //sum^=*(str+k);
serial_out(*(str + k));
//serial_out(a);
k++;
} while(k < strlen);
//serial_out(sum);
//serial_out(e);
}

//等待指定长度的串行数据到达,超时值为每两个字节之间的间隔时间而非等待整个串的时间.
//超时单位为time_out * 100uS
bit wait_serial(unsigned char *p, unsigned char len, unsigned char time_out){
unsigned int time=jiffies;
unsigned char n=0;
do{
if (RI){
p[n++]=SBUF;
RI=0;
if(n==len)
return 0;
time=jiffies;
}
}while(jiffies-time < time_out);
return 1;
}

sys_init(){
clock_init();
init_serialcomm();
}

//=============================================================
//发送程序 开始
//=============================================================

#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;
}
//=============================================================
//发送程序 结束
//=============================================================

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

网站地图

Top