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

📄 read-ds18b20-lcd1602-c51.lst

📁 基于51的DS18B20_1Wire温度控制等C,ASM源程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.02   READ_DS18B20_LCD1602_C51                                              09/18/2006 17:58:36 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE READ_DS18B20_LCD1602_C51
OBJECT MODULE PLACED IN READ-DS18B20-LCD1602-C51.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE READ-DS18B20-LCD1602-C51.C BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /*******************************************************************/
   2          /*                                                                 */
   3          /* ME300B单片机开发系统演示程序 - DS18B20-64位序列码读取程序       */
   4          /*                                                                 */
   5          /* LCD1602显示                                                     */
   6          /*                                                                 */
   7          /* 版本: V1.0 (2006/11/20)                                        */
   8          /* 作者: gguoqing (Email: gguoqing@willar.com)                    */
   9          /* 网站: www.willar.com(伟纳电子)   www.mcusj.com(伟纳单片机世界) */
  10          /* 时间: 2006/08/12                                               */
  11          /*                                                                 */
  12          /*【版权】Copyright(C)伟纳电子 www.willar.com  All Rights Reserved */
  13          /*【声明】此程序仅用于学习与参考,引用请注明版权和作者信息!       */
  14          /*                                                                 */
  15          /*******************************************************************/
  16          
  17          #include <reg51.h>
  18          #include <intrins.h>
  19          
  20          #define uchar unsigned char
  21          #define uint  unsigned int
  22          
  23          sbit DQ = P3^3;  //定义DS18B20端口DQ  
  24          sbit BEEP=P3^7 ; //蜂鸣器驱动线
  25          
  26          bit  presence ;
  27          
  28          sbit LCD_RS = P2^0;             
  29          sbit LCD_RW = P2^1;
  30          sbit LCD_EN = P2^2;
  31          
  32          uchar code  cdis1[ ] = {"   DS18B20 OK   "};
  33          uchar code  cdis2[ ] = {"                "};
  34          uchar code  cdis3[ ] = {" DS18B20  ERR0R "};
  35          uchar code  cdis4[ ] = {"  PLEASE CHECK  "};
  36          
  37          unsigned char data  display[2] = {0x00,0x00};
  38                                              
  39          unsigned char data  RomCode[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  40          
  41          unsigned char Temp;
  42          unsigned char  crc;
  43          
  44          void beep();
  45          
  46          #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
  47          
  48          /*******************************************************************/
  49          void delay1(int ms)
  50          {
  51   1       unsigned char y;
  52   1        while(ms--)
  53   1       {
  54   2        for(y = 0; y<250; y++)
  55   2        {
C51 COMPILER V8.02   READ_DS18B20_LCD1602_C51                                              09/18/2006 17:58:36 PAGE 2   

  56   3         _nop_();
  57   3         _nop_();
  58   3         _nop_();
  59   3         _nop_();
  60   3        }
  61   2       }
  62   1      }
  63          
  64          /******************************************************************/
  65          /*                                                                */
  66          /*检查LCD忙状态                                                   */
  67          /*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。     */
  68          /*                                                                */
  69          /******************************************************************/ 
  70          
  71          bit lcd_busy()
  72           {                          
  73   1          bit result;
  74   1          LCD_RS = 0;
  75   1          LCD_RW = 1;
  76   1          LCD_EN = 1;
  77   1          delayNOP();
  78   1          result = (bit)(P0&0x80);
  79   1          LCD_EN = 0;
  80   1          return(result); 
  81   1       }
  82          
  83          /*******************************************************************/
  84          /*                                                                 */
  85          /*写指令数据到LCD                                                  */
  86          /*RS=L,RW=L,E=高脉冲,D0-D7=指令码。                             */
  87          /*                                                                 */
  88          /*******************************************************************/
  89          
  90          void lcd_wcmd(uchar cmd)
  91          
  92          {                          
  93   1         while(lcd_busy());
  94   1          LCD_RS = 0;
  95   1          LCD_RW = 0;
  96   1          LCD_EN = 0;
  97   1          _nop_();
  98   1          _nop_(); 
  99   1          P0 = cmd;
 100   1          delayNOP();
 101   1          LCD_EN = 1;
 102   1          delayNOP();
 103   1          LCD_EN = 0;  
 104   1      }
 105          
 106          /*******************************************************************/
 107          /*                                                                 */
 108          /*写显示数据到LCD                                                  */
 109          /*RS=H,RW=L,E=高脉冲,D0-D7=数据。                               */
 110          /*                                                                 */
 111          /*******************************************************************/
 112          
 113          void lcd_wdat(uchar dat)
 114          {                          
 115   1         while(lcd_busy());
 116   1          LCD_RS = 1;
 117   1          LCD_RW = 0;
C51 COMPILER V8.02   READ_DS18B20_LCD1602_C51                                              09/18/2006 17:58:36 PAGE 3   

 118   1          LCD_EN = 0;
 119   1          P0 = dat;
 120   1          delayNOP();
 121   1          LCD_EN = 1;
 122   1          delayNOP();
 123   1          LCD_EN = 0; 
 124   1      }
 125          
 126          /*******************************************************************/
 127          /*                                                                 */
 128          /*  LCD初始化设定                                                  */
 129          /*                                                                 */
 130          /*******************************************************************/
 131          
 132          void lcd_init()
 133          { 
 134   1          delay1(15);   
 135   1          lcd_wcmd(0x01);      //清除LCD的显示内容
 136   1                  
 137   1          lcd_wcmd(0x38);      //16*2显示,5*7点阵,8位数据
 138   1          delay1(5);
 139   1          lcd_wcmd(0x38);         
 140   1          delay1(5);
 141   1          lcd_wcmd(0x38);         
 142   1          delay1(5);
 143   1      
 144   1          lcd_wcmd(0x0c);      //显示开,关光标
 145   1          delay1(5);
 146   1          lcd_wcmd(0x06);      //移动光标
 147   1          delay1(5);
 148   1          lcd_wcmd(0x01);      //清除LCD的显示内容
 149   1          delay1(5);
 150   1      }
 151          
 152          /*******************************************************************/
 153          /*                                                                 */
 154          /*  设定显示位置                                                   */
 155          /*                                                                 */
 156          /*******************************************************************/
 157          
 158          void lcd_pos(uchar pos)
 159          {                          
 160   1        lcd_wcmd(pos | 0x80);  //数据指针=80+地址变量
 161   1      }
 162          
 163          /*******************************************************************/
 164          /*                                                                 */
 165          /*us级延时函数                                                     */
 166          /*                                                                 */
 167          /*******************************************************************/
 168          
 169          void Delay(unsigned int num)
 170          {
 171   1        while( --num );
 172   1      }
 173          
 174          /*******************************************************************/
 175          /*                                                                 */
 176          /*初始化ds1820                                                     */
 177          /*                                                                 */
 178          /*******************************************************************/
 179          Init_DS18B20(void)
C51 COMPILER V8.02   READ_DS18B20_LCD1602_C51                                              09/18/2006 17:58:36 PAGE 4   

 180          {  
 181   1           DQ = 1;      //DQ复位
 182   1           Delay(8);    //稍做延时
 183   1      
 184   1           DQ = 0;      //将DQ拉低
 185   1           Delay(90);   //精确延时 大于 480us
 186   1      
 187   1           DQ = 1;       //拉高总线
 188   1           Delay(8);
 189   1      
 190   1           presence = DQ;    //读取存在信号
 191   1           Delay(100);
 192   1           DQ = 1; 
 193   1           
 194   1           return(presence); //返回信号,0=presence,1= no presence
 195   1      }
 196          
 197          /*******************************************************************/
 198          /*                                                                 */
 199          /* 读一位(bit)                                                   */
 200          /*                                                                 */
 201          /*******************************************************************/
 202          uchar read_bit(void) 
 203          {
 204   1      unsigned char i;
 205   1      DQ = 0;                  //将DQ 拉低开始读时间隙
 206   1      DQ = 1;                         // then return high
 207   1      for (i=0; i<3; i++);    // 延时15μs
 208   1      return(DQ);                     // 返回 DQ 线上的电平值
 209   1      }
 210          
 211          /*******************************************************************/
 212          /*                                                                 */
 213          /* 读一个字节                                                      */
 214          /*                                                                 */
 215          /*******************************************************************/
 216           ReadOneChar(void)
 217          {
 218   1      unsigned char i = 0;
 219   1      unsigned char dat = 0;
 220   1      
 221   1      //for (i = 8; i > 0; i--)
 222   1      //  {
 223   1      //       read_bit();
 224   1      //    DQ = 0; // 给脉冲信号
 225   1      //     dat >>= 1;
 226   1      //    DQ = 1; // 给脉冲信号
 227   1      for (i=0;i<8;i++) 
 228   1      {                               // 读取字节,每次读取一个字节
 229   2      if(read_bit()) dat|=0x01<<i;    // 然后将其左移
 230   2      
 231   2      //    if(DQ)
 232   2      //     dat |= 0x80;
 233   2          Delay(4);
 234   2        }
 235   1      
 236   1          return (dat);

⌨️ 快捷键说明

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