📄 driver.lst
字号:
562 2 _nop_();
563 2 _nop_();
564 2 _nop_();
565 2 }
566 1 return dat;
567 1 }
568
569
570
571 //每次最多写入32字节(24C64一页32字节)
572 uchar I2cPageWr(uchar device,uint adr,uchar len,uchar *dat)
573 {
574 1 uchar i;
575 1
576 1 if(device == EepromAdr)
577 1 {
578 2 if(len>32)len=32;
579 2 I2cStart();
580 2 I2cSendByte(device);
581 2 if(!WaitAck()) return 0xE0;
582 2 i=(uchar)(adr/256);
583 2 I2cSendByte(i);
584 2 if(!WaitAck()) return 0xE1;
585 2 i=(uchar)(adr % 256);
586 2 I2cSendByte(i);
587 2 if(!WaitAck()) return 0xE2;
588 2 for(i=0;i<len;i++)
589 2 {
590 3 I2cSendByte(dat[i]);
591 3 if(!WaitAck()) return 0xE4;
592 3 }
593 2 I2cStop();
594 2 I2cDelay();
595 2 }
596 1 else
597 1 {
598 2 I2cStart();
599 2 I2cSendByte(device);
600 2 if(!WaitAck()) return 0xE0;
601 2 i=(uchar)(adr);
602 2 I2cSendByte(i);
603 2 if(!WaitAck()) return 0xE2;
604 2 for(i=0;i<len;i++)
605 2 {
606 3 I2cSendByte(dat[i]);
607 3 if(!WaitAck()) return 0xE4;
608 3 }
609 2 I2cStop();
610 2 I2cDelay();
611 2 }
612 1
613 1 // I2cStart();
C51 COMPILER V8.01 DRIVER 03/28/2006 09:48:45 PAGE 11
614 1 // if(device == EepromAdr && len>32)len=32;
615 1 // I2cSendByte(device);
616 1 // if(!WaitAck()) return 0xE0;
617 1 // if(device == EepromAdr)
618 1 // {
619 1 // i=(uchar)(adr/256);
620 1 // I2cSendByte(i);
621 1 // if(!WaitAck()) return 0xE1;
622 1 // i=(uchar)(adr % 256);
623 1 // I2cSendByte(i);
624 1 // if(!WaitAck()) return 0xE2;
625 1 // }
626 1 // else
627 1 // {
628 1 // i=(uchar)adr;
629 1 // I2cSendByte(i);
630 1 // if(!WaitAck()) return 0xE3;
631 1 // }
632 1 // for(i=0;i<len;i++)
633 1 // {
634 1 // I2cSendByte(dat[i]);
635 1 // if(!WaitAck()) return 0xE4;
636 1 // }
637 1 // I2cStop();
638 1 // I2cDelay();
639 1 return 1;
640 1 }
641
642
643
644 uchar I2cWr(uchar device,uint adr,uint len,uchar *dat)
645 {
646 1 uint s;
647 1 uchar *str,bt,i,num;
648 1
649 1 if(device == EepromAdr)
650 1 {
651 2 str=dat;
652 2 s=32-adr%32; //本页还余s个字节
653 2 if(s>=len)
654 2 {
655 3 if(I2cPageWr(device,adr,len,str)==0)return 0;
656 3 return 1;
657 3 }
658 2 if(I2cPageWr(device,adr,s,str)==0)return 0;
659 2 adr+=s;
660 2 len-=s;
661 2 str+=s;
662 2 num=len/32; //还有多少个32
663 2 bt=len%32; //还余多少个
664 2 for(i=0;i<num;i++)
665 2 {
666 3 if(I2cPageWr(device,adr,32,str)==0)return 0;
667 3 adr+=32;str+=32;
668 3 }
669 2 if(I2cPageWr(device,adr,bt,str)==0)return 0;
670 2 return 1;
671 2 }
672 1 // if(device==ClockAdr || device==RfidAdr)
673 1 else
674 1 {
675 2 i=I2cPageWr(device,adr,len,dat);
C51 COMPILER V8.01 DRIVER 03/28/2006 09:48:45 PAGE 12
676 2 return i;
677 2 }
678 1 }
679
680
681
682
683 //************************************************************8
684 //* I2C------> 24c64 驱动*
685 //**************************************************************
686 //--------------------------------------------------------------------------------------------
687 // adr: 24C64地址 len: 读的数据长度 *dat: 读取的数据
688 //--------------------------------------------------------------------------------------------
689 uchar I2cRd(uchar device,uint adr,uint len, uchar *dat)
690 {
691 1 uint i;
692 1 uchar k;
693 1
694 1 if(device == EepromAdr)
695 1 {
696 2 I2cStart();
697 2 I2cSendByte(device);
698 2 if(!WaitAck()) return 0xF0;
699 2 k=(uchar)(adr/256);
700 2 I2cSendByte(k);
701 2 if(!WaitAck()) return 0xF1;
702 2 k=(uchar)(adr % 256);
703 2 I2cSendByte(k);
704 2 if(!WaitAck()) return 0xF2;
705 2 I2cStart();
706 2 I2cSendByte(device | 0x01);
707 2 if(!WaitAck()) return 0xF4;
708 2 i=0;
709 2 while(i<len)
710 2 {
711 3 dat[i] = I2cReceiveByte();
712 3 i++;
713 3 if(i<len)SendAck();
714 3 }
715 2 SendNotAck();
716 2 I2cStop();
717 2 }
718 1 else
719 1 {
720 2 I2cStart();
721 2 I2cSendByte(device);
722 2 if(!WaitAck()) return 0xF0;
723 2 k=(uchar)(adr);
724 2 I2cSendByte(k);
725 2 if(!WaitAck()) return 0xF1;
726 2 I2cStart();
727 2 I2cSendByte(device | 0x01);
728 2 if(!WaitAck()) return 0xF4;
729 2 i=0;
730 2 while(i<len)
731 2 {
732 3 dat[i] = I2cReceiveByte();
733 3 i++;
734 3 if(i<len)SendAck();
735 3 }
736 2 SendNotAck();
737 2 I2cStop();
C51 COMPILER V8.01 DRIVER 03/28/2006 09:48:45 PAGE 13
738 2 }
739 1
740 1 // I2cStart();
741 1 // I2cSendByte(device);
742 1 // if(!WaitAck()) return 0xF0;
743 1 // if(device == EepromAdr)
744 1 // {
745 1 // k=(uchar)(adr/256);
746 1 // I2cSendByte(k);
747 1 // if(!WaitAck()) return 0xF1;
748 1 // k=(uchar)(adr % 256);
749 1 // I2cSendByte(k);
750 1 // if(!WaitAck()) return 0xF2;
751 1 // }
752 1 // else
753 1 // {
754 1 // k=(uchar)adr;
755 1 // I2cSendByte(k);
756 1 // if(!WaitAck()) return 0xF3;
757 1 // }
758 1 // I2cStart();
759 1 // I2cSendByte(device | 0x01);
760 1 // if(!WaitAck()) return 0xF4;
761 1 // i=0;
762 1 // while(i<len)
763 1 // {
764 1 // dat[i] = I2cReceiveByte();
765 1 // i++;
766 1 // if(i<len)SendAck();
767 1 // }
768 1 // SendNotAck();
769 1 // I2cStop();
770 1 return 1;
771 1 }
772
773
774
775 uchar I2cWrite(uint adr,uint len,uchar *dat)
776 {
777 1 if(I2cWr(EepromAdr,adr,len,dat)!=1)
778 1 ErrorHint("写EEPROM出错!",0x800);
779 1 return 1;
780 1 }
781
782
783 uchar I2cReadget(uint adr,uint len, uchar *dat)
784 {
785 1 if(I2cRd(EepromAdr,adr,len,dat)!=1)
786 1 ErrorHint("读EEPROM出错!",0x801);
787 1 return 1;
788 1 }
789
790
791 /*
792 uchar RfidWr(uint adr,uint len,uchar *dat)
793 {
794 uchar k;
795
796 k=I2cWr(RfidAdr,adr,len,dat);
797 // if(k!=1)
798 // ErrorHint("写RFID控制器出错!",k|0x1000);
799 return 1;
C51 COMPILER V8.01 DRIVER 03/28/2006 09:48:45 PAGE 14
800 }
801
802 uchar RfidRd(uint adr,uint len, uchar *dat)
803 {
804 uchar k;
805
806 k=I2cRd(RfidAdr,adr,len,dat);
807 // if(k!=1)
808 // ErrorHint("读RFID控制器出错!",k|0x1000);
809 return 1;
810 }
811 */
812 //读时间函数
813 //调用方式:Readtime(uchar firsttype,uchar count,uchar *buff);返回的值是BCD码格式。
814
815
816 void Readtime(uchar *buff)
817 {
818 1 uchar i,time[8];
819 1
820 1 i=I2cRd(ClockAdr,ClockChip,7,time);
821 1 if(i!=1)
822 1 ErrorHint("读时钟芯片出错!",i|0x8000);
823 1 for(i=0;i<7;i++)buff[i]=time[6-i];
824 1 }
825
826 //连续设置时间,格式为:time[0]-[6]:年,月,日,星期,时,分,秒
827 //调用方式:Setuptime(uchar *time);注:time为BCD码格式
828
829
830 void SetTime(uchar *time)
831 {
832 1 uchar i,temp[8];
833 1
834 1 for(i=0;i<7;i++)
835 1 temp[i]=time[6-i];
836 1 i=I2cWr(ClockAdr,ClockChip,7,temp);
837 1 if(i!=1)
838 1 ErrorHint("写时钟芯片出错!",i|0x8000);
839 1 }
840
841
842 void ReadDate()
843 {
844 1 Readtime(&InvoDate[1]);
845 1 InvoDate[0]=0x20;
846 1 }
847 //************************************************************8
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -