📄 slave.lst
字号:
401 1 }
402
403 /*********************************************************************************************************
-**
404 功能:SetDataBytes
405 描述:设置自带RAM的字节长度
406 参数:address---存放长度的地址 len---长度值
407 返回:无
408 **********************************************************************************************************
-**/
409 void SetMyDataBytes(uint address,uchar len)
410 {
411 1 uint num;
412 1 xdata uchar temp[2];
413 1 MyReadBytes(address,temp,2);
414 1 num = temp[0] + temp[1] * 0x100 + len;
415 1 temp[0]=num%256;
416 1 temp[1]=num/256;
417 1 MyWriteBytes(address,temp,2);
418 1 }
419
420 /*********************************************************************************************************
-**
421 功能:GetMyDataBytes
422 描述:得到自带RAM里的数据字节数
423 参数:address---存放长度的地址
424 返回:uint
425 **********************************************************************************************************
-**/
426 uint GetMyDataBytes(uint address)
427 {
428 1 uint num;
429 1 xdata uchar temp[2] = {0x00,0x00};
430 1 MyReadBytes(address,temp,2);
431 1 num = temp[0] + temp[1] * 0x100;
432 1 return num;
433 1 }
434
435 /*********************************************************************************************************
-**
436 功能:ClearComRam
437 描述:清零字节数存储地址
438 参数:address---需要清零的地址
439 返回:无
440 **********************************************************************************************************
-**/
441 void ClearComRam(uint address)
442 {
443 1 ComWriteOneByte(address,0x00);
444 1 ComWriteOneByte(address+1,0x00);
445 1 }
446
447 /*********************************************************************************************************
-**
C51 COMPILER V7.50 SLAVE 03/17/2008 11:04:03 PAGE 9
448 功能:SetComDataBytes;设置字节数的函数
449 描述:设置公共RAM的字节长度。是先读出长度,再累加一个需要设置的长度。
450 参数:address---需要设置字节数的地址;len---需要设置的长度。
451 返回:无
452 **********************************************************************************************************
-**/
453 void SetComDataBytes(uint address,uchar len)
454 {
455 1 uint num;
456 1 xdata uchar temp[2];
457 1 ComReadBytes(address,temp,2);
458 1 num = temp[0] + temp[1] * 0x100 + len;
459 1 temp[0]=num%256;
460 1 temp[1]=num/256;
461 1 ComWriteBytes(address,temp,2);
462 1 }
463
464 /*********************************************************************************************************
-**
465 功能:GetComDataBytes;取到字节数的函数
466 描述:取得公共RAM里的数据字节数
467 参数:address---存放字节数的地址
468 返回:uint
469 **********************************************************************************************************
-**/
470 uint GetComDataBytes(uint address)
471 {
472 1 uint num;
473 1 xdata uchar temp[2] = {0x00,0x00};
474 1
475 1 ComReadBytes(address,temp,2);
476 1 num = temp[0] + temp[1] * 0x100;
477 1 return num;
478 1 }
479
480 /*********************************************************************************************************
-**
481 功能:ProcessOrders,处理上位机发来的数据包。
482 描述:从自己的RAM读取并处理接收到的命令字节,当为Ws命令时,每个通道的最大值(2Bytes),最小值(2Bytes),超高报
-警
483 号(2Bytes),超低报警号(2Bytes),通道使能,(1Bytes),超高报警动作号(1Bytes),超低报警动作号(1Bytes)。
484 如果已知外设i ,通道j,则对应存储的位置为:
485 1000H +(i-1) *100H+ j *10H。
486 参数:无
487 返回:无
488 **********************************************************************************************************
-**/
489 void ProcessOrders()
490 {
491 1 xdata uchar ProcessBuff[PROCESSLEN];
492 1 uchar StarTemp,BagFlagTemp,SlaveAddrTemp; //前3个字节用来判断
493 1 uchar Len; //除去首三个字节的其它字节总数
494 1 uchar ws,channel; //外设号,通道号
495 1 uint address2; //外设信息地址
496 1 uint length;
497 1 uint address1 = MyOrderStart; //address1---命令的起始保存地址,length---保存命令包的总长度
498 1
499 1 length=GetMyDataBytes(MyOrderLen); //先从MyOrderLen取出命令数据包的长度
500 1
501 1 if(length > 0) //判断length的有效性
502 1 {
503 2 for(;address1 < MyOrderStart+length;) //如果没有处理完,继续处理
C51 COMPILER V7.50 SLAVE 03/17/2008 11:04:03 PAGE 10
504 2 {
505 3 StarTemp=MyReadOneByte(address1); //先读取第一个字节,取出开始字符
506 3 if(StarTemp==StartFlag) //若第一个字节是开始字符2A
507 3 {
508 4 address1++; //地址向后移一个字节
509 4 BagFlagTemp=MyReadOneByte(address1); //读取下一个字节,取出包标识
510 4 address1++; //地址向后移一个字节
511 4 SlaveAddrTemp=MyReadOneByte(address1); //读出从机号
512 4
513 4 if(SlaveAddrTemp==SlaveCode) //若是从机1的数据包
514 4 {
515 5 switch(BagFlagTemp) //判断包标识
516 5 {
517 6 case CmdWs: //若是外设参数包
518 6 address1++; //地址向后移一个字节
519 6 Len=LenCmdWs-3; //取出除掉前3个字节之后的包的长度。
520 6 MyReadBytes(address1,ProcessBuff,Len); //然后把包读到ProcessBuff中。
521 6 ws=ProcessBuff[0]; //取出外设号
522 6 channel=ProcessBuff[1]; //取出通道号
523 6 address2=Ws1channel1+ (ws - 1) * 0x100+ channel * 0x10; //计算保存参数地址的地址
524 6 MyWriteBytes(address2,ProcessBuff+2,Len-2); //指针应指向ProcessBuff[2]的
-地址,写到相应的地址中去。
525 6 address1+=Len; //更新地址
526 6 break;
527 6 case CmdSlave: //若是从机参数包
528 6 address1++; //地址向后移一个字节
529 6 Len=LenCmdSlave-3; //取出除掉前3个字节之后的包的长度。
530 6 MyReadBytes(address1,ProcessBuff,Len);//然后把包读到ProcessBuff中。
531 6 if((ProcessBuff[0] >= 0x01) && (ProcessBuff[0] <= 0x03))
532 6 {
533 7 BodeRate=ProcessBuff[0]; //取出波特率0x01-2400,0x02-4800,0x03-9600
534 7 MyWriteOneByte(MySlaveParaStart,BodeRate); //保存波特率
535 7 }
536 6 if((ProcessBuff[1] == 0x01) || (ProcessBuff[1] == 0x02))
537 6 {
538 7 WsType=ProcessBuff[1]; //取出外设类型0x01---7017,0x02---7053
539 7 MyWriteOneByte(MySlaveParaStart+1,WsType); //保存外设类型
540 7 if(WsType == WsType1) //如果外设是7017,则RecvDataSize==56。
541 7 {
542 8 RecvDataSize = LenRecvWsType1;
543 8 }
544 7 else if(WsType == WsType2) //如果外设是7053,则RecvDataSize==4。
545 7 {
546 8 RecvDataSize = LenRecvWsType2;
547 8 }
548 7 }
549 6 if((ProcessBuff[2] >= 0x01) && (ProcessBuff[2] <= 0x05))
550 6 {
551 7 WsNumber=ProcessBuff[2]; //取出外设个数
552 7 MyWriteOneByte(MySlaveParaStart+2,WsNumber); //保存外设个数
553 7 }
554 6 ReadDataRate=ProcessBuff[3] * 0x100 + ProcessBuff[4]; //取出读外设数据的速率
555 6 address1+=Len; //更新地址
556 6 MyWriteOneByte(MySlaveParaStart+3,ReadDataRate/0x100); //保存读外设数据的速率
557 6 MyWriteOneByte(MySlaveParaStart+4,ReadDataRate%0x100); //保存读外设数据的速率
558 6 FlagReInit = 1; //从机初始化标志,标识从机的参数包已经处理,可以执行新的从机参数设置操作了。
559 6 break;
560 6 case CmdWsControl: //若是外设控制包.
561 6 address1++; //地址向后移一个字节.
562 6 ws= MyReadOneByte(address1); //取出外设编号.
563 6 address1++; //地址向后移一个字节.
564 6 Len=MyReadOneByte(address1); //取出命令长度.
C51 COMPILER V7.50 SLAVE 03/17/2008 11:04:03 PAGE 11
565 6 address1++; //地址向后移一个字节.
566 6 MyReadBytes(address1,ProcessBuff,Len); //然后把包读到ProcessBuff中。
567 6 SendStringCom(ProcessBuff,Len); //发送命令到外设。
568 6 address1+=Len; //更新地址.
569 6 break;
570 6 default:
571 6 break;
572 6 }
573 5 }
574 4
575 4 else //所取的命令不是该从机的处理情况
576 4 {
577 5 switch(BagFlagTemp) //判断包标识
578 5 {
579 6 case CmdWs: //若是外设参数包
580 6 Len=LenCmdWs-2; //取出除掉前3个字节之后的包的长度。
581 6 address1+=Len; //更新地址
582 6 break;
583 6 case CmdSlave: //若是从机参数包
584 6 Len=LenCmdSlave-2; //取出除掉前3个字节之后的包的长度。
585 6 address1+=Len; //更新地址
586 6 break;
587 6 case CmdWsControl: //若是外设控制包.
588 6 Len=MyReadOneByte(address1 + 2); //取出命令长度.
589 6 address1+=(Len+3); //更新地址.
590 6 break;
591 6 default:
592 6 break;
593 6 }
594 5
595 5 }
596 4
597 4 }
598 3
599 3 }
600 2 }
601 1 }
602
603 /*********************************************************************************************************
-**
604 功能:SetBodeRate
605 描述:设置波特率,假设晶振为12MHZ
606 参数:boderate---波特率编号
607 返回:无
608 **********************************************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -