📄 comm.lst
字号:
191 2 // 升降频, 转换时将频率计数值放低4位,频率增量放高4位
192 2 MotorStatus[idx].UpDwon = TwoHexCharToInt( CommBuf.MsgData[8], CommBuf.MsgData[7] );
193 2 if ( !(MotorStatus[idx].UpDwon & 0xf0) || !(MotorStatus[idx].UpDwon & 0x0f) )
194 2 MotorStatus[idx].UpDwon = 0;
195 2
196 2 // 频率
197 2 MotorStatus[idx].SetFreq = (TwoHexCharToInt( CommBuf.MsgData[9], CommBuf.MsgData[10] )<<8)
198 2 | TwoHexCharToInt( CommBuf.MsgData[11], CommBuf.MsgData[12] );
199 2 if ( MotorStatus[idx].SetFreq > MAX_FREQ_VAL )
200 2 MotorStatus[idx].SetFreq = MAX_FREQ_VAL;
201 2 if ( MotorStatus[idx].SetFreq < START_FREQ )
202 2 MotorStatus[idx].SetFreq = START_FREQ;
203 2
204 2 // 脉冲数量
205 2 ptr = (BYTE idata*)&MotorStatus[idx].CmdPluseNum + MSB;
206 2 *ptr = TwoHexCharToInt( CommBuf.MsgData[13], CommBuf.MsgData[14] );
207 2 ptr++;
208 2 *ptr = TwoHexCharToInt( CommBuf.MsgData[15], CommBuf.MsgData[16] );
209 2 ptr++;
210 2 *ptr = TwoHexCharToInt( CommBuf.MsgData[17], CommBuf.MsgData[18] );
211 2 ptr++;
212 2 *ptr = TwoHexCharToInt( CommBuf.MsgData[19], CommBuf.MsgData[20] );
213 2
214 2 // 回零命令
215 2 if ( MotorStatus[idx].Status & ZERO_CHECK_MASK )
216 2 {
217 3 // 已在零点
218 3 if ( (MotorStatus[idx].TotalPluseCnt == 0) && (MotorStatus[idx].Status & ZERO_RECORD_MASK) )
219 3 {
220 4 // 保留零点记录
221 4 MotorStatus[idx].Status &= ZERO_RECORD_MASK;
222 4 MotorStatus[idx].Dir = 0;
223 4 }
224 3 }
225 2 CommBuf.MsgData[0] = ACK_CHAR;
226 2 break;
227 2
228 2 case WRITE_MOTOR_FREQ: // 改变电机频率
229 2 if ( idx >= MAX_MOTOR_NUM )
230 2 break;
231 2
232 2 // 已启动
233 2 if ( MotorStatus[idx].Status & MOTOR_RUN_MASK )
234 2 {
235 3 // 不是升降速都可以改变频率
236 3 if ( !MotorStatus[idx].UpDwon )
237 3 {
C51 COMPILER V7.04 COMM 01/23/2007 12:25:25 PAGE 5
238 4 tmp = (TwoHexCharToInt( CommBuf.MsgData[5], CommBuf.MsgData[6] ) << 8)
239 4 | TwoHexCharToInt( CommBuf.MsgData[7], CommBuf.MsgData[8] );
240 4 if ( tmp > MAX_FREQ_VAL )
241 4 tmp = MAX_FREQ_VAL;
242 4 if ( tmp < START_FREQ )
243 4 tmp = START_FREQ;
244 4
245 4 MotorStatus[idx].SetFreq = tmp;
246 4 CommBuf.MsgData[0] = ACK_CHAR;
247 4 }
248 3 }
249 2 break;
250 2
251 2 case WRITE_MOTOR_STOP_CMD: // 电机停止
252 2 // 停止所有电机
253 2 if ( idx == 0xff )
254 2 {
255 3 if ( Timer[0].MotorIdx )
256 3 MotorStatus[Timer[0].MotorIdx-1].Status |= MOTOR_STOP_CMD_MASK;
257 3
258 3 if ( Timer[1].MotorIdx )
259 3 MotorStatus[Timer[1].MotorIdx-1].Status |= MOTOR_STOP_CMD_MASK;
260 3
261 3 CommBuf.MsgData[0] = ACK_CHAR;
262 3 }
263 2 else if ( idx < MAX_MOTOR_NUM )
264 2 {
265 3 MotorStatus[idx].Status |= MOTOR_STOP_CMD_MASK;
266 3 CommBuf.MsgData[0] = ACK_CHAR;
267 3 }
268 2 break;
269 2
270 2 case WRITE_CLR_PLUSE_CNT:
271 2 // 清除所有电机频率
272 2 if ( idx == 0xff )
273 2 {
274 3 for( idx = 0; idx < MAX_MOTOR_NUM; idx++ )
275 3 {
276 4 MotorStatus[idx].TotalPluseCnt = 0;
277 4 }
278 3 CommBuf.MsgData[0] = ACK_CHAR;
279 3 }
280 2 else if ( idx < MAX_MOTOR_NUM )
281 2 {
282 3 MotorStatus[idx].TotalPluseCnt = 0;
283 3 CommBuf.MsgData[0] = ACK_CHAR;
284 3 }
285 2 break;
286 2 }
287 1
288 1 CommandProcessExtra( );
289 1 }
290
291 void RecvMsgProce( void )
292 {
293 1 u_char tmp;
294 1
295 1 if ( !RecvOkFlag )
296 1 return;
297 1
298 1 // 模块地址
299 1 tmp = HexCharToInt(CommBuf.MsgData[1]);
C51 COMPILER V7.04 COMM 01/23/2007 12:25:25 PAGE 6
300 1 if ( tmp && (tmp != (P1 & 0x0f) ) )
301 1 return;
302 1
303 1 // 检查校验码
304 1 if ( TwoHexCharToInt( CommBuf.MsgData[CommBuf.MsgCnt-3], CommBuf.MsgData[CommBuf.MsgCnt-2] )
305 1 != CalCheckCode( CommBuf.MsgData, CommBuf.MsgCnt-3 ) )
306 1 {
307 2 CommBuf.MsgData[0] = NO_ACK_CHAR;
308 2 CommBuf.MsgCnt = CMD_HEAD_LEN;
309 2 CommandProcessExtra( );
310 2 return;
311 2 }
312 1
313 1 switch( CommBuf.MsgData[3] )
314 1 {
315 2 case 'R':
316 2 ReadCommand( );
317 2 return;
318 2 case 'W':
319 2 WriteCommand( );
320 2 return;
321 2 default:
322 2 CommBuf.MsgData[0] = NO_ACK_CHAR;
323 2 CommBuf.MsgCnt = CMD_HEAD_LEN;
324 2 CommandProcessExtra( );
325 2 break;
326 2 }
327 1 }
328
329 // 注意:中断程序中不能调用带参数子程序
330 void SerialComm( void ) interrupt 4 using 1
331 {
332 1 if ( RI )
333 1 {
334 2 RI = 0;
335 2
336 2 CommChar = SBUF;
337 2 if ( (CommChar >= 'a') && (CommChar <= 'z') )
338 2 CommChar = CommChar - 'a' + 'A';
339 2
340 2 if ( !RecvOkFlag && !TxdStartFlag )
341 2 {
342 3 switch( CommBuf.MsgCnt )
343 3 {
344 4 case 0:
345 4 if ( CommChar == START_CHAR )
346 4 {
347 5 CommBuf.MsgData[0] = CommChar;
348 5 CommBuf.MsgCnt = 1;
349 5 }
350 4 break;
351 4 default:
352 4 if ( CommChar == START_CHAR )
353 4 {
354 5 CommBuf.MsgData[0] = CommChar;
355 5 CommBuf.MsgCnt = 1;
356 5 break;
357 5 }
358 4
359 4 CommBuf.MsgData[CommBuf.MsgCnt] = CommChar;
360 4 CommBuf.MsgCnt++;
361 4 if ( CommBuf.MsgCnt == MAX_COMM_BUF_SIZE )
C51 COMPILER V7.04 COMM 01/23/2007 12:25:25 PAGE 7
362 4 CommBuf.MsgCnt = 0;
363 4
364 4 if ( CommChar == END_CHAR )
365 4 RecvOkFlag = 1;
366 4 break;
367 4 }
368 3 }
369 2 }
370 1
371 1 if ( TI )
372 1 {
373 2 TI = 0;
374 2 if ( (CommBuf.SendIdx < CommBuf.MsgCnt) && TxdStartFlag )
375 2 {
376 3 SBUF = CommBuf.MsgData[CommBuf.SendIdx];
377 3 CommBuf.SendIdx++;
378 3 }
379 2 else
380 2 {
381 3 RE = RECV;
*** ERROR C202 IN LINE 381 OF COMM.C: 'P3_3': undefined identifier
382 3 CommBuf.MsgCnt = 0;
383 3 CommBuf.SendIdx = 0;
384 3 TxdStartFlag = 0;
385 3 }
386 2 }
387 1 }
388
389
C51 COMPILATION COMPLETE. 0 WARNING(S), 5 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -