12864显示DS18B20温度曲线

源代码在线查看: w.lst

软件大小: 33 K
上传用户: dounob
关键词: 12864 18B B20 DS
下载地址: 免注册下载 普通下载 VIP

相关代码

				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 1   
				
				
				C51 COMPILER V9.01, COMPILATION OF MODULE W
				OBJECT MODULE PLACED IN w.OBJ
				COMPILER INVOKED BY: e:\Keil\C51\BIN\C51.EXE w.c BROWSE DEBUG OBJECTEXTEND
				
				line level    source
				
				   1          #include 
				   2          #define uint unsigned int
				   3          #define uchar unsigned char 
				   4          
				   5          sbit rs=P2^0;      //数据\指令 选择
				   6          sbit rw=P2^1;      //读/写 选择   
				   7          sbit e=P2^2;       //读/写 使能 
				   8          sbit ds=P1^0;      //18B20信号脚
				   9          uint temp;
				  10          float ftemp;
				  11          uchar code table[]="0123456789.CT:";
				  12          
				  13          void writecom(uchar com);       //写命令函数
				  14          void writedat(uchar dat);       //写数据函数
				  15          void initinal(void);
				  16          
				  17          void delay50us(uint t)          //12MHZ晶振 延时50微秒 
				  18          {
				  19   1              uchar j;
				  20   1                      for(;t>0;t--)
				  21   1                              for(j=19;j>0;j--);
				  22   1      }
				  23          void dsreset()                          //ds18b20初始化函数
				  24          {
				  25   1              uint i;
				  26   1              ds=0;                           //拉低总线开始复位
				  27   1              i=103;
				  28   1              while(i>0)i--;
				  29   1              ds=1;                                   //释放总线
				  30   1              i=4;
				  31   1              while(i>0)i--;                  //省去了等待芯片返回应答的过程
				  32   1      }
				  33          bit readbit(void)                       //ds18b20读取一个二进制位,参照时序图
				  34          {
				  35   1              uint i;
				  36   1              bit dat;
				  37   1              ds=0;i++;
				  38   1              ds=1;i++,i++;
				  39   1              dat=ds;
				  40   1              i=8;while(i>0)i--;
				  41   1              return(dat);
				  42   1      }
				  43          uchar tempread(void)            //ds18b20读取一个字节的数据
				  44          {
				  45   1              uchar i,j,dat;
				  46   1              dat=0;
				  47   1              for(i=0;i				  48   1              {
				  49   2                      j=readbit();
				  50   2                      dat=(j1);  
				  51   2              }
				  52   1              return(dat);
				  53   1      }
				  54          void tempwrite(uchar dat)       //ds18b20写入字节数据
				  55          {
				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 2   
				
				  56   1              uint i;uchar j;
				  57   1              bit testb;
				  58   1              for(j=1;j				  59   1                      {
				  60   2                              testb=dat&0x01;
				  61   2                              dat=dat>>1;
				  62   2                          if(testb==1)        //写1
				  63   2                                 {
				  64   3                                      ds=0;       //拉低总线以开始一个写时序
				  65   3                                              i++;i++;
				  66   3                                              ds=1;           //释放总线
				  67   3                                              i=8;
				  68   3                                              while(i>0) i--;
				  69   3                                 }
				  70   2                              else                            //写0
				  71   2                                 {
				  72   3                                              ds=0;
				  73   3                                              i=8;
				  74   3                                              while(i>0)i--;
				  75   3                                              ds=1;
				  76   3                                              i++;i++;
				  77   3                                 }
				  78   2                      }
				  79   1      }
				  80          void tempwendu(void)            //转换温度
				  81          {
				  82   1               dsreset();                             //ds18b20初始化函数
				  83   1               delay50us(20); 
				  84   1               tempwrite(0xcc);   
				  85   1               tempwrite(0x44); 
				  86   1      }
				  87          uint gettemp()                          //获取温度
				  88          {
				  89   1              uchar a,b;
				  90   1              dsreset();                              //ds18b20初始化函数
				  91   1              delay50us(2000);        //延时100
				  92   1              tempwrite(0xcc);        
				  93   1              tempwrite(0xbe);
				  94   1              delay50us(2);    
				  95   1              a=tempread();           
				  96   1              b=tempread();            
				  97   1              temp=b;
				  98   1              temp				  99   1              temp=temp|a;
				 100   1              ftemp=temp*0.0625;      
				 101   1              temp=ftemp*10+0.5;   
				 102   1              ftemp=ftemp+0.05;    
				 103   1              return temp;                   
				 104   1      }  
				 105          /****************LCD检查忙位*******************/
				 106          void chkbusy()
				 107          {
				 108   1          rs=0;
				 109   1          rw=1;
				 110   1          e=1;
				 111   1          P0=0xff;
				 112   1          while((P0&0x80)==0x80);
				 113   1          e=0;
				 114   1      }
				 115          /*****************读LCD数据******************/
				 116          uchar read()
				 117          {
				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 3   
				
				 118   1              uchar shuju;
				 119   1              chkbusy();
				 120   1              P0=0xff;
				 121   1              rs=1;                         
				 122   1              rw=1;   
				 123   1              e=0; 
				 124   1              delay50us(1);                    
				 125   1              e=1;
				 126   1              shuju=P0;            
				 127   1              e=0;
				 128   1              return(shuju);   
				 129   1      }
				 130          /***********写命令**********/
				 131          void writecom(uchar com)
				 132          { 
				 133   1              chkbusy();
				 134   1              rw=0;
				 135   1              rs=0;
				 136   1              delay50us(1);
				 137   1              P0=com;
				 138   1              e=1;
				 139   1              delay50us(10);
				 140   1              e=0;
				 141   1              delay50us(10);
				 142   1      }
				 143          /*************写数据****************/
				 144          void writedat(uchar dat)
				 145          {  
				 146   1              chkbusy();
				 147   1              rw=0;
				 148   1              rs=1;
				 149   1              delay50us(1);
				 150   1              P0=dat;
				 151   1              e=1;
				 152   1              delay50us(10);
				 153   1              e=0;
				 154   1              delay50us(10);
				 155   1      }
				 156          /****************初始化12864*****************/
				 157          void initinal(void)
				 158          {
				 159   1              delay50us(1000);
				 160   1              writecom(0x30);
				 161   1              delay50us(4);
				 162   1              writecom(0x30);
				 163   1              delay50us(2);
				 164   1              writecom(0x0e);
				 165   1              delay50us(4);
				 166   1              writecom(0x01);
				 167   1              delay50us(400);
				 168   1              writecom(0x06);
				 169   1              delay50us(10);
				 170   1      }
				 171          /*************画点程序**************/
				 172          void DrawPoint(uchar X,uchar Y,uchar Color)
				 173          {
				 174   1          uchar Row,Tier,Tier_bit ;
				 175   1          uchar ReadOldH,ReadOldL ;
				 176   1          writecom(0x34);       //扩充指令操作
				 177   1          writecom(0x36);
				 178   1          Tier=X>>4;
				 179   1          Tier_bit=X&0x0f;
				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 4   
				
				 180   1          if(Y				 181   1                  {
				 182   2                      Row=Y;
				 183   2                  }
				 184   1          else 
				 185   1                  {
				 186   2                      Row=Y-32;
				 187   2                      Tier+=8;
				 188   2                  }
				 189   1          writecom(Row+0x80);
				 190   1          writecom(Tier+0x80);
				 191   1          read();
				 192   1          ReadOldH=read();
				 193   1          ReadOldL=read();
				 194   1          writecom(Row+0x80);
				 195   1          writecom(Tier+0x80);
				 196   1          if(Tier_bit				 197   1                  {
				 198   2                      switch(Color)
				 199   2                      {
				 200   3                          case 0 :ReadOldH&=(~(0x01				 201   3                          case 1 :ReadOldH|=(0x01				 202   3                          case 2 :ReadOldH^=(0x01				 203   3                          default : break;
				 204   3                      }
				 205   2                      writedat(ReadOldH);
				 206   2                      writedat(ReadOldL);
				 207   2                  }
				 208   1          else 
				 209   1                  {
				 210   2                      switch(Color)
				 211   2                      {
				 212   3                          case 0 :ReadOldL&=(~(0x01				 213   3                          case 1 :ReadOldL|=(0x01				 214   3                          case 2 :ReadOldL^=(0x01				 215   3                          default : break;
				 216   3                      }
				 217   2                      writedat(ReadOldH);
				 218   2                      writedat(ReadOldL);
				 219   2                  }
				 220   1          writecom(0x30);             //基本指令操作
				 221   1      }
				 222          /***************清屏函数*****************/
				 223          void clrscreen()
				 224          {
				 225   1              writecom(0x01);
				 226   1              delay50us(10);
				 227   1      }
				 228          /************清整个GDRAM空间***************/
				 229          void clrgdram()
				 230          {
				 231   1          unsigned char x,y ;
				 232   1          for(y=0;y				 233   1              for(x=0;x				 234   1                      {
				 235   2                              writecom(0x34);
				 236   2                              writecom(y+0x80);   //行地址
				 237   2                              writecom(x+0x80);   //列地址
				 238   2                              writecom(0x30);         //基本指令操作
				 239   2                              writedat(0x00);
				 240   2                              writedat(0x00);
				 241   2                      }                               
				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 5   
				
				 242   1      }
				 243          /*************主函数********************/
				 244          void main()
				 245          {  
				 246   1          uchar i=0,j,e,colour=1;
				 247   1              uint a;
				 248   1              rw=0;              //写
				 249   1          initinal();        //初始化12864
				 250   1          clrgdram();        //清整个GDRAM空间
				 251   1          delay50us(2);
				 252   1              //writecom(0x34);          //扩充指令操作
				 253   1              writecom(0x0c);
				 254   1              while(1)
				 255   1                      {
				 256   2                              uint a,b,c,d;
				 257   2                      DrawPoint(17,6,colour);
				 258   2                      DrawPoint(17,22,colour);
				 259   2                      DrawPoint(17,38,colour);
				 260   2                      DrawPoint(17,54,colour);
				 261   2                              for(j=16;j				 262   2                              {     
				 263   3                                      DrawPoint(16,j-16,colour);
				 264   3                                              DrawPoint(j,63,colour);
				 265   3                                      writecom(0x80);
				 266   3                                      writedat(table[6]);
				 267   3                                      writedat(table[7]);
				 268   3                                              writecom(0x90);
				 269   3                                      writedat(table[5]);
				 270   3                                              writedat(table[1]);
				 271   3                                              writecom(0x88);
				 272   3                                      writedat(table[3]);
				 273   3                                              writedat(table[5]);
				 274   3                                              writecom(0x98);
				 275   3                                      writedat(table[1]);
				 276   3                                              writedat(table[9]);
				 277   3                              }
				 278   2                         for(i=16;i				 279   2                      {     
				 280   3                              tempwendu();   //转换温度
				 281   3                              a=gettemp();   //获取温度
				 282   3                              e=73-a/10; 
				 283   3                              DrawPoint(i,e,colour);
				 284   3                              delay50us(10000);
				 285   3                              b=a/100;
				 286   3                                      c=a%100/10;
				 287   3                                      d=a%100%10;
				 288   3                                      writecom(0x82);         //写地址
				 289   3                                      writedat(table[12]);
				 290   3                                      delay50us(1);
				 291   3                                      writedat(table[13]);
				 292   3                                      delay50us(1);
				 293   3                                      writecom(0x83);         //写地址
				 294   3                                      writedat(table[b]);
				 295   3                                      delay50us(1);
				 296   3                                      writedat(table[c]);
				 297   3                                      delay50us(1);
				 298   3                                      writedat(table[10]);
				 299   3                                      delay50us(1);
				 300   3                                      writedat(table[d]);
				 301   3                                      delay50us(1);
				 302   3                                      writedat(table[11]);
				 303   3                                      delay50us(4); 
				C51 COMPILER V9.01   W                                                                     06/23/2011 09:59:30 PAGE 6   
				
				 304   3                      } 
				 305   2                    clrgdram();                               //清整个GDRAM空间
				 306   2                      }
				 307   1      
				 308   1      }
				*** WARNING C280 IN LINE 247 OF W.C: 'a': unreferenced local variable
				
				
				MODULE INFORMATION:   STATIC OVERLAYABLE
				   CODE SIZE        =   1191    ----
				   CONSTANT SIZE    =     15    ----
				   XDATA SIZE       =   ----    ----
				   PDATA SIZE       =   ----    ----
				   DATA SIZE        =      6      17
				   IDATA SIZE       =   ----    ----
				   BIT SIZE         =   ----       2
				END OF MODULE INFORMATION.
				
				
				C51 COMPILATION COMPLETE.  1 WARNING(S),  0 ERROR(S)
							

相关资源