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

📄 ds1631.lst

📁 ds1631温度传感器程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 219   2                      I2CBitDly();
 220   2                      SCL = 0;
 221   2                      I2CBitDly();
 222   2                      bt = bt << 1;
 223   2              }
 224   1              checkack();
 225   1              if(ackFlag == 0)            // 如果非应答
 226   1              {
 227   2                      NACK = 1;
 228   2                      return;            // 若非应答表明器件错误或已坏,置错误标志位NACK
 229   2              }
 230   1      
 231   1      }
 232          
 233          
 234          void I2CSendAddr(unsigned char addr, unsigned char rd)
 235          {
 236   1              start();
 237   1              I2CSendByte(addr+rd); // send address byte
 238   1      }
 239          
 240          unsigned char I2CGetByte(unsigned char lastone) // last one == 1 for last byte; 0 for any other byte
 241          {
C51 COMPILER V7.50   DS1631                                                                12/18/2006 13:06:56 PAGE 5   

 242   1              register unsigned char i, res;
 243   1              res = 0;
 244   1              for (i=0;i<8;i++) // Each bit at a time, MSB first
 245   1              {
 246   2                      I2CSCLHigh();
 247   2                      I2CBitDly();
 248   2                      res = res<<1;
 249   2                      if (SDA) res++;
 250   2                      SCL = 0;
 251   2                      I2CBitDly();
 252   2              }
 253   1              SDA = lastone;  // Send ACK according to 'lastone'
 254   1              I2CSCLHigh();
 255   1              I2CBitDly();
 256   1              SCL = 0;
 257   1              SDA = 1;       // end transmission
 258   1              SCL = 1;
 259   1              I2CBitDly();
 260   1              return(res);
 261   1      }
 262          
 263          void WriteConfig(unsigned char Address, unsigned char Data) //Pass Device Address such as 0x90h or 0x92h
 264          {
 265   1                                                          // Routine to write data to Config Register
 266   1           I2CSendAddr(Address,WRITE); // send START and control byte
 267   1           I2CSendByte (0xAC); // send Access Config command byte
 268   1           I2CSendByte (Data); // send data
 269   1           stop(); // send STOP
 270   1      }
 271          
 272          
 273          void GetConfig(unsigned char Address) //Pass an Address such as 0x90h or 0x92h
 274          {
 275   1                                            // Routine to read data from Config Register
 276   1           I2CSendAddr(Address,WRITE); // send START and control byte
 277   1           I2CSendByte(0xAC); // send Access Config command byte
 278   1           I2CSendAddr(Address,READ); // send repeat START and control byte
 279   1           Config = I2CGetByte(1); // read Config Register
 280   1           stop(); // send STOP
 281   1      }
 282          /*******************************************************
 283          
 284          
 285          void recnbyte()//uchar sla, uchar no)
 286          {
 287                  uchar i,sla=0x90;
 288          
 289                  start();                        // 发送启动信号
 290                  sendbyte(sla);
 291          
 292                  checkack();                     // 检查应答位
 293                  if(ackFlag == 0)
 294                  {
 295                          NACK = 1;
 296                          return;
 297                  }
 298          
 299          
 300                  start();
 301                  sendbyte(sla+1);          // 发送控制字  读DS1631信息
 302                  checkack();
 303                  if(ackFlag == 0)
C51 COMPILER V7.50   DS1631                                                                12/18/2006 13:06:56 PAGE 6   

 304                  {
 305                          NACK = 1;
 306                          return;
 307                  }
 308          //      p = rbuf;                       // 接收字节存放在rbuf中,指针P指向rbuf
 309                  for(i=0;i<2;i++)
 310                  {
 311                          rbuf[i] = recbyte();
 312          
 313                          ack();                  // 收到一个字节后,MCU主机发送一个应答位
 314          
 315                  }
 316                  n_ack();                // 收到最后一个字节后发送一个非应答位
 317                  stop();
 318          }
 319          ********************************************************/
 320          
 321          
 322          /* 主函数,模拟实现I2C总线的数据收发 */
 323          void main(void)
 324          {
 325   1      
 326   1              unsigned char Address = 0x90;
 327   1            WriteConfig(0x90,0x0e);
 328   1            GetConfig(0x90);
 329   1      
 330   1        while(1)
 331   1        {
 332   2              I2CSendAddr(Address,WRITE); // send START and control byte
 333   2              I2CSendByte(0x51); // send Start Convert Tcommand byte
 334   2              stop(); // send STOP
 335   2              I2CBitDly(); // wait
 336   2      
 337   2              I2CSendAddr(Address,WRITE); // send START and control byte
 338   2              I2CSendByte(0xAA); // send Read Temperature command byte
 339   2      
 340   2              I2CSendAddr(Address,READ); // send repeat START and control byte
 341   2              MSB = I2CGetByte(0); // read Temp MSB
 342   2              LSB = I2CGetByte(1); // read Temp LSB
 343   2              stop(); // send STOP
 344   2      
 345   2              if(MSB>=0x80) //if sign bit is set, then temp is negative
 346   2              temp_c = (float)((MSB<<8 | LSB) - 65536) * 0.0625;
 347   2              else
 348   2              temp_c = (float)((MSB<<8|LSB) * 0.00390625);
 349   2              /*
 350   2              temp_f =( temp_c * 9/5) + 32;
 351   2      */
 352   2      
 353   2        }
 354   1      
 355   1      }
 356          
 357          
 358          
 359          
 360          
C51 COMPILER V7.50   DS1631                                                                12/18/2006 13:06:56 PAGE 7   

NAME                                    CLASS   MSPACE  TYPE    OFFSET  SIZE
====                                    =====   ======  ====    ======  ====


n_ack. . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
temp_f . . . . . . . . . . . . . . . .  PUBLIC   DATA   FLOAT    0000H  4
P1 . . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   0090H  1
P3 . . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   00B0H  1
I2CBitDly. . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  time_end . . . . . . . . . . . . . .  * REG *  DATA   U_INT    0006H  2
  index. . . . . . . . . . . . . . . .  * REG *  DATA   U_INT    0004H  2
IE . . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   00A8H  1
IP . . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   00B8H  1
nackFlag . . . . . . . . . . . . . . .  PUBLIC   DATA   BIT      0000H  1
NACK . . . . . . . . . . . . . . . . .  PUBLIC   DATA   BIT      0001H  1
I2CSCLHigh . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  err. . . . . . . . . . . . . . . . .  AUTO     DATA   INT      0000H  2
main . . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  Address. . . . . . . . . . . . . . .  AUTO     DATA   U_CHAR   0000H  1
_I2CSendAddr . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  addr . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0004H  1
  rd . . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0005H  1
SLAdd. . . . . . . . . . . . . . . . .  PUBLIC   IDATA  U_CHAR   0000H  1
ackFlag. . . . . . . . . . . . . . . .  PUBLIC   DATA   BIT      0002H  1
delay3us . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  i. . . . . . . . . . . . . . . . . .  * REG *  DATA   U_INT    0006H  2
ack. . . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
start. . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
delay5us . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0004H  -----
  i. . . . . . . . . . . . . . . . . .  * REG *  DATA   U_INT    0006H  2
rbuf . . . . . . . . . . . . . . . . .  PUBLIC   IDATA  ARRAY    0001H  8
checkack . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
sbuf . . . . . . . . . . . . . . . . .  PUBLIC   IDATA  ARRAY    0009H  8
SCON . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   0098H  1
TCON . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   0088H  1
_I2CSendByte . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  bt . . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0003H  1
  i. . . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0002H  1
_sendbyte. . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  ch . . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0007H  1
  n. . . . . . . . . . . . . . . . . .  AUTO     IDATA  U_CHAR   0000H  1
  temp . . . . . . . . . . . . . . . .  AUTO     IDATA  U_CHAR   0001H  1
_WriteConfig . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  Address. . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0007H  1
  Data . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0001H  1
stop . . . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
_I2CGetByte. . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  lastone. . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0001H  1
  i. . . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0002H  1
  res. . . . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0003H  1
SDA. . . . . . . . . . . . . . . . . .  ABSBIT   -----  BIT      0094H  1
Config . . . . . . . . . . . . . . . .  PUBLIC   DATA   U_CHAR   0004H  1
_GetConfig . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  Address. . . . . . . . . . . . . . .  * REG *  DATA   U_CHAR   0001H  1
SLAdd1 . . . . . . . . . . . . . . . .  PUBLIC   IDATA  U_CHAR   0011H  1
LSB. . . . . . . . . . . . . . . . . .  PUBLIC   DATA   U_CHAR   0005H  1
MSB. . . . . . . . . . . . . . . . . .  PUBLIC   DATA   U_CHAR   0006H  1
SCL. . . . . . . . . . . . . . . . . .  ABSBIT   -----  BIT      0096H  1
T2CON. . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   00C8H  1
recbyte. . . . . . . . . . . . . . . .  PUBLIC   CODE   PROC     0000H  -----
  n. . . . . . . . . . . . . . . . . .  AUTO     IDATA  U_CHAR   0000H  1
C51 COMPILER V7.50   DS1631                                                                12/18/2006 13:06:56 PAGE 8   

NAME                                    CLASS   MSPACE  TYPE    OFFSET  SIZE
====                                    =====   ======  ====    ======  ====


  temp . . . . . . . . . . . . . . . .  AUTO     IDATA  U_CHAR   0001H  1
PSW. . . . . . . . . . . . . . . . . .  SFR      DATA   U_CHAR   00D0H  1
temp_c . . . . . . . . . . . . . . . .  PUBLIC   DATA   FLOAT    0007H  4


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    544    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     11       3
   IDATA SIZE       =     18       4
   BIT SIZE         =      3    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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