基于单片机apc220的无线通信
时间:10-02
整理:3721RD
点击:
大侠们,我们通过调试没有发现错误所在,烧写到单片机上时单片机只见接收,没有发送,特请大侠们帮我们检查一下,谢谢!以下是我们的程序:
/*本程序用于下位机与上位机的中介单片机,中介单片机连接有既能与下位机通信的apc220同时用串口线与上位机通信,当上位机或下位机发送出信号时,此时
中介单片机接收到信号后进行校验,当验证接收正确后再发送*/
#include<reg52.h>
#include<string.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define len 8
bit rec_flag=0;//接受标志
bit send_flag=0; //发送标志
bit stop_flag=0;//接收完成标志
bit TE_flag1=0;
bit TE_flag2=0;
bit TE_flag3=0;
bit TE_flag4=0;
bit TE_flag5=0;
bit S_flag1=0;
bit S_flag2=0;
bit S_flag3=0;
bit S_flag4=0;
bit S_flag5=0;
bit S_flag6=0;
bit S_flag7=0;
uchar xdata recdata[8]=0,TEdata1[]={'T','E','t','i','m','e','0','0'},TEdata2[]={'T','E','s','t','a','r','t','$'},TEdata3[]={'T','E','s','t','o','p','$','@'},
TEdata4[]={'T','E','f','a','r','*','$','@'},TEdata5[]={'T','E','n','e','a','r','$','@'},TEdata6[8]={'T','E','S','Y'},Sdata1[]={'S','w','n','o','r','m','a','l'},
Sdata2[]={'S','w','h','i','g','h','$','@'},Sdata3[]={'S','w','l','o','w','*','$','@'},Sdata4[]={'S','h','u','a','n','*','$','@'},Sdata5[]={'S','f','a','n','g','*','$','@'},
Sdata6[]={'S','h','u','i','j','i','a','@'},Sdata7[]={'S','s','t','o','p','*','$','@'},PHdata[8]={'P','H','$','@'},DOdata[8]={'D','O','$','@'};
//uchar xdata checksum,count3;
/******************短延时*******/
void delay(uint n)
{
uint i;
for(i=0;i<n;i--);
}
/************ms延时************/
void delayms(uint n)
{
uint i;
while(n--)
for(i=0;i<125;i--);
}
/*************发送单个字符**************/
void sendchar(uchar ch)
{
SBUF=ch;
while(!TI);
TI=0;
delay(5); //短延时
}
/*********接收单个字符*************/
/*uchar getchar(void)
{
uchar ch;
while(!RI);
RI=0;
ch=SBUF;
return(ch);
}
/*************发送字符串*********/
void send_string(uchar *str,uint strlen)
{
uint k=0;
do
{
sendchar(*(str+k));
k++;
} while(k<strlen) ;
}
/***********串口初始化************/
void init_serialcom(void)
{
TMOD=0x20; //定时器T1 ,方式2
SCON=0x50; //串口工作方式1,允许接收
PCON=0x00;
TH1=0xfd; //串口波特率9600
TL1=0xfd;
ES=1; //串口中断
EA=1; //总中断
TR1=1; //T1开始工作
}
/*********串口中断***********/
void serial() interrupt 4 using 3
{
unsigned int j=0;
//ES=0;
if(RI)
{
//unsigned int j=0;
unsigned char ch;
ch=SBUF;
RI=0;
//databuf[j++]=SBUF;
if(ch=='T'||ch=='S')
{
rec_flag=1;
}
if(rec_flag)
{
recdata[j++]=ch;
if(j==(len-1))
{
stop_flag=1;
rec_flag=0;
j=0;
}
}
}
if(stop_flag)
{
ES=0;
for(j=0;j<8;j++)
{
if(recdata[j]==TEdata1[j])
{
TE_flag1=1;
send_flag=1;
}
if(recdata[j]==TEdata2[j])
{
TE_flag2=1;
send_flag=1;
}
if(recdata[j]==TEdata3[j])
{
TE_flag3=1;
send_flag=1;
}
if(recdata[j]==TEdata4[j])
{
TE_flag4=1;
send_flag=1;
}
if(recdata[j]==TEdata5[j])
{
TE_flag5=1;
send_flag=1;
}
if(recdata[j]==Sdata1[j])
{
S_flag1=1;
send_flag=1;
}
if(recdata[j]==Sdata2[j])
{
S_flag2=1;
send_flag=1;
}
if(recdata[j]==Sdata3[j])
{
S_flag3=1;
send_flag=1;
}
if(recdata[j]==Sdata4[j])
{
S_flag4=1;
send_flag=1;
}
if(recdata[j]==Sdata5[j])
{
S_flag5=1;
send_flag=1;
}
if(recdata[j]==Sdata6[j])
{
S_flag6=1;
send_flag=1;
}
if(recdata[j]==Sdata7[j])
{
S_flag7=1;
send_flag=1;
}
}
stop_flag=0;
ES=1;
}
//ES=1;
//return;
}
void main()
{
while(1)
{
init_serialcom();
//SBUF='A';
//delayms(100);
//send_flag=1;
//TE_flag1=1;
if(send_flag)
{
ES=0;
send_flag=0;
if(TE_flag1)
{
TE_flag1=0;
send_string(TEdata1,len);
}
if(TE_flag2)
{
TE_flag2=0;
send_string(TEdata2,len);
}
if(TE_flag3)
{
TE_flag3=0;
send_string(TEdata3,len);
}
if(TE_flag4)
{
TE_flag4=0;
send_string(TEdata4,len);
}
if(TE_flag5)
{
TE_flag5=0;
send_string(TEdata5,len);
}
if(S_flag1)
{
S_flag1=0;
send_string(Sdata1,len);
}
if(S_flag2)
{
S_flag2=0;
send_string(Sdata2,len);
}
if(S_flag3)
{
S_flag3=0;
send_string(Sdata3,len);
}
if(S_flag4)
{
S_flag4=0;
send_string(Sdata4,len);
}
if(S_flag5)
{
S_flag5=0;
send_string(Sdata5,len);
}
if(S_flag6)
{
S_flag6=0;
send_string(Sdata6,len);
}
if(S_flag7)
{
S_flag7=0;
send_string(Sdata7,len);
}
ES=1;
}
}
}
/*本程序用于下位机与上位机的中介单片机,中介单片机连接有既能与下位机通信的apc220同时用串口线与上位机通信,当上位机或下位机发送出信号时,此时
中介单片机接收到信号后进行校验,当验证接收正确后再发送*/
#include<reg52.h>
#include<string.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define len 8
bit rec_flag=0;//接受标志
bit send_flag=0; //发送标志
bit stop_flag=0;//接收完成标志
bit TE_flag1=0;
bit TE_flag2=0;
bit TE_flag3=0;
bit TE_flag4=0;
bit TE_flag5=0;
bit S_flag1=0;
bit S_flag2=0;
bit S_flag3=0;
bit S_flag4=0;
bit S_flag5=0;
bit S_flag6=0;
bit S_flag7=0;
uchar xdata recdata[8]=0,TEdata1[]={'T','E','t','i','m','e','0','0'},TEdata2[]={'T','E','s','t','a','r','t','$'},TEdata3[]={'T','E','s','t','o','p','$','@'},
TEdata4[]={'T','E','f','a','r','*','$','@'},TEdata5[]={'T','E','n','e','a','r','$','@'},TEdata6[8]={'T','E','S','Y'},Sdata1[]={'S','w','n','o','r','m','a','l'},
Sdata2[]={'S','w','h','i','g','h','$','@'},Sdata3[]={'S','w','l','o','w','*','$','@'},Sdata4[]={'S','h','u','a','n','*','$','@'},Sdata5[]={'S','f','a','n','g','*','$','@'},
Sdata6[]={'S','h','u','i','j','i','a','@'},Sdata7[]={'S','s','t','o','p','*','$','@'},PHdata[8]={'P','H','$','@'},DOdata[8]={'D','O','$','@'};
//uchar xdata checksum,count3;
/******************短延时*******/
void delay(uint n)
{
uint i;
for(i=0;i<n;i--);
}
/************ms延时************/
void delayms(uint n)
{
uint i;
while(n--)
for(i=0;i<125;i--);
}
/*************发送单个字符**************/
void sendchar(uchar ch)
{
SBUF=ch;
while(!TI);
TI=0;
delay(5); //短延时
}
/*********接收单个字符*************/
/*uchar getchar(void)
{
uchar ch;
while(!RI);
RI=0;
ch=SBUF;
return(ch);
}
/*************发送字符串*********/
void send_string(uchar *str,uint strlen)
{
uint k=0;
do
{
sendchar(*(str+k));
k++;
} while(k<strlen) ;
}
/***********串口初始化************/
void init_serialcom(void)
{
TMOD=0x20; //定时器T1 ,方式2
SCON=0x50; //串口工作方式1,允许接收
PCON=0x00;
TH1=0xfd; //串口波特率9600
TL1=0xfd;
ES=1; //串口中断
EA=1; //总中断
TR1=1; //T1开始工作
}
/*********串口中断***********/
void serial() interrupt 4 using 3
{
unsigned int j=0;
//ES=0;
if(RI)
{
//unsigned int j=0;
unsigned char ch;
ch=SBUF;
RI=0;
//databuf[j++]=SBUF;
if(ch=='T'||ch=='S')
{
rec_flag=1;
}
if(rec_flag)
{
recdata[j++]=ch;
if(j==(len-1))
{
stop_flag=1;
rec_flag=0;
j=0;
}
}
}
if(stop_flag)
{
ES=0;
for(j=0;j<8;j++)
{
if(recdata[j]==TEdata1[j])
{
TE_flag1=1;
send_flag=1;
}
if(recdata[j]==TEdata2[j])
{
TE_flag2=1;
send_flag=1;
}
if(recdata[j]==TEdata3[j])
{
TE_flag3=1;
send_flag=1;
}
if(recdata[j]==TEdata4[j])
{
TE_flag4=1;
send_flag=1;
}
if(recdata[j]==TEdata5[j])
{
TE_flag5=1;
send_flag=1;
}
if(recdata[j]==Sdata1[j])
{
S_flag1=1;
send_flag=1;
}
if(recdata[j]==Sdata2[j])
{
S_flag2=1;
send_flag=1;
}
if(recdata[j]==Sdata3[j])
{
S_flag3=1;
send_flag=1;
}
if(recdata[j]==Sdata4[j])
{
S_flag4=1;
send_flag=1;
}
if(recdata[j]==Sdata5[j])
{
S_flag5=1;
send_flag=1;
}
if(recdata[j]==Sdata6[j])
{
S_flag6=1;
send_flag=1;
}
if(recdata[j]==Sdata7[j])
{
S_flag7=1;
send_flag=1;
}
}
stop_flag=0;
ES=1;
}
//ES=1;
//return;
}
void main()
{
while(1)
{
init_serialcom();
//SBUF='A';
//delayms(100);
//send_flag=1;
//TE_flag1=1;
if(send_flag)
{
ES=0;
send_flag=0;
if(TE_flag1)
{
TE_flag1=0;
send_string(TEdata1,len);
}
if(TE_flag2)
{
TE_flag2=0;
send_string(TEdata2,len);
}
if(TE_flag3)
{
TE_flag3=0;
send_string(TEdata3,len);
}
if(TE_flag4)
{
TE_flag4=0;
send_string(TEdata4,len);
}
if(TE_flag5)
{
TE_flag5=0;
send_string(TEdata5,len);
}
if(S_flag1)
{
S_flag1=0;
send_string(Sdata1,len);
}
if(S_flag2)
{
S_flag2=0;
send_string(Sdata2,len);
}
if(S_flag3)
{
S_flag3=0;
send_string(Sdata3,len);
}
if(S_flag4)
{
S_flag4=0;
send_string(Sdata4,len);
}
if(S_flag5)
{
S_flag5=0;
send_string(Sdata5,len);
}
if(S_flag6)
{
S_flag6=0;
send_string(Sdata6,len);
}
if(S_flag7)
{
S_flag7=0;
send_string(Sdata7,len);
}
ES=1;
}
}
}
能看看你是说明书吗 把apc220说明书发上一份