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

📄 text1.lst

📁 SHTxx用数码管显示数据望大家有用
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.05a   TEXT1                                                                07/03/2007 18:01:22 PAGE 1   


C51 COMPILER V8.05a, COMPILATION OF MODULE TEXT1
OBJECT MODULE PLACED IN Text1.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Text1.C BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /* -----------------------       版权声明      ---------------------------------- 
   2            ------------------------------ 创新科技   2007/01/05 --------------------------
   3           
   4            --------------  Mobile:13433018379  (陶志学) ---------------------------------- 
   5            ------       Email:taozhixue123@163.com   --------
   6            未经东莞市创新科技书面同意, 不得将本程序泄露、公开给第三方。
   7            不得将本程序(或修改后的程序)使用在非东莞市创新科技销售的产品上。
   8            客户产品上使用本程序时,客户产品的源程序中必须注明使用了东莞市创新科技的程序,
   9            并保留如下内容:
  10           ******************************************************************************** */
  11          //SHTxx程序演示 89 系列 MCU。 时钟12MHZ
  12          //结果以数码管显示
  13          
  14          
  15          #include <reg51.h> //Microcontroller specific library, e.g. port definitions
  16          #include <intrins.h> //Keil library (is used for _nop()_ operation)  
  17          #include <math.h>    //Keil library  
  18          #include <stdio.h>   //Keil library             
  19          const unsigned char DATA_7SEG[ ] ={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  20          unsigned char led[6];
  21          
  22          typedef union 
  23          { unsigned int i;
  24            float f;
  25          } value;
  26          
  27          //----------------------------------------------------------------------------------
  28          // modul-var
  29          //----------------------------------------------------------------------------------
  30          enum {TEMP,HUMI};
  31          
  32          sbit    DATA= P2^3;
  33          sbit    SCK= P2^7;
  34          
  35          #define noACK 0
  36          #define ACK   1
  37                                      //adr  command  r/w
  38          #define STATUS_REG_W 0x06   //000   0011    0
  39          #define STATUS_REG_R 0x07   //000   0011    1
  40          #define MEASURE_TEMP 0x03   //000   0001    1
  41          #define MEASURE_HUMI 0x05   //000   0010    1
  42          #define RESET        0x1e   //000   1111    0
  43          
  44          //----------------------------------------------------------------------------------
  45          char s_write_byte(unsigned char value)
  46          //----------------------------------------------------------------------------------
  47          // writes a byte on the Sensibus and checks the acknowledge 
  48          { 
  49   1        unsigned char i,error=0;  
  50   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
  51   1        { if (i & value) DATA=1;          //masking value with i , write to SENSI-BUS
  52   2          else DATA=0;                        
  53   2          SCK=1;                          //clk for SENSI-BUS
  54   2          _nop_();_nop_();_nop_();        //pulswith approx. 5 us     
  55   2          SCK=0;
C51 COMPILER V8.05a   TEXT1                                                                07/03/2007 18:01:22 PAGE 2   

  56   2        }
  57   1        DATA=1;                           //release DATA-line
  58   1        SCK=1;                            //clk #9 for ack 
  59   1        error=DATA;                       //check ack (DATA will be pulled down by SHT11)
  60   1        SCK=0;        
  61   1        return error;                     //error=1 in case of no acknowledge
  62   1      }
  63          
  64          //----------------------------------------------------------------------------------
  65          char s_read_byte(unsigned char ack)
  66          //----------------------------------------------------------------------------------
  67          // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" 
  68          { 
  69   1        unsigned char i,val=0;
  70   1        DATA=1;                           //release DATA-line
  71   1        for (i=0x80;i>0;i/=2)             //shift bit for masking
  72   1        { SCK=1;                          //clk for SENSI-BUS
  73   2          if (DATA) val=(val | i);        //read bit  
  74   2          SCK=0;                                       
  75   2        }
  76   1        DATA=!ack;                        //in case of "ack==1" pull down DATA-Line
  77   1        SCK=1;                            //clk #9 for ack
  78   1        _nop_();_nop_();_nop_();          //pulswith approx. 5 us 
  79   1        SCK=0;                                                    
  80   1        DATA=1;                           //release DATA-line
  81   1        return val;
  82   1      }
  83          
  84          //----------------------------------------------------------------------------------
  85          void s_transstart(void)
  86          //----------------------------------------------------------------------------------
  87          // generates a transmission start 
  88          //       _____         ________
  89          // DATA:      |_______|
  90          //           ___     ___
  91          // SCK : ___|   |___|   |______
  92          {  
  93   1         DATA=1; SCK=0;                   //Initial state
  94   1         _nop_();
  95   1         SCK=1;
  96   1         _nop_();
  97   1         DATA=0;
  98   1         _nop_();
  99   1         SCK=0;  
 100   1         _nop_();_nop_();_nop_();
 101   1         SCK=1;
 102   1         _nop_();
 103   1         DATA=1;                 
 104   1         _nop_();
 105   1         SCK=0;                  
 106   1      }
 107          
 108          //----------------------------------------------------------------------------------
 109          void s_connectionreset(void)
 110          //----------------------------------------------------------------------------------
 111          // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
 112          //       _____________________________________________________         ________
 113          // DATA:                                                      |_______|
 114          //          _    _    _    _    _    _    _    _    _        ___     ___
 115          // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
 116          {  
 117   1        unsigned char i; 
C51 COMPILER V8.05a   TEXT1                                                                07/03/2007 18:01:22 PAGE 3   

 118   1        DATA=1; SCK=0;                    //Initial state
 119   1        for(i=0;i<9;i++)                  //9 SCK cycles
 120   1        { SCK=1;
 121   2          SCK=0;
 122   2        }
 123   1        s_transstart();                   //transmission start
 124   1      }
 125          
 126          //----------------------------------------------------------------------------------
 127          char s_softreset(void)
 128          //----------------------------------------------------------------------------------
 129          // resets the sensor by a softreset 
 130          { 
 131   1        unsigned char error=0;  
 132   1        s_connectionreset();              //reset communication
 133   1        error+=s_write_byte(RESET);       //send RESET-command to sensor
 134   1        return error;                     //error=1 in case of no response form the sensor
 135   1      }
 136          
 137          //----------------------------------------------------------------------------------
 138          char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
 139          //----------------------------------------------------------------------------------
 140          // reads the status register with checksum (8-bit)
 141          { 
 142   1        unsigned char error=0;
 143   1        s_transstart();                   //transmission start
 144   1        error=s_write_byte(STATUS_REG_R); //send command to sensor
 145   1        *p_value=s_read_byte(ACK);        //read status register (8-bit)
 146   1        *p_checksum=s_read_byte(noACK);   //read checksum (8-bit)  
 147   1        return error;                     //error=1 in case of no response form the sensor
 148   1      }
 149          
 150          //----------------------------------------------------------------------------------
 151          char s_write_statusreg(unsigned char *p_value)
 152          //----------------------------------------------------------------------------------
 153          // writes the status register with checksum (8-bit)
 154          { 
 155   1        unsigned char error=0;
 156   1        s_transstart();                   //transmission start
 157   1        error+=s_write_byte(STATUS_REG_W);//send command to sensor
 158   1        error+=s_write_byte(*p_value);    //send value of status register
 159   1        return error;                     //error>=1 in case of no response form the sensor
 160   1      }
 161                                                                     
 162          //----------------------------------------------------------------------------------
 163          char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
 164          //----------------------------------------------------------------------------------
 165          // makes a measurement (humidity/temperature) with checksum
 166          { 
 167   1        unsigned error=0;
 168   1        unsigned int i;
 169   1      
 170   1        s_transstart();                   //transmission start
 171   1        switch(mode){                     //send command to sensor
 172   2          case TEMP   : error+=s_write_byte(MEASURE_TEMP); break;
 173   2          case HUMI   : error+=s_write_byte(MEASURE_HUMI); break;
 174   2          default     : break;         
 175   2        }
 176   1        for (i=0;i<65535;i++) if(DATA==0) break; //wait until sensor has finished the measurement
 177   1        if(DATA) error+=1;                // or timeout (~2 sec.) is reached
 178   1        *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)

⌨️ 快捷键说明

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