提交 | 用户 | age
|
5dd1b7
|
1 |
//#include "main.h" |
Q |
2 |
#include <modbus.h> |
|
3 |
|
|
4 |
//字地址 0 - 255 (只取低8位) |
|
5 |
//位地址 0 - 255 (只取低8位) |
|
6 |
|
|
7 |
/* CRC 高位字节值表 */ |
|
8 |
const uint8 code auchCRCHi[256] = { |
|
9 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
10 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
11 |
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
12 |
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
|
13 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, |
|
14 |
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
|
15 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, |
|
16 |
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
17 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
18 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, |
|
19 |
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, |
|
20 |
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
|
21 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
22 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, |
|
23 |
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
24 |
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
|
25 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
26 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
27 |
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
28 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
29 |
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
30 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, |
|
31 |
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, |
|
32 |
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
33 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
34 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 |
|
35 |
} ; |
|
36 |
/* CRC低位字节值表*/ |
|
37 |
const uint8 code auchCRCLo[256] = { |
|
38 |
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, |
|
39 |
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, |
|
40 |
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, |
|
41 |
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, |
|
42 |
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, |
|
43 |
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, |
|
44 |
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, |
|
45 |
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, |
|
46 |
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, |
|
47 |
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, |
|
48 |
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, |
|
49 |
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, |
|
50 |
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, |
|
51 |
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, |
|
52 |
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, |
|
53 |
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, |
|
54 |
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, |
|
55 |
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, |
|
56 |
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, |
|
57 |
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, |
|
58 |
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, |
|
59 |
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, |
|
60 |
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, |
|
61 |
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, |
|
62 |
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, |
|
63 |
0x43, 0x83, 0x41, 0x81, 0x80, 0x40 |
|
64 |
} ; |
|
65 |
|
|
66 |
uint8 testCoil; //用于测试 位地址1 |
|
67 |
uint16 testRegister; //用于测试 字址址16 |
|
68 |
uint16 Data[16]= {0}; |
|
69 |
uint8 localAddr = 1; //单片机控制板的地址 |
|
70 |
uint8 sendCount =0; //发送字节个数 |
|
71 |
uint8 receCount=0; //接收到的字节个数 |
|
72 |
uint8 sendPosi=0; //发送位置 |
|
73 |
|
|
74 |
//sbit bRunLED = P3^4; //Run |
|
75 |
//sbit bWatchDog = P1^7; //WatchDog |
|
76 |
//sbit b485Send = P1^3; //75LBC184 send |
|
77 |
//testRegister; |
|
78 |
|
|
79 |
uint16 crc16(uint8 *puchMsg, uint16 usDataLen) |
|
80 |
{ |
|
81 |
uint8 uchCRCHi = 0xFF ; /* 高CRC字节初始化 */ |
|
82 |
uint8 uchCRCLo = 0xFF ; /* 低CRC 字节初始化 */ |
|
83 |
uint32 uIndex ; /* CRC循环中的索引 */ |
|
84 |
while (usDataLen--) /* 传输消息缓冲区 */ |
|
85 |
{ |
|
86 |
uIndex = uchCRCHi ^ *puchMsg++ ; /* 计算CRC */ |
|
87 |
uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex] ; |
|
88 |
uchCRCLo = auchCRCLo[uIndex] ; |
|
89 |
} |
|
90 |
return (uchCRCHi << 8 | uchCRCLo) ; |
|
91 |
}//uint16 crc16(uint8 *puchMsg, uint16 usDataLen) |
|
92 |
|
|
93 |
//开始发送 |
|
94 |
void beginSend(void) |
|
95 |
{ |
|
96 |
//b485Send = 1; //设为发送 |
|
97 |
|
|
98 |
sendPosi = 0; |
|
99 |
if(sendCount > 1) |
|
100 |
sendCount--; |
|
101 |
|
|
102 |
//ACC = sendBuf[0]; |
|
103 |
//TB8 = P; |
|
104 |
TI=0; |
|
105 |
SBUF = sendBuf[0]; |
|
106 |
|
|
107 |
}//void beginSend(void) |
|
108 |
|
|
109 |
|
|
110 |
//读线圈状态 |
|
111 |
void readCoil(void) |
|
112 |
{ |
|
113 |
uint8 addr; |
|
114 |
uint8 tempAddr; |
|
115 |
uint8 byteCount; |
|
116 |
uint8 bitCount; |
|
117 |
uint16 crcData; |
|
118 |
uint8 position; |
|
119 |
uint8 i,k; |
|
120 |
// uint8 result; |
|
121 |
uint16 tempData; |
|
122 |
uint8 exit = 0; |
|
123 |
|
|
124 |
//addr = (receBuf[2]<<8) + receBuf[3]; |
|
125 |
//tempAddr = addr & 0xfff; |
|
126 |
addr = receBuf[3]; |
|
127 |
tempAddr = addr; |
|
128 |
|
|
129 |
//bitCount = (receBuf[4]<<8) + receBuf[5]; //读取的位个数 |
|
130 |
bitCount = receBuf[5]; |
|
131 |
|
|
132 |
byteCount = bitCount / 8; //字节个数 |
|
133 |
if(bitCount%8 != 0) |
|
134 |
byteCount++; |
|
135 |
|
|
136 |
for(k=0;k<byteCount;k++) |
|
137 |
{//字节位置 |
|
138 |
position = k + 3; |
|
139 |
sendBuf[position] = 0; |
|
140 |
for(i=0;i<8;i++) |
|
141 |
{ |
|
142 |
getCoilVal(tempAddr,&tempData); |
|
143 |
|
|
144 |
sendBuf[position] |= tempData << i; |
|
145 |
tempAddr++; |
|
146 |
if(tempAddr >= addr+bitCount) |
|
147 |
{ //读完 |
|
148 |
exit = 1; |
|
149 |
break; |
|
150 |
} |
|
151 |
} |
|
152 |
if(exit == 1) |
|
153 |
break; |
|
154 |
} |
|
155 |
|
|
156 |
sendBuf[0] = localAddr; |
|
157 |
sendBuf[1] = 0x01; |
|
158 |
sendBuf[2] = byteCount; |
|
159 |
byteCount += 3; |
|
160 |
crcData = crc16(sendBuf,byteCount); |
|
161 |
sendBuf[byteCount] = crcData >> 8; |
|
162 |
byteCount++; |
|
163 |
sendBuf[byteCount] = crcData & 0xff; |
|
164 |
sendCount = byteCount + 1; |
|
165 |
|
|
166 |
beginSend(); |
|
167 |
}//void readCoil(void) |
|
168 |
|
|
169 |
//读寄存器 |
|
170 |
void readRegisters(void) |
|
171 |
{ |
|
172 |
uint8 addr; |
|
173 |
uint8 tempAddr; |
|
174 |
// uint16 result; |
|
175 |
uint16 crcData; |
|
176 |
uint8 readCount; |
|
177 |
uint8 byteCount; |
|
178 |
// uint8 finsh; //1完成 0出错 |
|
179 |
uint16 i; |
|
180 |
uint16 tempData[] = {934,835,234,115}; |
|
181 |
|
|
182 |
for(i=0;i<4;i++) |
|
183 |
{ |
|
184 |
Data[i]=(unsigned int)outvalue[i]>>2; |
|
185 |
} |
|
186 |
for(i=4;i<8;i++) |
|
187 |
{ |
|
188 |
Data[i]=(unsigned int)(maxCurrentValue[i-4]>>2); |
|
189 |
} |
|
190 |
for(i=8;i<12;i++) |
|
191 |
{ |
|
192 |
Data[i]=ZeroPara[i-8]; |
|
193 |
} |
|
194 |
for(i=12;i<16;i++) |
|
195 |
{ |
|
196 |
Data[i]=RatePara[i-12]; |
|
197 |
} |
|
198 |
//addr = (receBuf[2]<<8) + receBuf[3]; |
|
199 |
//tempAddr = addr & 0xfff; |
|
200 |
addr = receBuf[3]; |
|
201 |
tempAddr = addr; |
|
202 |
|
|
203 |
readCount = (receBuf[4]<<8) + receBuf[5]; //要读的个数 |
|
204 |
//readCount = receBuf[5]; |
|
205 |
|
|
206 |
byteCount = readCount * 2; |
|
207 |
|
|
208 |
for(i=0;i<byteCount;i+=2,tempAddr++) |
|
209 |
{ |
|
210 |
sendBuf[i+3] = Data[tempAddr] >> 8; |
|
211 |
sendBuf[i+4] = Data[tempAddr] & 0xff; |
|
212 |
} |
|
213 |
|
|
214 |
sendBuf[0] = localAddr; |
|
215 |
sendBuf[1] = 3; |
|
216 |
sendBuf[2] = byteCount; |
|
217 |
byteCount += 3; |
|
218 |
crcData = crc16(sendBuf,byteCount); |
|
219 |
sendBuf[byteCount] = crcData >> 8; |
|
220 |
byteCount++; |
|
221 |
sendBuf[byteCount] = crcData & 0xff; |
|
222 |
|
|
223 |
sendCount = byteCount + 1; |
|
224 |
beginSend(); |
|
225 |
}//void readRegisters(void) |
|
226 |
|
|
227 |
|
|
228 |
//强制单个线圈 |
|
229 |
void forceSingleCoil(void) |
|
230 |
{ |
|
231 |
uint8 addr; |
|
232 |
uint8 tempAddr; |
|
233 |
uint16 tempData; |
|
234 |
uint8 onOff; |
|
235 |
uint8 i; |
|
236 |
|
|
237 |
//addr = (receBuf[2]<<8) + receBuf[3]; |
|
238 |
//tempAddr = addr & 0xfff; |
|
239 |
addr = receBuf[3]; |
|
240 |
tempAddr = addr; |
|
241 |
|
|
242 |
//onOff = (receBuf[4]<<8) + receBuf[5]; |
|
243 |
onOff = receBuf[4]; |
|
244 |
|
|
245 |
//if(onOff == 0xff00) |
|
246 |
if(onOff == 0xff) |
|
247 |
{ //设为ON |
|
248 |
tempData = 1; |
|
249 |
} |
|
250 |
//else if(onOff == 0x0000) |
|
251 |
else if(onOff == 0x00) |
|
252 |
{ //设为OFF |
|
253 |
tempData = 0; |
|
254 |
} |
|
255 |
|
|
256 |
setCoilVal(tempAddr,tempData); |
|
257 |
|
|
258 |
for(i=0;i<receCount;i++) |
|
259 |
{ |
|
260 |
sendBuf[i] = receBuf[i]; |
|
261 |
} |
|
262 |
sendCount = receCount; |
|
263 |
beginSend(); |
|
264 |
}//void forceSingleCoil(void) |
|
265 |
|
|
266 |
//设置单个寄存器 06 |
|
267 |
void presetSingleRegister(void) |
|
268 |
{ |
|
269 |
/* |
|
270 |
01 06 00 01 00 66 58 20 ----send |
|
271 |
(31 ms) |
|
272 |
01 06 00 01 00 66 58 20 ----receive |
|
273 |
*/ |
|
274 |
uint8 addr; |
|
275 |
uint8 tempAddr; |
|
276 |
uint16 crcData; |
|
277 |
uint16 tempData; |
|
278 |
// uint8 finsh; //?1??? ?0??? |
|
279 |
|
|
280 |
addr = (receBuf[2]<<8) + receBuf[3]; |
|
281 |
tempAddr = addr & 0xff;//???????? |
|
282 |
|
|
283 |
// sendBuf[4] = receBuf[4]; |
|
284 |
// sendBuf[5] = receBuf[5]; |
|
285 |
tempData = (receBuf[4]<<8) + receBuf[5]; |
|
286 |
|
|
287 |
//setRegisterVal(tempAddr,tempData); |
|
288 |
lightControl(tempAddr,tempData); |
|
289 |
|
|
290 |
sendBuf[0] = localAddr; |
|
291 |
sendBuf[1] = receBuf[1]; |
|
292 |
sendBuf[2] = addr >> 8; |
|
293 |
sendBuf[3] = addr & 0xff; |
|
294 |
|
|
295 |
crcData = crc16(sendBuf,6); |
|
296 |
sendBuf[6] = crcData >> 8; |
|
297 |
sendBuf[7] = crcData & 0xff; |
|
298 |
sendCount = 8; |
|
299 |
beginSend(); |
|
300 |
} |
|
301 |
|
|
302 |
|
|
303 |
//设置多个寄存器 |
|
304 |
void presetMultipleRegisters(void) |
|
305 |
{ |
|
306 |
uint8 addr; |
|
307 |
uint8 tempAddr; |
|
308 |
uint8 byteCount; |
|
309 |
uint8 setCount; |
|
310 |
uint16 crcData; |
|
311 |
uint16 tempData[8]; |
|
312 |
// uint8 finsh; //为1时完成 为0时出错 |
|
313 |
uint8 i; |
|
314 |
|
|
315 |
|
|
316 |
//addr = (receBuf[2]<<8) + receBuf[3]; |
|
317 |
//tempAddr = addr & 0xfff; |
|
318 |
addr = receBuf[3]; |
|
319 |
tempAddr = addr & 0xff; |
|
320 |
|
|
321 |
//setCount = (receBuf[4]<<8) + receBuf[5]; |
|
322 |
setCount = receBuf[5]; |
|
323 |
byteCount = receBuf[6]; |
|
324 |
|
|
325 |
for(i=0;i<setCount;i++) |
|
326 |
{ |
|
327 |
tempData[i] = (receBuf[i*2+7]<<8) + receBuf[i*2+8]; |
|
328 |
|
|
329 |
|
|
330 |
} |
|
331 |
setRegisterVal(tempAddr,tempData); |
|
332 |
|
|
333 |
sendBuf[0] = localAddr; |
|
334 |
sendBuf[1] = 16; |
|
335 |
sendBuf[2] = addr >> 8; |
|
336 |
sendBuf[3] = addr & 0xff; |
|
337 |
sendBuf[4] = setCount >> 8; |
|
338 |
sendBuf[5] = setCount & 0xff; |
|
339 |
crcData = crc16(sendBuf,6); |
|
340 |
sendBuf[6] = crcData >> 8; |
|
341 |
sendBuf[7] = crcData & 0xff; |
|
342 |
sendCount = 8; |
|
343 |
beginSend(); |
|
344 |
}//void presetMultipleRegisters(void) |
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
//检查uart0数据 |
|
349 |
void checkComm0Modbus(void) |
|
350 |
{ |
|
351 |
uint16 crcData; |
|
352 |
uint16 tempData; |
|
353 |
|
|
354 |
if(receCount > 4) |
|
355 |
{ |
|
356 |
|
|
357 |
switch(receBuf[1]) |
|
358 |
{ |
|
359 |
|
|
360 |
case 1://读取线圈状态(读取点 16位以内) |
|
361 |
case 3://读取保持寄存器(一个或多个) |
|
362 |
case 5://强制单个线圈 |
|
363 |
case 6://设置单个寄存器 |
|
364 |
if(receCount >= 8) |
|
365 |
{//接收完成一组数据 |
|
366 |
//应该关闭接收中断 |
|
367 |
|
|
368 |
if(receBuf[0]==localAddr && checkoutError==0) |
|
369 |
//if(receBuf[0]==localAddr) |
|
370 |
{ |
|
371 |
|
|
372 |
crcData = crc16(receBuf,6); |
|
373 |
|
|
374 |
if(crcData == receBuf[7]+(receBuf[6]<<8)) |
|
375 |
{ |
|
376 |
//校验正确 |
|
377 |
if(receBuf[1] == 1) |
|
378 |
{//读取线圈状态(读取点 16位以内) |
|
379 |
readCoil(); |
|
380 |
} |
|
381 |
else if(receBuf[1] == 3) |
|
382 |
{//读取保持寄存器(一个或多个) |
|
383 |
|
|
384 |
readRegisters(); |
|
385 |
|
|
386 |
} |
|
387 |
else if(receBuf[1] == 5) |
|
388 |
{//强制单个线圈 |
|
389 |
forceSingleCoil(); |
|
390 |
} |
|
391 |
else if(receBuf[1] == 6) |
|
392 |
{ |
|
393 |
presetSingleRegister(); |
|
394 |
// readRegisters(); |
|
395 |
|
|
396 |
} |
|
397 |
|
|
398 |
} |
|
399 |
} |
|
400 |
receCount = 0; |
|
401 |
checkoutError = 0; |
|
402 |
} |
|
403 |
break; |
|
404 |
|
|
405 |
case 15://设置多个线圈 |
|
406 |
tempData = receBuf[6]; |
|
407 |
tempData += 9; //数据个数 |
|
408 |
if(receCount >= tempData) |
|
409 |
{ |
|
410 |
if(receBuf[0]==localAddr && checkoutError==0) |
|
411 |
{ |
|
412 |
crcData = crc16(receBuf,tempData-2); |
|
413 |
if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1]) |
|
414 |
{ |
|
415 |
//forceMultipleCoils(); |
|
416 |
} |
|
417 |
} |
|
418 |
receCount = 0; |
|
419 |
checkoutError = 0; |
|
420 |
} |
|
421 |
break; |
|
422 |
|
|
423 |
case 16://设置多个寄存器 |
|
424 |
tempData = (receBuf[4]<<8) + receBuf[5]; |
|
425 |
tempData = tempData * 2; //数据个数 |
|
426 |
tempData += 9; |
|
427 |
|
|
428 |
if(receCount >= tempData) |
|
429 |
{ |
|
430 |
|
|
431 |
if(receBuf[0]==localAddr && checkoutError==0) |
|
432 |
{ |
|
433 |
crcData = crc16(receBuf,tempData-2); |
|
434 |
if(crcData == (receBuf[tempData-2]<<8)+ receBuf[tempData-1]) |
|
435 |
{ |
|
436 |
presetMultipleRegisters(); |
|
437 |
|
|
438 |
} |
|
439 |
} |
|
440 |
receCount = 0; |
|
441 |
checkoutError = 0; |
|
442 |
} |
|
443 |
break; |
|
444 |
|
|
445 |
default: |
|
446 |
break; |
|
447 |
} |
|
448 |
} |
|
449 |
}//void checkComm0(void) |
|
450 |
|
|
451 |
//取线圈状态 返回0表示成功 |
|
452 |
uint16 getCoilVal(uint16 addr,uint16 *tempData) |
|
453 |
{ |
|
454 |
uint16 result = 0; |
|
455 |
uint16 tempAddr; |
|
456 |
|
|
457 |
tempAddr = addr & 0xfff; |
|
458 |
//只取低8位地址 |
|
459 |
switch(tempAddr & 0xff) |
|
460 |
{ |
|
461 |
case 0: |
|
462 |
break; |
|
463 |
case 1: |
|
464 |
*tempData = testCoil; |
|
465 |
break; |
|
466 |
case 2: |
|
467 |
break; |
|
468 |
case 3: |
|
469 |
break; |
|
470 |
case 4: |
|
471 |
break; |
|
472 |
case 5: |
|
473 |
break; |
|
474 |
case 6: |
|
475 |
break; |
|
476 |
case 7: |
|
477 |
break; |
|
478 |
case 8: |
|
479 |
break; |
|
480 |
case 9: |
|
481 |
break; |
|
482 |
case 10: |
|
483 |
break; |
|
484 |
case 11: |
|
485 |
break; |
|
486 |
case 12: |
|
487 |
break; |
|
488 |
case 13: |
|
489 |
break; |
|
490 |
case 14: |
|
491 |
break; |
|
492 |
case 15: |
|
493 |
break; |
|
494 |
case 16: |
|
495 |
break; |
|
496 |
default: |
|
497 |
break; |
|
498 |
} |
|
499 |
|
|
500 |
return result; |
|
501 |
}//uint16 getCoilVal(uint16 addr,uint16 *data) |
|
502 |
|
|
503 |
|
|
504 |
//设定线圈状态 返回0表示成功 |
|
505 |
uint16 setCoilVal(uint16 addr,uint16 tempData) |
|
506 |
{ |
|
507 |
uint16 result = 0; |
|
508 |
uint16 tempAddr; |
|
509 |
|
|
510 |
tempAddr = addr & 0xfff; |
|
511 |
|
|
512 |
|
|
513 |
switch(tempAddr & 0xff) |
|
514 |
{ |
|
515 |
case 0: |
|
516 |
break; |
|
517 |
case 1: |
|
518 |
testCoil = tempData; |
|
519 |
break; |
|
520 |
case 2: |
|
521 |
break; |
|
522 |
case 3: |
|
523 |
break; |
|
524 |
case 4: |
|
525 |
break; |
|
526 |
case 5: |
|
527 |
break; |
|
528 |
case 6: |
|
529 |
break; |
|
530 |
case 7: |
|
531 |
break; |
|
532 |
case 8: |
|
533 |
break; |
|
534 |
case 9: |
|
535 |
break; |
|
536 |
case 10: |
|
537 |
break; |
|
538 |
case 11: |
|
539 |
break; |
|
540 |
case 12: |
|
541 |
break; |
|
542 |
case 13: |
|
543 |
break; |
|
544 |
case 14: |
|
545 |
break; |
|
546 |
case 15: |
|
547 |
break; |
|
548 |
case 16: |
|
549 |
break; |
|
550 |
default: |
|
551 |
break; |
|
552 |
} |
|
553 |
|
|
554 |
|
|
555 |
return result; |
|
556 |
}//uint16 setCoilVal(uint16 addr,uint16 data) |
|
557 |
|
|
558 |
//取寄存器值 返回0表示成功 |
|
559 |
uint16 getRegisterVal(uint16 addr,uint16 *tempData) |
|
560 |
{ |
|
561 |
uint16 result = 0; |
|
562 |
uint16 tempAddr; |
|
563 |
|
|
564 |
tempAddr = addr & 0xfff; |
|
565 |
|
|
566 |
switch(tempAddr & 0xff) |
|
567 |
{ |
|
568 |
case 0: |
|
569 |
break; |
|
570 |
case 1: |
|
571 |
break; |
|
572 |
case 2: |
|
573 |
break; |
|
574 |
case 3: |
|
575 |
break; |
|
576 |
case 4: |
|
577 |
break; |
|
578 |
case 5: |
|
579 |
break; |
|
580 |
case 6: |
|
581 |
break; |
|
582 |
case 7: |
|
583 |
break; |
|
584 |
case 8: |
|
585 |
break; |
|
586 |
case 9: |
|
587 |
break; |
|
588 |
case 10: |
|
589 |
break; |
|
590 |
case 11: |
|
591 |
break; |
|
592 |
case 12: |
|
593 |
break; |
|
594 |
case 13: |
|
595 |
break; |
|
596 |
case 14: |
|
597 |
break; |
|
598 |
case 15: |
|
599 |
break; |
|
600 |
case 16: |
|
601 |
*tempData = testRegister; |
|
602 |
break; |
|
603 |
default: |
|
604 |
break; |
|
605 |
} |
|
606 |
|
|
607 |
return result; |
|
608 |
}//uint16 getRegisterVal(uint16 addr,uint16 &data) |
|
609 |
|
|
610 |
|
|
611 |
//设置寄存器值 返回0表示成功 |
|
612 |
uint16 lightControl(uint16 addr,uint16 tempData) |
|
613 |
{ |
|
614 |
uint16 result = 0; |
|
615 |
uint16 tempAddr; |
|
616 |
|
|
617 |
tempAddr = addr & 0xfff; |
|
618 |
|
|
619 |
switch(tempAddr & 0xff) |
|
620 |
{ |
|
621 |
case 0: |
|
622 |
LedOn[0] = 0; |
|
623 |
LedOn[1] = 0; |
|
624 |
LedOn[2] = 0; |
|
625 |
LedOn[3] = 0; |
|
626 |
break; |
|
627 |
case 1: |
|
628 |
if(tempData==1) |
|
629 |
{ |
|
630 |
LedOn[0] = 1; |
|
631 |
} |
|
632 |
else |
|
633 |
{ |
|
634 |
LedOn[0] = 0; |
|
635 |
} |
|
636 |
break; |
|
637 |
case 2: |
|
638 |
if(tempData==1) |
|
639 |
{ |
|
640 |
LedOn[1]= 1; |
|
641 |
} |
|
642 |
else |
|
643 |
{ |
|
644 |
LedOn[1] = 0; |
|
645 |
} |
|
646 |
break; |
|
647 |
case 3: |
|
648 |
if(tempData==1) |
|
649 |
{ |
|
650 |
LedOn[2] = 1; |
|
651 |
} |
|
652 |
else |
|
653 |
{ |
|
654 |
LedOn[2] = 0; |
|
655 |
} |
|
656 |
break; |
|
657 |
case 4: |
|
658 |
if(tempData==1) |
|
659 |
{ |
|
660 |
LedOn[3] = 1; |
|
661 |
} |
|
662 |
else |
|
663 |
{ |
|
664 |
LedOn[3]= 0; |
|
665 |
} |
|
666 |
break; |
|
667 |
case 5: |
|
668 |
needstorePC =1; |
|
669 |
break; |
|
670 |
case 6: |
|
671 |
break; |
|
672 |
case 7: |
|
673 |
break; |
|
674 |
case 8: |
|
675 |
break; |
|
676 |
case 9: |
|
677 |
break; |
|
678 |
case 10: |
|
679 |
break; |
|
680 |
case 11: |
|
681 |
break; |
|
682 |
case 12: |
|
683 |
break; |
|
684 |
case 13: |
|
685 |
break; |
|
686 |
case 14: |
|
687 |
break; |
|
688 |
case 15: |
|
689 |
break; |
|
690 |
case 16: |
|
691 |
LedOn[0]=1; |
|
692 |
LedOn[1]=1; |
|
693 |
LedOn[2]=1; |
|
694 |
LedOn[3]=1; |
|
695 |
break; |
|
696 |
default: |
|
697 |
break; |
|
698 |
} |
|
699 |
|
|
700 |
return result; |
|
701 |
}//uint8 lightControl(uint16 addr,uint16 data) |
|
702 |
|
|
703 |
//设置寄存器值 返回0表示成功 |
|
704 |
uint16 setRegisterVal(uint8 addr,uint16 tempData[]) |
|
705 |
{ |
|
706 |
uint16 result = 0; |
|
707 |
uint16 tempAddr; |
|
708 |
uint16 i; |
|
709 |
tempAddr = addr & 0xfff; |
|
710 |
|
|
711 |
switch(tempAddr & 0xff) |
|
712 |
{ |
|
713 |
case 0: |
|
714 |
for(i=0;i<4;i++) |
|
715 |
{ |
|
716 |
Data[i] = tempData[i]; |
|
717 |
Data[i] =Data[i] <<2; |
|
718 |
outvalue[i]=(int)Data[i]; |
|
719 |
} |
|
720 |
break; |
|
721 |
case 1: |
|
722 |
for(i=0;i<4;i++) |
|
723 |
{ |
|
724 |
//Data[i] = tempData[i]; |
|
725 |
//Data[i] =Data[i] <<2; |
|
726 |
ZeroPara[i]=(int)tempData[i]; |
|
727 |
} |
|
728 |
for(i=4;i<8;i++) |
|
729 |
{ |
|
730 |
//Data[i] = tempData[i]; |
|
731 |
//Data[i] =Data[i] <<2; |
|
732 |
RatePara[i-4]=(int)tempData[i]; |
|
733 |
} |
|
734 |
|
|
735 |
break; |
|
736 |
case 2: |
|
737 |
for(i=0;i<4;i++) |
|
738 |
{ |
|
739 |
Data[i] = tempData[i]; |
|
740 |
Data[i] =Data[i] <<2; |
|
741 |
maxCurrentValue[i]=(int)Data[i]; |
|
742 |
} |
|
743 |
break; |
|
744 |
case 3: |
|
745 |
|
|
746 |
break; |
|
747 |
case 4: |
|
748 |
break; |
|
749 |
case 5: |
|
750 |
break; |
|
751 |
case 6: |
|
752 |
break; |
|
753 |
case 7: |
|
754 |
break; |
|
755 |
case 8: |
|
756 |
break; |
|
757 |
case 9: |
|
758 |
break; |
|
759 |
case 10: |
|
760 |
break; |
|
761 |
case 11: |
|
762 |
break; |
|
763 |
case 12: |
|
764 |
break; |
|
765 |
case 13: |
|
766 |
break; |
|
767 |
case 14: |
|
768 |
break; |
|
769 |
case 15: |
|
770 |
break; |
|
771 |
case 16: |
|
772 |
// testRegister = tempData; |
|
773 |
break; |
|
774 |
default: |
|
775 |
break; |
|
776 |
} |
|
777 |
|
|
778 |
return result; |
|
779 |
}//uint8 setRegisterVal(uint16 addr,uint16 data) |