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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234








#include <STC8H.H> //8H头文件


#include <UART2.H>


#include <UART1.H>


bit busy2;


char Uart2_RecvBuf[30] = {0}; //串口数据缓存


char uart2Index = 0;          //绶存指针


char uart2RXEnd = 0;


char Uart2RXLength = 0;








/*-------------------------------------------屏幕通信初始化----------------------------------------------*/


void Uart2Init(void) //9600bps@11.0592MHz


{


    S2CON = 0x50;        //8位数据,可变波特率


    AUXR |= 0x04;        //定时器2时钟为Fosc,即1T


    T2L = 0xE8;        //设定定时初值


    T2H = 0xFF;        //设定定时初值


    AUXR |= 0x10;        //启动定时器2





    S2CON |= 0x10; //使能接收 S2REN=1


    IE2 |= 0x01;   //使能串口2中断


    EA = 1;


}








/*-------------------------------------------接收信息----------------------------------------------*/


void Uart2() interrupt 8{


    if ((S2CON & S2RI) && uart2RXEnd == 0) {


        S2CON &= ~S2RI; //清除S2RI位


        P47 = ~P47;


        Uart2_RecvBuf[uart2Index] =


                S2BUF;


        if (Uart2_RecvBuf[0] == 0xFE) //Uart2_RecvBuf协议第一个字节是0x5a


        {


            uart2Index++;


        }


        if (uart2Index > 5) {


            uart2Index = 0;


            uart2RXEnd = 1;


        }


    }


    if (S2CON & S2TI) {


        S2CON &= ~S2TI; //清除S2TI位


        busy2 = 0;       //清忙标志


    }


}





char aaa[5] = {0xfe, 0xbb, 0x01, 0x00, 0x16};





void UartRx2(void) {


    if (uart2RXEnd) {


        uart2RXEnd = 0;


        UART1_SendStr(Uart2_RecvBuf);


        SendString2(aaa, 5);


    }





}








/*-------------------------------------------发送----------------------------------------------*/


void SendData2(char dat) {


    while (busy2); //等待前面的数据发送完成


    //ACC = dat;                  //获取校验位P (PSW.0)


    busy2 = 1;


    S2BUF = dat; //写数据到UART2数据寄存器


}








void SendString2(char *s, char length) //发送字符串


{


    while (length != 0) //检测字符串结束标志


    {


        SendData2(*s++); //发送当前字符


        length--;


    }


}