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

📄 3_ds.lst

📁 51单片机同时读写3个ds18B20和一个SHT10
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V8.09   3_DS                                                                  01/20/2009 09:40:18 PAGE 1   


C51 COMPILER V8.09, COMPILATION OF MODULE 3_DS
OBJECT MODULE PLACED IN 3_ds.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE 3_ds.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include<reg52.h> //Microcontroller specific library, e.g. port definitions
   2          #include <intrins.h> //Keil library (is used for _nop()_ operation)  
   3          #include <math.h>    //Keil library  
   4          #include <stdio.h>   //Keil library
   5          #include <absacc.h>
   6          
   7          
   8          
   9          typedef union 
  10          { unsigned int i;
  11            float f;
  12          } value;
  13          
  14          //----------------------------------------------------------------------------------
  15          // modul-var
  16          //----------------------------------------------------------------------------------
  17          enum {TEMP,HUMI};
  18          
  19          //#define       DATA    P1_0
  20          //#define       SCK     P1_1
  21          
  22          #define noACK 0
  23          #define ACK   1
  24                                      //adr  command  r/w
  25          #define STATUS_REG_W 0x06   //000   0011    0
  26          #define STATUS_REG_R 0x07   //000   0011    1
  27          #define MEASURE_TEMP 0x03   //000   0001    1
  28          #define MEASURE_HUMI 0x05   //000   0010    1
  29          #define RESET        0x1e   //000   1111    0
  30          
  31          
  32          #define uchar unsigned char 
  33          #define uint unsigned int 
  34          
  35          uchar tplsb,tpmsb;              // 温度值低位、高位字节 
  36          uchar ds_num;
  37          
  38          unsigned char xdata redata[8];   
  39          unsigned char xdata sedata[13];
  40          
  41          
  42          //sbit DQ = P0^1;                       // 数据通信线DQ
  43          
  44          union{
  45                  unsigned int  ds1_temp;   
  46                  unsigned char  ds1_t[2]; 
  47          
  48          }ds1;
  49          
  50          union{
  51                  unsigned int  ds2_temp;   
  52                  unsigned char  ds2_t[2]; 
  53          
  54          }ds2;
  55          
C51 COMPILER V8.09   3_DS                                                                  01/20/2009 09:40:18 PAGE 2   

  56          union{
  57                  unsigned int  ds3_temp;   
  58                  unsigned char  ds3_t[2]; 
  59          
  60          }ds3;
  61          
  62          
  63          union{
  64                  unsigned int  s_temp;   
  65                  unsigned char  s_t[2];
  66          
  67          }s;
  68          
  69          sbit DATA=P1^0;
  70          sbit SCK=P1^1;
  71          //sbit int485=P3^2;
  72          //unsigned char *tm;
  73          
  74          //unsigned int s_temph,s_templ,ds_temph,ds_templ;
  75          //float s_tem;
  76          //float xdata ds1_tem,ds2_tem,ds3_tem;
  77          
  78          
  79          //----------------------------------------------------------------------------------
  80          char s_write_byte(unsigned char value)
  81          //----------------------------------------------------------------------------------
  82          // writes a byte on the Sensibus and checks the acknowledge 
  83          { 
  84   1        unsigned char i,error=0;  
  85   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
  86   1        { if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS
  87   2          else DATA=0;                        
  88   2          SCK=1;                          //clk for SENSI-BUS
  89   2          _nop_();_nop_();_nop_();        //pulswith approx. 5 us     
  90   2          SCK=0;
  91   2        }
  92   1        DATA=1;                           //release DATA-line
  93   1        SCK=1;                            //clk #9 for ack 
  94   1        error=DATA;                       //check ack (DATA will be pulled down by SHT11)
  95   1        SCK=0;        
  96   1        return error;                     //error=1 in case of no acknowledge
  97   1      }
  98          
  99          //----------------------------------------------------------------------------------
 100          char s_read_byte(unsigned char ack)
 101          //----------------------------------------------------------------------------------
 102          // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 
 103          { 
 104   1        unsigned char i,val=0;
 105   1        DATA=1;                           //release DATA-line
 106   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
 107   1        { SCK=1;                          //clk for SENSI-BUS
 108   2          if (DATA) val=(val | i);        //read bit  
 109   2          SCK=0;                                       
 110   2        }
 111   1        DATA=!ack;                        //in case of "ack==1" pull down DATA-Line
 112   1        SCK=1;                            //clk #9 for ack
 113   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us 
 114   1        SCK=0;                                                    
 115   1        DATA=1;                           //release DATA-line
 116   1        return val;
 117   1      }
C51 COMPILER V8.09   3_DS                                                                  01/20/2009 09:40:18 PAGE 3   

 118          
 119          //----------------------------------------------------------------------------------
 120          void s_transstart(void)
 121          //----------------------------------------------------------------------------------
 122          // generates a transmission start 
 123          //       _____         ________
 124          // DATA:      |_______|
 125          //           ___     ___
 126          // SCK : ___|   |___|   |______
 127          {  
 128   1         DATA=1; SCK=0;                   //Initial state
 129   1         _nop_();
 130   1         SCK=1;
 131   1         _nop_();
 132   1         DATA=0;
 133   1         _nop_();
 134   1         SCK=0;  
 135   1         _nop_();_nop_();_nop_();
 136   1         SCK=1;
 137   1         _nop_();
 138   1         DATA=1;                 
 139   1         _nop_();
 140   1         SCK=0;                  
 141   1      }
 142          
 143          //----------------------------------------------------------------------------------
 144          void s_connectionreset(void)
 145          //----------------------------------------------------------------------------------
 146          // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
 147          //       _____________________________________________________         ________
 148          // DATA:                                                      |_______|
 149          //          _    _    _    _    _    _    _    _    _        ___     ___
 150          // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
 151          {  
 152   1        unsigned char i; 
 153   1        DATA=1; SCK=0;                    //Initial state
 154   1        for(i=0;i<9;i++)                  //9 SCK cycles
 155   1        { SCK=1;
 156   2          SCK=0;
 157   2        }
 158   1        s_transstart();                   //transmission start
 159   1      }
 160          
 161          //----------------------------------------------------------------------------------
 162          char s_softreset(void)
 163          //----------------------------------------------------------------------------------
 164          // resets the sensor by a softreset 
 165          { 
 166   1        unsigned char error=0;  
 167   1        s_connectionreset();              //reset communication
 168   1        error+=s_write_byte(RESET);       //send RESET-command to sensor
 169   1        return error;                     //error=1 in case of no response form the sensor
 170   1      }
 171          
 172          //----------------------------------------------------------------------------------
 173          char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
 174          //----------------------------------------------------------------------------------
 175          // reads the status register with checksum (8-bit)
 176          { 
 177   1        unsigned char error=0;
 178   1        s_transstart();                   //transmission start
 179   1        error=s_write_byte(STATUS_REG_R); //send command to sensor
C51 COMPILER V8.09   3_DS                                                                  01/20/2009 09:40:18 PAGE 4   

 180   1        *p_value=s_read_byte(ACK);        //read status register (8-bit)
 181   1        *p_checksum=s_read_byte(noACK);   //read checksum (8-bit)  
 182   1        return error;                     //error=1 in case of no response form the sensor
 183   1      }
 184          
 185          //----------------------------------------------------------------------------------
 186          char s_write_statusreg(unsigned char *p_value)
 187          //----------------------------------------------------------------------------------
 188          // writes the status register with checksum (8-bit)
 189          { 
 190   1        unsigned char error=0;
 191   1        s_transstart();                   //transmission start
 192   1        error+=s_write_byte(STATUS_REG_W);//send command to sensor
 193   1        error+=s_write_byte(*p_value);    //send value of status register
 194   1        return error;                     //error>=1 in case of no response form the sensor
 195   1      }
 196                                                                     
 197          //----------------------------------------------------------------------------------
 198          char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
 199          //----------------------------------------------------------------------------------
 200          // makes a measurement (humidity/temperature) with checksum
 201          { 
 202   1        unsigned error=0;
 203   1        unsigned int i;
 204   1      
 205   1        s_transstart();                   //transmission start
 206   1        switch(mode){                     //send command to sensor
 207   2          case TEMP   : error+=s_write_byte(MEASURE_TEMP); break;
 208   2          case HUMI   : error+=s_write_byte(MEASURE_HUMI); break;

⌨️ 快捷键说明

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