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

📄 ds1820.lst

📁 零基础单片机C语言设计全套教程
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   DS1820                                                                08/29/2008 11:02:01 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE DS1820
OBJECT MODULE PLACED IN DS1820.OBJ
COMPILER INVOKED BY: C:\Program Files\Keil\C51\BIN\C51.EXE DS1820.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include <stdio.h>                                              //头文件
   2          #include <reg52.h>
   3          
   4          #define uchar unsigned char                             //预定义
   5          #define FALSE 0
   6          #define TRUE 1
   7          
   8          sbit ONEWIRE_DQ = P2^0;                 //定义P2^0引脚为DS18S20的DQ引脚
   9          
  10          /******************************* 全局变量声明 ********************************/
  11          uchar DS18S20ROM[8];                            // DS18S20 ROM位
  12          uchar ROMFound[5][8];                                   // DS18S20的ROM 代码表
  13          uchar CRCdsc;                                                   //用于CRC校验
  14          
  15          /******************************* 子函数声明 ********************************/
  16          void Delay(int useconds);
  17          uchar Reset(void);
  18          void WriteBit(char val);
  19          void WriteByte(char val);
  20          uchar ReadBit(void);
  21          uchar ReadByte(void);
  22          void ReadROMNumber(void);
  23          uchar CRCCheck( uchar x);
  24          uchar SearchDevice(uchar EndFlag, uchar LastData);
  25          uchar FindFirstDevice(void);
  26          void ReadData(void);
  27          void FindDevices(void);
  28          void ReadTemperature(void);
  29          
  30          /******************************* 主函数 ********************************/
  31          void main(void) 
  32          {
  33   1      uchar MenuOption;                                       //功能选择
  34   1      uchar LastData = 0; 
  35   1      uchar EndFlag = 0; 
  36   1      
  37   1      SCON=0x50;                                                      //初始化串行口模式1
  38   1      TMOD=0x20;                                                      //初始化T1为定时功能,模式2
  39   1      PCON=0x80;                                                      //设置SMOD=1
  40   1      TL1=0xF4;                                                       //波特率4800bit/s,初值
  41   1      TH1=0xF4;
  42   1      
  43   1        TR1 = 1;                                                      //启动T1
  44   1        TI = 1;                                                               //启动发送
  45   1      
  46   1        while(1)                                                              //主循环
  47   1      {                                               
  48   2        printf (" AT89S52 Control 1-Wire DS18S20\n");
  49   2        printf("\n*********************************************************\n");
  50   2        printf (" Select Control Menu Option\n");
  51   2        printf (" 1. Reset All 1-Wire Devices.\n");
  52   2        printf (" 2. Read DS18S20 ROM Number.\n");
  53   2        printf (" 3. Search DS18S20 ROM.\n");
  54   2        printf (" 4. Find All DS18S20 Devices.\n");
  55   2        printf (" 5. Read DS18S20 Scratch.\n");
C51 COMPILER V8.08   DS1820                                                                08/29/2008 11:02:01 PAGE 2   

  56   2        printf (" 6. Read Temperature.\n");
  57   2        printf (" 7. Exit Program.\n");
  58   2        printf (" Please Input Menu Option:\n");
  59   2      
  60   2              MenuOption = _getkey();                                 //从键盘输入选择数字
  61   2              switch(MenuOption)
  62   2      {
  63   3                      case '1':                                               //复位1-Wire总线
  64   3                              printf ("\n You Select 1. Reset All 1-Wire Devices .\n");
  65   3                              printf("\nResetting!......\n");
  66   3                              Reset();                                        //复位函数
  67   3                              printf("\nReset Over!\n");
  68   3                              break;
  69   3                      case '2':                                               //读DS18S20 ROM
  70   3                              printf ("\n You Select 2. Read DS18S20 ROM Number. \n");
  71   3                              printf("\nReading!......\n");
  72   3                              ReadROMNumber();                        //读取ROM代码函数
  73   3                              printf("\nRead Over!\n");
  74   3                              break;
  75   3                      case '3':                                               //搜索DS18S20
  76   3                              printf("\n You Select 3. Search DS18S20 ROM \n");
  77   3                              printf("\nSearching!......\n");
  78   3                              FindFirstDevice();                      //搜索第一个器件
  79   3                              printf("\nDS18S20 ROM CODE =%02X%02X%02X%02X\n",
  80   3                                ROMFound[5][7],ROMFound[5][6],ROMFound[5][5],ROMFound[5][4],
  81   3                                ROMFound[5][3],ROMFound[5][2],ROMFound[5][1],ROMFound[5][0]);
  82   3                              printf("\nSearch Over!\n");
  83   3                              break;
  84   3                      case '4':                                               //搜索所有DS18S20
  85   3                              printf ("\n You Select 4. Find All DS18S20\n");
  86   3                              printf("\nFinding!......\n");
  87   3                              FindDevices();                          //查找器件函数
  88   3                              printf("\nFind Over!\n");
  89   3                              break;
  90   3                      case '5':                                               //读取高速暂存器
  91   3                              printf ("\n You Select 5. Read DS18S20 Scratch \n");
  92   3                              printf("\nReading!......\n");
  93   3                              WriteByte(0xCC);                        //跳过ROM序列号命令(代码为CCH)
  94   3                              ReadData();                             //读取高速暂存器
  95   3                              printf("\nRead Over!\n");
  96   3                              break;
  97   3                      case '6':                                               //读取温度
  98   3                              printf ("\n You Select 6. Read DS18S20 Temperatur\n");
  99   3                              printf("\nReading!......\n");
 100   3                              ReadTemperature();              //读取温度值
 101   3                              printf("\nRead Over!\n");
 102   3                              break;
 103   3                      case '7':                                               //退出程序
 104   3                              printf ("\n You Select 7. Exit\n");
 105   3                              goto Exit;                              //转向Exit标号处
 106   3                              break;
 107   3                      default: 
 108   3      printf ("\n Error: Please Select Right Menu Option\n");
 109   3                              break;
 110   3                      }; 
 111   2        }
 112   1      Exit: printf("Exit the program!");                              //退出
 113   1      }
 114          
 115          
 116          
 117           void Delay(int useconds) 
C51 COMPILER V8.08   DS1820                                                                08/29/2008 11:02:01 PAGE 3   

 118          {
 119   1      int s,t;
 120   1      for (s=0; s<useconds;s++)                                       //双重循环
 121   1      {
 122   2              for(t=0;t<10;t++);                                      //空循环语句实现延时
 123   2      }
 124   1      }
 125          
 126          uchar Reset(void) 
 127          {
 128   1      uchar PresencePalse;
 129   1      ONEWIRE_DQ = 0;                                         //拉低数据线DQ
 130   1      Delay(3);                                                               //延时
 131   1      ONEWIRE_DQ = 1;                                                 //置数据线DQ为高电平
 132   1      Delay(1);                                                               //延时
 133   1      PresencePalse = ONEWIRE_DQ;                     //读取存在信号
 134   1      Delay(3);                                                               //延时,等待时间隙结束
 135   1      if(PresencePalse)                                               //返回存在信号
 136   1              return 1;
 137   1      else
 138   1              return 0;
 139   1      }
 140          
 141          void WriteBit(char val) 
 142          {
 143   1      ONEWIRE_DQ = 0;                                                 //写时间隙
 144   1      if(val==1)
 145   1       ONEWIRE_DQ =1;                                         //数据线DQ置1,写1 
 146   1      else
 147   1       ONEWIRE_DQ=0;                                          //数据线DQ置0,写0
 148   1      Delay(1);                                                               //延时,在时间隙内保持电平值,
 149   1      ONEWIRE_DQ = 1;                                                 //拉高数据线DQ
 150   1      }
 151          
 152          void WriteByte(char val) 
 153          {
 154   1      uchar i;
 155   1      uchar temp;
 156   1      for (i=0; i<8; i++)                                             //循环写入字节,每次写入一位
 157   1      { 
 158   2              temp = val>>i;                                          //移位
 159   2              temp &= 0x01;           
 160   2              WriteBit(temp);                                         //逐位写
 161   2      }
 162   1      }
 163          
 164          uchar ReadBit(void) 
 165          {
 166   1      ONEWIRE_DQ = 0;                                         //读时间隙
 167   1      ONEWIRE_DQ = 1;                                                 //DQ置1
 168   1      Delay(1);                                                               //延时
 169   1      if(ONEWIRE_DQ)                                          //返回数据总线DQ上的位数据
 170   1              return 1;
 171   1      else
 172   1              return 0;
 173   1      }
 174          
 175          uchar ReadByte(void) 
 176          {
 177   1      uchar i;
 178   1      uchar value = 0;
 179   1      for (i=0;i<8;i++)                                               //读取字节,每次读取一位
C51 COMPILER V8.08   DS1820                                                                08/29/2008 11:02:01 PAGE 4   

 180   1      {
 181   2      if(ReadBit()) 
 182   2      value|=0x01<<i;                                         //循环左移
 183   2      else
 184   2              ;                                                               //空语句
 185   2      Delay(1);                                       
 186   2      }
 187   1      return(value);                                                  //返回字节数据
 188   1      }
 189          
 190          void ReadROMNumber(void) 
 191          {
 192   1      int n;
 193   1      char dat[9];
 194   1      printf("\nReading DS18S20 ROM Number\n");       //输出信息
 195   1      Reset();                                                                //复位子函数
 196   1      WriteByte(0x33);                                                //读出ROM序列号命令
 197   1      for (n=0;n<8;n++)
 198   1      {
 199   2              dat[n]=ReadByte();                                      //循环读ROM序列号
 200   2      }
 201   1      printf("\nDS18S20 ROMNumber= ");                        //输出ROM序列号

⌨️ 快捷键说明

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