代码片段C单片机51单片机接收AT指令(字符串)功能实现
钱涛开发背景
51单片机需要接收电脑端发送的指令,来控制继电器的通断,从而控制连接电脑端的USB数据线的通断。
指令配置如下
1 2
| AT+USB=1 // 连接usb AT+USB=0 // 断开usb
|
功能实现
具体代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include <reg52.h> #include <string.h>
#define uchar unsigned char #define uint unsigned int
uint dataCount; uint receiveStatus; uchar receiveStr[10];
uint checkReceiveStatus(); void send_str_com(unsigned char *sendStr);
void init() { TMOD = 0x20; PCON = 0x80; TH1 = 0xFD; TL1 = 0xFD; TR1 = 1; REN = 1; SM0 = 0; SM1 = 1; EA = 1; ES = 1; }
void main() { init(); send_str_com("init ok!"); receiveStatus = -1; while (1) { if (checkReceiveStatus() == 1) { send_str_com(receiveStr);
if (0 == strncmp(receiveStr, "AT+USB=1", 8)) { send_str_com("connect usb"); P1 = 0xff; } else if (0 == strncmp(receiveStr, "AT+USB=0", 8)) { send_str_com("break usb"); P1 = 0x00; } else { receiveStatus = -1; memset(receiveStr, 0, 10); } } } }
void ser() interrupt 4 using 3 { if (RI) { RI = 0; if (receiveStatus == -1) { if (SBUF != 'A') { receiveStatus = 0; dataCount = 0; } else { receiveStatus = 1; dataCount = 0; memset(receiveStr, 0, sizeof(receiveStr[10])); receiveStr[dataCount] = SBUF; dataCount++; } } else if (receiveStatus == 1) { if (dataCount > 10) { receiveStatus = 0; dataCount = 0; } else { receiveStr[dataCount] = SBUF; dataCount++; } } } }
uint checkReceiveStatus() { if (receiveStatus == 0) { send_str_com("RECEIVE ERROR"); receiveStatus = -1; return 0; } if ((dataCount > 1) && (receiveStatus == 1)) { if ((receiveStr[dataCount - 2] == 0X0D) && (receiveStr[dataCount - 1] == 0X0A)) { receiveStatus = -1; return 1; } } return 0; }
void send_str_com(unsigned char *sendStr) { uchar i; for (i = 0; sendStr[i] != '\0'; i++) { SBUF = sendStr[i]; while (TI == 0) ; TI = 0; } }
|