标题:
通过电脑发送数据控制单片机继电器的程序求助
[打印本页]
作者:
代码虾1
时间:
2018-9-7 10:52
标题:
通过电脑发送数据控制单片机继电器的程序求助
能利用这段代码控制继电器的,(就是电脑通过发送一段
7E 80 00 01 00 00 80 AA 00 01 01 00 DE 62 7E
的指令来控制继电器。
请大神指导啊)
#include "reg52.h"
#include "init.h"
#include "uart.h"
#include "delay.h"
#include "ad.h"
#include "flash.h"
#include "iic.h"
//串口数据的格式
#define HEAD 0XF1 //帧头
#define VERSION 0X15 //发送版本
#define YUAN 0XB1 //源
#define FH_VERSION 0XB1 //接收版本
#define FH_YUAN 0XBF //接收版本
#define ZI 0XD1 // 控制字
#define LEN1 0X28 //24 =4+20+1
uint xdata num=0;
uint xdata n;
extern uchar xdata buf[35];
extern uchar i;
uchar uart1_flag=0;
//数组定义
uchar xdata fs_buf[46];
uchar xdata rbuf[]={
0x7E,0x80, 0x00, 0x01,
0x00, 0x00, 0x80, 0xAA,
0x00, 0x01, 0x01, 0x00,
0xDE, 0x62, 0x7E};
uchar xdata uart2_rbuf[46];
void uart1_fs();
void uart2_fs();
void receive_deal();
sbit jdq1=P0^0;
sbit jdq2=P0^1;
void main()
{
Time_init(); //定时器初始化
PCA_init(); //pca端口初始化
InitUart() ;
EA=1;
ES=1;
jdq1=jdq2=0;
RI=1;
while(1)
{
jdq1=1;
mDelay(100);
}
}
void uart_js() interrupt 4 //RI==1时执行串口中断
{
static unsigned char count;
uchar xdata receive1_xorg=0;
if(RI==1)
{
RI=0;
rbuf[count]=SBUF; //保存数据
if(uart1_flag!=0)
{
count=0;
return;
}
count++;
if((count==1)&&(rbuf[0]!=0x7E))//0 0xf1
{
count=0;
jdq1=!jdq1;
}
else if((count==2)&&(rbuf[1]!=0x80)) //1 0xb1
{
count=0;
}
else if((count==3)&&(rbuf[2]!=0x00)) //1 0xbf
{
count=0;
}
else if((count==4)&&(rbuf[3]!=0x01)) //rbuf[3]
{
count=0;
}
else if((count==5)&&(rbuf[4]!=0x00)) //rbuf[4]
{
count=0;
}
else if(count==6&&rbuf[5]==(rbuf[0]^rbuf[1]^rbuf[2]^rbuf[3]^rbuf[4])) //count=6 实际接收6个字节 0-5 rbuf[5]
{
count=0 ;
uart1_flag=rbuf[3];
ES=0;
}
else if(count==7&&(rbuf[6]==(rbuf[0]^rbuf[1]^rbuf[2]^rbuf[3]^rbuf[4]^rbuf[5]))) //count=7 实际接收7个字节 0-6
{
count=0 ;
uart1_flag=rbuf[3];
ES=0;
}
else if(count==8&&(rbuf[7]==(rbuf[0]^rbuf[1]^rbuf[2]^rbuf[3]^rbuf[4]^rbuf[5]^rbuf[6]))) //count=7 实际接收7个字节 0-6
{
count=0 ;
uart1_flag=rbuf[3];
ES=0;
}
// else if(count>8&&count<45) //6-42
// {
// }
// else if(count==45) //0-44
// {
// receive1_xorg=0;
// count=0;
// for (n=0;n<44;n++) //0-43
// {
// receive1_xorg^= rbuf[n] ;
// }
// if(rbuf[44]==receive1_xorg) //通讯模块采集数据
// {
// uart1_flag=rbuf[3];
// ES=0;
// }
// }
else if(count>8)
{
count=0;
}
}
}
复制代码
作者:
代码虾1
时间:
2018-9-7 11:41
搞了半天也没搞好啊,请大神给个建议或者方法吧
作者:
小猫猫爱吃鱼
时间:
2018-9-7 14:38
你好!
1、你没有说明,发送数据的格式含义
2、你发送的数据和你定义的串口数据格式不一致
作者:
代码虾1
时间:
2018-9-7 15:51
小猫猫爱吃鱼 发表于 2018-9-7 14:38
你好!
1、你没有说明,发送数据的格式含义
2、你发送的数据和你定义的串口数据格式不一致
具体在哪里还请说明
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1