能利用這段代碼控制繼電器的,(就是電腦通過發送一段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;
- }
- }
- }
復制代碼
|