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

📄 ds18b20(lcd).c

📁 单片机型号为STC89C52
💻 C
字号:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;           //define interface
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
sbit dscs=P1^4;
sbit dsas=P1^5;
sbit dsrw=P1^6;
sbit dsds=P1^7;
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit ss=P3^6;
sbit rd=P3^7;
uchar code tab[] ={ 0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00,	//℃,代码 0x00
				    0x00,0x00,0x00,0x00,0x0C,0x0C,0x00,0x00,};  //.
uint temp,bai,shi,ge;             // 温度值的变量
uchar flag1;           // 结果为负和正的标志位
void readrom();
void tmp();
void write_date(uchar) ;
void write_com(uchar );
void tmpwritebyte(uchar );

void delay(uint count)      //delay
{
  uint i;
  while(count)
  {
    i=20;
    while(i>0)
    i--;
    count--;
  }
}
void dsreset(void)       //发送复位和初始化
{
  uint i;
  DS=0;
  i=103;
  while(i>0)i--;  //延时
  DS=1;
  i=4;
  while(i>0)i--;
}

bit tmpreadbit(void)       //读取数据的一位
{
   uint i;
   bit dat;
   DS=0;i++;          //i++ for delay
   DS=1;i++;i++;
   dat=DS;
   i=8;while(i>0)i--;
   return (dat);
}

uchar tmpread(void)   //读一个字节
{
  uchar i,j,dat;
  dat=0;
  for(i=1;i<=8;i++)
  {
    j=tmpreadbit();
    dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
  }
  return(dat);
}

void tmpwritebyte(uchar dat)   //write a byte to ds18b20
{
	uint i;
	uchar j;
	bit testb;
	for(j=1;j<=8;j++)
	{
	    testb=dat&0x01;
	    dat=dat>>1;
	    if(testb)     //write 1
	    {
			DS=0;
			i++;i++;
			DS=1;
			i=8;while(i>0)i--;
		}
	    else
	    {
			DS=0;       //write 0
			i=8;while(i>0)i--;
			DS=1;
			i++;i++;
	    }
	}
}

void tmpchange(void)  //DS18B20 begin change
{
	dsreset();
	delay(1);
	tmpwritebyte(0xcc);  // address all drivers on bus
	tmpwritebyte(0x44);  //  initiates a single temperature conversion
}

void tmp()               //读取温度
{
  	float tt;
	uchar a,b;
	dsreset(); //复位
	delay(1);
 	tmpwritebyte(0xcc);	  //跳过序列号命令
	tmpwritebyte(0xbe);	  //发转换命令
	a=tmpread();  //读取低位温度
	b=tmpread();  //读取高位温度
 	temp=b;
 	temp<<=8;             //two byte  compose a int variable
 	temp=temp|a;
 	tt=temp*0.0625;
  	temp=tt*10+0.5;
//  	return temp;
}

/************************
1602
**************************/
void write_com(uchar com) //写命令 
{
    lcdrs=0;
    lcden=0;
    P0=com;
    delay(5);
    lcden=1;
    delay(5);
    lcden=0;	
}

void write_date(uchar dat) //写数据 
{
    lcdrs=1;
    lcden=0;
    P0=dat;
    delay(5);
    lcden=1;
    delay(5);
    lcden=0;	
}

void init()  //初始化 
{
    EA=1;
    EX0=1;//开外部0中断 
    IT0=1;//外部中断0的触发方式为边沿触发方式( 下降沿有效) 

    dula=0;
    wela=0;
    lcden=0;
    write_com(0x38);
    write_com(0x0c);
    write_com(0x06);
    write_com(0x01);
    write_com(0x80);
    write_com(0x80+0x40);
}

void write_tmp(uchar add2, uchar date)  //写温度
{
    uchar bai,shi,ser;
	ser=temp/10;
   	SBUF=ser;
	bai=temp/100;
    shi=temp%100/10;
    write_com(0x80+0x40+add2);
	write_date(0x30+bai);
    write_date(0x30+shi);
}
void write_tmp1(uchar add3, uchar date)  //写温度小数点后一位
{
    uchar ge,ser;
	ser=temp/10;
   	SBUF=ser;
    ge=temp%10;
    write_com(0x80+0x40+add3);
    write_date(0x30+ge);
}
//写地址函数
void LCD_set_rc( uchar r, uchar c )
{
    uchar address;    
    if (r == 1)
    address = 0x80 + c;
    else if(r == 2)
    address = 0xc0 + c;    
    write_com( address );
}


//写一个字符
void LCD_write_char(uchar r, uchar c, uchar date)
{
  LCD_set_rc( r, c );                 //写地址
  write_date( date );
}

//向CGRAM写入字模数据
void write_CGRAM( uchar TAB[], uchar n )     //定义输入CGRAM的字模代码,字模的个数
{
    uchar tmp;
    uchar i;
    uchar j;
    uchar k;
    tmp = 0x40;                       //设置CGRAM地址的格式字
    k = 0;
    for( j = 0; j < n; j++)
    {
        for(i = 0; i < 8; i++)
        {
            write_com(tmp + i); // 设置自定义字符的 CGRAM 地址
            write_date(TAB[k]);   // 向CGRAM写入自定义字符表的数据
            k++;
         }
         tmp = tmp + 0x08;
     }
}
void main()
{
	init();
	do
	{	
		write_CGRAM(tab, 2);
		tmp();
		tmpchange();
	 	write_tmp(10,bai); 
		LCD_write_char(2, 12, 1); 
   		write_tmp1(13,ge);  		
  		LCD_write_char(2, 14, 0); //显示℃
		
	
	}
	while(1);
}

⌨️ 快捷键说明

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