📄 pcckmodbus.lst
字号:
573 子地址suba 读出的内容放入s 指向的存储区读no 个字节如果返回1
574 表示操作成功否则操作有误
575 **********************************************************************/
576 bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
577 {
578 1 uchar i;
579 1 GetBus();
580 1 SendByte(sla);
581 1 if(I2STAT!=0x18)
582 1 {
583 2 I2CON=0x54;
584 2 return(0);
585 2 }
586 1 SendByte(suba);
587 1 if(I2STAT!=0x28)
588 1 {
589 2 I2CON=0x54;
590 2 return(0);
591 2 }
592 1 for(i=0;i<no;i++)
593 1 {
594 2 wdog=1;
595 2 SendByte(*s);
596 2 if(I2STAT!=0x28)
597 2 {
598 3 I2CON=0x54;
599 3 return(0);
600 3 }
601 2 s++;
602 2 }
603 1 I2CON=0x54;//结束总线
604 1 return(1);
605 1 }
606 /*******************************************************************
607 向无子地址器件读字节数据函数
608 函数原型: bit IRcvByte(uchar sla,ucahr *c);
609 功能:从启动总线到发送地址读数据结束总线的全过程,从器件地址sla 返
610 回值在c 如果返回1 表示操作成功否则操作有误
611 ********************************************************************/
612 /*bit IRcvByte(uchar sla,uchar *c)
613 {
614 GetBus();
615 SendByte(sla+1);
616 if(I2STAT!=0x40)
617 {
618 I2CON=0x54;
619 return(0);
620 }
621 I2CON=0x41;//
622 while(SI==0);
C51 COMPILER V7.01 PCCKMODBUS 07/23/2004 15:55:57 PAGE 15
623 if(I2STAT!=0x58)
624 {
625 I2CON=0x54;
626 return(0);
627 }
628 *c=I2DAT;
629 I2CON=0x54;
630 return(1);
631 } */
632 /**********************************************************************
633 向有子地址器件读取多字节数据函数
634 函数原型: bit ISendStr(uint sla,ucahr *s,uchar no);
635 功能:从启动总线到发送地址子地址,读数据结束总线的全过程,从器件地址sla
636 子地址suba 读出的内容放入s 指向的存储区读no 个字节如果返回1
637 表示操作成功否则操作有误
638 **********************************************************************/
639 bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
640 {
641 1 uchar i;
642 1 GetBus();
643 1 SendByte(sla);
644 1 if(I2STAT!=0x18)
645 1 {
646 2 I2CON=0x54;
647 2 return(0);
648 2 }
649 1 SendByte(suba);
650 1 if(I2STAT!=0x28)
651 1 {
652 2 I2CON=0x54;
653 2 return(0);
654 2 }
655 1 I2CON=0x64;//重新启动总线
656 1 while(SI==0);
657 1 SendByte(sla+1);
658 1 if(I2STAT!=0x40)
659 1 {
660 2 I2CON=0x54;
661 2 return(0);
662 2 }
663 1 for(i=0;i<no-1;i++)
664 1 {
665 2 I2CON=0x44;
666 2 while(SI==0);
667 2 wdog=1;//+++++++++++++++++++++
668 2 if(I2STAT!=0x50)
669 2 {
670 3 I2CON=0x54;
671 3 return(0);
672 3 }
673 2 *s=I2DAT;
674 2 s++;
675 2 }
676 1 I2CON=0x40;//接收最后一字节数据并发送非应答位
677 1 while(SI==0);
678 1 *s=I2DAT;
679 1 I2CON=0x54;//结束总线
680 1 return(1);
681 1 }
682 /*****************************
683 初始化
684 ******************************/
C51 COMPILER V7.01 PCCKMODBUS 07/23/2004 15:55:57 PAGE 16
685 void chushihua1()
686 {
687 1 scl=0;
688 1 sda=1;
689 1 if(sda==0)
690 1 {
691 2 I2CON=0x54;//11010101B
692 2 // return(0);
693 2 }
694 1 scl=1;
695 1 if(scl==0)
696 1 {
697 2 I2CON=0x54;
698 2 // return(0);
699 2 }
700 1
701 1 P1M1=0x0c;
702 1 P1M2=0x0c;
703 1 // I2ADR= /////////////////
704 1 I2SCLH=75;/*设置IIC波特率*/
705 1 I2SCLL=75;
706 1 I2CON=0x44;//IIC使能使用内部SCL发生器
707 1 //IEN1=1;
708 1 EA=1;
709 1 }
710 /***************************/
711 void main()
712 {
713 1 // bit derror;
714 1 // unsigned int i;
715 1 unsigned char lm,lk;
716 1 IEN0=0;
717 1 IEN1=0;
718 1 wdog=1;
719 1 chushihua();
720 1 chushihua1();
721 1 modadder=0xfe; //debug++++++++++++++++++++++++++++++++++++=================
722 1 click3=0;
723 1 T2buf[0]=0x0;
724 1 T2buf[1]=0x0;
725 1 T2buf[2]=0x0;
726 1 T2buf[3]=0x0;
727 1 T2buf[4]=0x0;
728 1 T2buf[5]=0x0;
729 1 led1=0;
730 1 /*while(click3<=0x01f)led1=1;
731 1 click=0;
732 1 while(click3<=0x01f)led1=0;
733 1 click=0;*/
734 1 I2cadder=&R2buf[0];
735 1 //derror=0;
736 1 if (IRcvStr(0x88,0,I2cadder,50))
737 1 {
738 2 lm=R2buf[5];//modbug
739 2 lm=lm&0xf0;
740 2 lk=R2buf[6];
741 2 if ((R2buf[4]!=0) && (lm==0xe0) & (lk==0xb0))
742 2 { //正确则更改波特率。。。
743 3 modadder=R2buf[4];
744 3 /* 0xfe;
745 3 EA=0;
746 3 SSTAT=0x44; // separate Rx/Tx
C51 COMPILER V7.01 PCCKMODBUS 07/23/2004 15:55:57 PAGE 17
747 3 BRGCON=0;
748 3 SCON=0x50; // ; setup UART mode 1, enables serial reception.
749 3 BRGR1=0x05; // 串口波特率发生器0x2f0-->9600((7372800/n)-16)
750 3 BRGR0=0xf0; //170-->19200,2f0-->9600, 5f0-->4800 ,bf0-->2400 ,17f0-->1200
751 3 BRGCON=0x03; // ; Baud Rate Generator Enable.
752 3 EA=1; */
753 3 }
754 2 /* else
755 2 {
756 2 EA=0;
757 2 SSTAT=0x44; // separate Rx/Tx
758 2 BRGCON=0;
759 2 SCON=0x50; // ; setup UART mode 1, enables serial reception.
760 2 BRGR1=0x05; // 串口波特率发生器0x2f0-->9600((7372800/n)-16)
761 2 BRGR0=0xf0; //170-->19200,2f0-->9600, 5f0-->4800 ,bf0-->2400 ,17f0-->1200
762 2 BRGCON=0x03; // ; Baud Rate Generator Enable.
763 2 EA=1;
764 2 } */
765 2 }
766 1 /*if (derror==0)
767 1 {
768 1 led1=1;
769 1 while(click3<=0x01f); //I2c通讯出错
770 1 led1=0;
771 1 click=0;
772 1 while(click3<=0x01f)
773 1 led1=1;
774 1 click=0;
775 1 while(click3<=0x01f)
776 1 led1=0;
777 1 click=0;
778 1 } */
779 1 //================================debug===
780 1 for (lm=0;lm<50;lm++)
781 1 {
782 2 R2buf[lm]=0;
783 2 }
784 1 //================================debug===
785 1 I2cwr=1;
786 1 while(1)
787 1 {
788 2 wdog=1;
789 2 if ((click3>0x1f) || (I2cwr==0))
790 2 {
791 3 // I2cwr ^=1;
792 3 click3=0;
793 3 if (I2cwr==0)
794 3 {
795 4 I2cadder=&T2buf[0];
796 4 if (ISendStr(0x88,0,I2cadder,30))
797 4 {
798 5 led1^=1;
799 5 I2cwr=1;
800 5 }
801 4 }
802 3 else
803 3 {
804 4 I2cadder=&R2buf[0];
805 4 if (IRcvStr(0x88,0,I2cadder,50))
806 4 {
807 5 lm=R2buf[5];//modbug
808 5 lm=lm&0xf0;
C51 COMPILER V7.01 PCCKMODBUS 07/23/2004 15:55:57 PAGE 18
809 5 lk=R2buf[6];
810 5 if ((R2buf[4]!=0) && (lm==0xe0) & (lk==0xb0))
811 5 { //正确则更改波特率。。。
812 6 modadder=R2buf[4];
813 6 }
814 5 led1^=1;
815 5 I2cwr=1;
816 5 }
817 4 }
818 3 }
819 2 //===========================================debug====
820 2
821 2 //-------------------------------------------debug----
822 2 modbu001();
823 2 }
824 1 }
C51 COMPILER V7.01 PCCKMODBUS 07/23/2004 15:55:57 PAGE 19
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION int0 (BEGIN)
0000 C0E0 PUSH ACC
0002 C083 PUSH DPH
0004 C082 PUSH DPL
; SOURCE LINE # 47
; SOURCE LINE # 49
0006 900000 R MOV DPTR,#debuf
0009 E4 CLR A
000A F0 MOVX @DPTR,A
; SOURCE LINE # 50
000B D082 POP DPL
000D D083 POP DPH
000F D0E0 POP ACC
0011 32 RETI
; FUNCTION int0 (END)
; FUNCTION int2 (BEGIN)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -