⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xiugai.lst

📁 此程序为非接触式IC的读写程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
 932   1      }
 933          
 934          //显示error子函数
 935          void DisplayErrorCard(void)
 936          {
 937   1              write7279(DECODE1,10);
 938   1              write7279(DECODE1+1,0);
 939   1              write7279(DECODE1+2,10);
 940   1              write7279(DECODE1+3,10);
 941   1              write7279(DECODE1+4,14);
 942   1      
 943   1      }
 944          
 945          //24C02子程序
 946          //定时函数
 947          void DelayMs(unsigned int number)
 948          {
 949   1        unsigned char temp;
 950   1       for(;number!=0;number--)
 951   1       {
 952   2        for(temp=112;temp!=0;temp--) ;
 953   2       }
 954   1      }
 955          
 956          //开始总线
 957          void Start()
 958          {
 959   1       SDA=1;
 960   1       SCL=1;
 961   1       SDA=0;
 962   1       SCL=0;
 963   1      }
 964          
 965          //结束总线
 966          void Stop()
 967          {
 968   1       SCL=0;
C51 COMPILER V7.50   XIUGAI                                                                11/27/2006 17:18:35 PAGE 17  

 969   1       SDA=0;
 970   1       SCL=1;
 971   1       SDA=1;
 972   1      
 973   1      }
 974          
 975          
 976          //发 ACK0
 977          void NoAck()
 978          {
 979   1       SDA=1;
 980   1       SCL=1;
 981   1       SCL=0;
 982   1      }
 983          
 984          //测试 ACK
 985          bit TestAck()
 986          {
 987   1       bit ErrorBit;
 988   1       SDA=1;
 989   1       SCL=1;
 990   1       ErrorBit=SDA;
 991   1       SCL=0;
 992   1       return(ErrorBit);
 993   1      }
 994          
 995          //写入 8 个 bit 到 24c02
 996          Write8Bit(unsigned char input)
 997          {
 998   1        unsigned char temp;
 999   1       for(temp=8;temp!=0;temp--)
1000   1       {
1001   2        SDA=(bit)(input&0x80);
1002   2        SCL=1;
1003   2        SCL=0;
1004   2        input=input<<1;
1005   2       }
1006   1      }
1007          
1008          //写入一个字节到 24c02 中
1009          void Write24c02(uchar ch,uchar address)
1010          {
1011   1       Start();
1012   1       Write8Bit(WriteDeviceAddress);
1013   1       TestAck();
1014   1       Write8Bit(address);
1015   1       TestAck();
1016   1      
1017   1      
1018   1       Write8Bit(ch);
1019   1       TestAck();
1020   1      
1021   1       Stop();
1022   1       DelayMs(10);
1023   1      }
1024          
1025          //从 24c02 中读出 8 个 bit
1026          uchar Read8Bit()
1027          {
1028   1        unsigned char temp,rbyte=0;
1029   1       for(temp=8;temp!=0;temp--)
1030   1       {
C51 COMPILER V7.50   XIUGAI                                                                11/27/2006 17:18:35 PAGE 18  

1031   2        SCL=1;
1032   2        rbyte=rbyte<<1;
1033   2        rbyte=rbyte|((unsigned char)(SDA));
1034   2        SCL=0;
1035   2       }
1036   1       return(rbyte);
1037   1      }
1038          
1039          //从 24c02 中读出 1 个字节
1040          uchar Read24c02(uchar address)
1041          {
1042   1       uchar ch;
1043   1      
1044   1       Start();
1045   1       Write8Bit(WriteDeviceAddress);
1046   1       TestAck();
1047   1       Write8Bit(address);
1048   1       TestAck();
1049   1       Start();
1050   1       Write8Bit(ReadDviceAddress);
1051   1       TestAck();
1052   1       ch=Read8Bit();
1053   1       NoAck();
1054   1       Stop();
1055   1       return(ch);
1056   1      }
1057          
1058          //7279子函数//
1059          void write7279(unsigned char cmd,unsigned char dta)
1060          {
1061   1              send_byte(cmd);
1062   1              send_byte(dta);
1063   1      }
1064          
1065          void send_byte(unsigned char out_byte)
1066          {
1067   1              unsigned char i;
1068   1              cs=0;
1069   1              long_delay();
1070   1              for(i=0;i<8;i++)
1071   1              {
1072   2                      if(out_byte&0x80)
1073   2                              {
1074   3                                      dat=1;
1075   3                              }
1076   2                              else
1077   2                                      {
1078   3                                              dat=0;
1079   3                                      }
1080   2                                      clk=1;
1081   2                                      short_delay();
1082   2                                      clk=0;
1083   2                                      short_delay();
1084   2                                      out_byte=out_byte*2;
1085   2         }
1086   1                 dat=0;
1087   1      }
1088          unsigned char receive_byte(void)
1089          {
1090   1              unsigned char i,in_byte;
1091   1              dat=1;
1092   1              long_delay();
C51 COMPILER V7.50   XIUGAI                                                                11/27/2006 17:18:35 PAGE 19  

1093   1              for(i=0;i<8;i++)
1094   1              {
1095   2                      clk=1;
1096   2                      short_delay();
1097   2                      in_byte=in_byte*2;
1098   2                      if(dat)
1099   2                              {
1100   3                      in_byte=in_byte|0x01;
1101   3                        }
1102   2                      clk=0;
1103   2                      short_delay();
1104   2              }
1105   1              dat=0;
1106   1              return(in_byte);
1107   1      }
1108          void long_delay(void)
1109          {
1110   1              unsigned char i;
1111   1              for(i=0;i<0x30;i++);
1112   1      }
1113          void short_delay(void)
1114          {
1115   1              unsigned char i;
1116   1              for(i=0;i<8;i++);
1117   1      }
1118          
1119          void delay10ms(unsigned char time)
1120          {
1121   1              unsigned char i;
1122   1              unsigned int j;
1123   1              for(i=0;i<time;i++)
1124   1              {
1125   2                      for(j=0;j<0x390;j++)
1126   2                      {
1127   3                              if(!key)
1128   3                                      {
1129   4                                              key_int();
*** WARNING C206 IN LINE 1129 OF D:\1.2\XIUGAI.C: 'key_int': missing function-prototype
1130   4                                      }
1131   3                      }
1132   2              }
1133   1      }
1134          
1135          
1136          //IC Card 子程序
1137          /*****************************************************************************
1138          *function: IIC start condition
1139          *IIC起始条件
1140          *****************************************************************************/
1141          void I2CStart(void)
1142          {
1143   1              SDA = 1;
1144   1              SCL = 1;
1145   1              SDA = 0;
1146   1              _nop_();
1147   1              SCL = 0;
1148   1      }
1149          /*****************************************************************************
1150          *function: IIC stop condition
1151          *IIC停止条件
1152          *****************************************************************************/
1153          void I2CStop(void)
C51 COMPILER V7.50   XIUGAI                                                                11/27/2006 17:18:35 PAGE 20  

1154          {
1155   1              SCL = 0;
1156   1              SDA = 0;
1157   1              _nop_();
1158   1              SCL = 1;
1159   1              SDA = 1;
1160   1      }
1161          /*****************************************************************************
1162          *function: IIC wait ACK
1163          *等待IIC从器件的"ACK"
1164          *****************************************************************************/
1165          bit I2CWaitAck(void)
1166          {
1167   1              unsigned char cErrTime = 255;
1168   1              SDA = 1;
1169   1              _nop_();
1170   1              SCL = 1;
1171   1              while(SDA)
1172   1              {
1173   2                      cErrTime--;
1174   2                      if (0 == cErrTime)
1175   2                      {
1176   3                              I2CStop();
1177   3                              return 1;
1178   3                      }
1179   2              }
1180   1              SCL = 0;
1181   1              return SUCCESS;
1182   1      }
1183          /*****************************************************************************
1184          *function: IIC send ACK
1185          *发送"ACK"
1186          *****************************************************************************/
1187          void I2CSendAck(void)
1188          {
1189   1              SDA = 0;
1190   1              _nop_();
1191   1              SCL = 1;
1192   1              SCL = 0;
1193   1      }
1194          /*****************************************************************************
1195          *function: IIC send Not ACK
1196          *发送"NOT ACK"
1197          *****************************************************************************/
1198          void I2CSendNotAck(void)
1199          {
1200   1              SDA = 1;
1201   1              _nop_();
1202   1              SCL = 1;
1203   1              SCL = 0;
1204   1      }
1205          /*****************************************************************************
1206          *function: send a byte over IIC bus
1207          *发送一个字节
1208          *****************************************************************************/
1209          void I2CSendByte(unsigned char cSendByte)
1210          {
1211   1              unsigned char data i = 8;
1212   1              while (i--)
1213   1              {
1214   2                      SCL = 0;
1215   2                      SDA = (bit)(cSendByte & 0x80);
C51 COMPILER V7.50   XIUGAI                                                                11/27/2006 17:18:35 PAGE 21  

1216   2                      cSendByte += cSendByte;
1217   2                      _nop_();
1218   2                      SCL = 1;
1219   2              }
1220   1              SCL = 0;
1221   1      }
1222          /*****************************************************************************
1223          *function: receive a byte over IIC bus
1224          *接收一个字节
1225          *****************************************************************************/
1226          unsigned char I2CReceiveByte(void)
1227          {
1228   1              unsigned char data i = 8;
1229   1              unsigned char data cR_Byte = 0;
1230   1              SDA = 1;
1231   1              while (i--)
1232   1              {
1233   2                      cR_Byte += cR_Byte;
1234   2                      SCL = 0;
1235   2                      _nop_();
1236   2                      _nop_();
1237   2                      SCL = 1;
1238   2                      cR_Byte |= (unsigned char)SDA;
1239   2              }
1240   1              SCL = 0;
1241   1              return cR_Byte;
1242   1      }
1243          /*****************************************************************************
1244          *function: send data to JMY-5xx over IIC bus
1245          *这是向模块发送数据的函数,所有操作模块的命令均由此函数发送
1246          *程序输入的参数是一个指针,由通讯协议所定,指针指向的第一个字节是发送的数据包的长度(不包含最后的校验字节

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -