
#include <reg51.h>
#include <intrins.h>
#include <math.h>
#define uint unsigned int
#define uchar unsigned char	

sbit rs=P3^5;      /*数据\指令 选择*/ 
sbit rw=P3^6;      /*读\写 选择*/ 
sbit e=P3^4;      /*读\写使能*/ 
sbit ds=P2^2;	  //18B20信号脚
sbit PSB = P3^7;
sbit WE = P2^6;
sbit DU = P2^7;

uint temp;
float ftemp;
uchar code table[]="0123456789.度";

void writecom(uchar com);
void writedat(uchar dat);
void initinal(void);

//12MHZ晶振 延时50微秒
void delay50us(uint t)   
{
   uchar j;
for(;t>0;t--)
   for(j=19;j>0;j--);
}
void dsreset()	  // ds18b20初始化函数
{
     uint i;
	 ds=0;         //拉低总线开始复位
	 i=103;
	 while(i>0)i--;
	 ds=1;//释放总线
	 i=4;
	 while(i>0)i--;
   // 省去了等待芯片返回应答的过程 。。。。。。
}
bit readbit(void)	  //ds18b20读取一个二进制位，参照时序图
{
  uint i;
  bit dat;
  ds=0;i++;
  ds=1;i++,i++;
  dat=ds;
  i=8;while(i>0)i--;
  return(dat);
}
uchar tempread(void)  //ds18b20读取一个字节的数据
{
  uchar i,j,dat;
  dat=0;
  for(i=0;i<8;i++)
  {
	 j=readbit();
	 dat=(j<<7)|(dat>>1);  //
  }
  return(dat);
}
void tempwrite(uchar dat)	//ds18b20写入字节数据
{
 uint i;uchar j;
 bit testb;
 for(j=1;j<=8;j++)
 {
   testb=dat&0x01;
   dat=dat>>1;
   if(testb==1)	   //	写1
   {
     ds=0;		     //	 拉低总线以开始一个写时序
	 i++;i++;
	 ds=1;			 //	 释放总线
	 i=8;while(i>0)i--;
   }
   else					  //   写0
   {
   	 ds=0;
	 i=8;while(i>0)i--;
	 ds=1;
	 i++;i++;
   }
 }
}
void tempchang(void)	//温度转换
{
 dsreset();
 delay50us(20); 
 tempwrite(0xcc);	  //
 tempwrite(0x44);	 //
}
uint gettemp()	 //	获取温度
{
  uchar a,b;
  dsreset();
  delay50us(2000); 	   //延时100毫秒消除恐怖85度
  tempwrite(0xcc);	  //
  tempwrite(0xbe);	 //
  a=tempread();		 //
  b=tempread();		   //
  temp=b;
  temp<<=8;       //////
  temp=temp|a;
  ftemp=temp*0.0625;      //
  temp=ftemp*10+0.5;		 //
  ftemp=ftemp+0.05;			 //
  return temp;                //   
}		

 /*------------------检查忙位-----------------------------*/
void chkbusy()
{
    rs=0;
    rw=1;
    e=1;
    P0=0xff ;
    while((P0&0x80)==0x80);
    e=0;
}
/*************读数据************/
uchar read()
{
uchar shuju;
chkbusy();
P0=0xff;
rs=1;                         
rw=1;   
e=0; 
delay50us(1);                    
e=1;
shuju=P0; 
// delay(5);               
e=0;
return(shuju);   
}
//增加画点子程序
void DrawPoint(uchar X,uchar Y,uchar Color)
{
    uchar Row,Tier,Tier_bit ;
    uchar ReadOldH,ReadOldL ;
    //writecom(0x34);	//扩展指令开
    writecom(0x36);		//扩展指令加绘图模块
    Tier=X>>4 ;
    Tier_bit=X&0x0f ;
    if(Y<32)
    {
        Row=Y ;
    }
    else 
    {
        Row=Y-32 ;
        Tier+=8 ;
    }
    writecom(Row+0x80);
    writecom(Tier+0x80);
    read();
    ReadOldH=read();
    ReadOldL=read();
    writecom(Row+0x80);
    writecom(Tier+0x80);
    if(Tier_bit<8)
    {
        switch(Color)
        {
            case 0 :
            ReadOldH&=(~(0x01<<(7-Tier_bit)));
            break ;
            case 1 :
            ReadOldH|=(0x01<<(7-Tier_bit));
            break ;
            case 2 :
            ReadOldH^=(0x01<<(7-Tier_bit));
            break ;
            default :
            break ;
        }
        writedat(ReadOldH);
        writedat(ReadOldL);
    }
    else 
    {
        switch(Color)
        {
            case 0 :
            ReadOldL&=(~(0x01<<(15-Tier_bit)));
            break ;
            case 1 :
            ReadOldL|=(0x01<<(15-Tier_bit));
            break ;
            case 2 :
            ReadOldL^=(0x01<<(15-Tier_bit));
            break ;
            default :
            break ;
        }
        writedat(ReadOldH);
        writedat(ReadOldL);
    }
    writecom(0x30);
}
 //写命令
void writecom(uchar com)
{ 
  chkbusy();
  rw=0;
  rs=0;
  delay50us(1);
  P0=com;
  e=1;
  delay50us(10);
  e=0;
  delay50us(10);
}

//写数据
void writedat(uchar dat)
{  
  chkbusy();
  rw=0;
  rs=1;
  delay50us(1);
  P0=dat;
  e=1;
  delay50us(10);
  e=0;
  delay50us(10);
}

//初始化
void initinal(void)
{
  delay50us(2);
  writecom(0x30);
  delay50us(4);
  writecom(0x30);
  delay50us(4);
  writecom(0x0e);
  delay50us(4);
  writecom(0x01);
  delay50us(240);
  writecom(0x06);
  delay50us(10);
}
 void clrscreen()
{
    writecom(0x01);
    delay50us(10);
}
//------------------清整个GDRAM空间----------------------------
void clrgdram()
{
    unsigned char x,y ;
    for(y=0;y<64;y++)
    for(x=0;x<16;x++)
    {
        writecom(0x34);
        writecom(y+0x80);
        //行地址
        writecom(x+0x80);
        //列地址
        writecom(0x30);
        writedat(0x00);
        writedat(0x00);
    }
}
//------------------------------------------------------------
  //主函数
void main()
{	 
    uchar i,j,e,colour=1;
//	uint a ;
  	rw=0;
    WE = DU = 0;
	initinal();
    clrgdram();
  //  delay50us(2);
	clrscreen();
//	writecom(0x34);
	
	 	
    while(1)
	{
	  uint a,b,c,d;
	   DrawPoint(18,6,colour);
	   DrawPoint(18,22,colour);
	   DrawPoint(18,38,colour);
	   DrawPoint(18,54,colour);
		for(j=17;j<128;j++)
    {   	 
	   
		DrawPoint(17,j-17,colour);
		DrawPoint(j,63,colour);
	    writecom(0x80);
	    writedat(table[6]);
	    writedat(table[7]);
		writecom(0x90);
	    writedat(table[5]);
		writedat(table[1]);
		writecom(0x88);
	    writedat(table[3]);
		writedat(table[5]);
		writecom(0x98);
	    writedat(table[1]);
		writedat(table[9]);	
    }

	for(i=17;i<128;i++)
    {   	 
	   tempchang();
       a=gettemp();
	   delay50us(1000);
	   e=73-a/10;	
       DrawPoint(i,e,colour);
	  
	     b=a/100;
	  c=a%100/10;
	  d=a%100%10;
	  writecom(0x83);
	  writedat(table[b]);
	  delay50us(1);
	  writedat(table[c]);
	  delay50us(1);
	  writedat(table[10]);
	  delay50us(1);
	  writedat(table[d]);
	  delay50us(1);
	  writedat(table[11]);
	  writedat(table[12]);
	  delay50us(4);
	  writecom(0x83);	      
    } 
    clrgdram();
/*	writecom(0x36);
    delay50us(5);
	clrgdram();
	clrscreen();	  
      tempchang();
	  a=gettemp();
	  				  */
	}

}