标题: 三路温度采集,lcd1602显示 [打印本页]

作者: shishi    时间: 2015-6-28 11:19
标题: 三路温度采集,lcd1602显示

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

//--定义使用的IO口--//
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P1^2;
sbit LCD1602_RW=P1^1;
sbit LCD1602_RS=P1^0;
sbit DSPORT=P3^2;
sbit DSPORT_1=P3^3;
sbit DSPORT_2=P3^4;
//--声明全局函数--//
void Delay1ms(uint );
uchar Ds18b20Init();
void Ds18b20WriteByte(uchar com);
uchar Ds18b20ReadByte();
void  Ds18b20ChangTemp();
void  Ds18b20ReadTempCom();
int Ds18b20ReadTemp();
uchar Ds18b20_1Init();
void Ds18b20_1WriteByte(uchar com);
uchar Ds18b20_1ReadByte();
void  Ds18b20_1ChangTemp();
void  Ds18b20_1ReadTempCom();
int Ds18b20_1ReadTemp();
/*在51单片机12MHZ时钟下的延时函数*/
void Lcd1602_Delay1ms(uint c);   //误差 0us
/*LCD1602写入8位命令子函数*/
void LcdWriteCom(uchar com);
/*LCD1602写入8位数据子函数*/
void LcdWriteData(uchar dat) ;
/*LCD1602初始化子程序*/  
void LcdInit();
void LcdDisplay(int temp);
void LcdDisplay_1(int temp1);
void LcdDisplay_2(int temp2);
void Lcd1602_Delay1ms(uint c)   //误差 0us
{
    uchar a,b;
for (; c>0; c--)
{
   for (b=199;b>0;b--)
   {
     for(a=1;a>0;a--);
   }      
}
     
}
/*******************************************************************************
* 函 数 名         : LcdWriteCom
* 函数功能     : 向LCD写入一个字节的命令
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
  
/*void LcdWriteCom(uchar com)   //写入命令
{
LCD1602_E = 0;     //使能
LCD1602_RS = 0;    //选择发送命令
LCD1602_RW = 0;    //选择写入

LCD1602_DATAPINS = com;     //放入命令
Lcd1602_Delay1ms(1);  //等待数据稳定
LCD1602_E = 1;           //写入时序
Lcd1602_Delay1ms(5);   //保持时间
LCD1602_E = 0;
}
   */
void LcdWriteCom(uchar com)   //写入命令
{
LCD1602_E = 0;  //使能清零
LCD1602_RS = 0;  //选择写入命令
LCD1602_RW = 0;  //选择写入
LCD1602_DATAPINS = com; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1;  //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
// Lcd1602_Delay1ms(1);
LCD1602_DATAPINS = com << 4; //发送低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1;  //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
/*******************************************************************************
* 函 数 名         : LcdWriteData
* 函数功能     : 向LCD写入一个字节的数据
* 输    入         : dat
* 输    出         : 无
*******************************************************************************/     
      
/*void LcdWriteData(uchar dat)   //写入数据
{
LCD1602_E = 0; //使能清零
LCD1602_RS = 1; //选择输入数据
LCD1602_RW = 0; //选择写入
LCD1602_DATAPINS = dat; //写入数据
Lcd1602_Delay1ms(1);
LCD1602_E = 1;   //写入时序
Lcd1602_Delay1ms(5);   //保持时间
LCD1602_E = 0;
} */
void LcdWriteData(uchar dat)   //写入数据
{
LCD1602_E = 0;   //使能清零
LCD1602_RS = 1;   //选择写入数据
LCD1602_RW = 0;   //选择写入
LCD1602_DATAPINS = dat; //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
Lcd1602_Delay1ms(1);
LCD1602_E = 1;   //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
LCD1602_DATAPINS = dat << 4; //写入低四位
Lcd1602_Delay1ms(1);
LCD1602_E = 1;   //写入时序
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
/*******************************************************************************
* 函 数 名       : LcdInit()
* 函数功能   : 初始化LCD屏
* 输    入       : 无
* 输    出       : 无
*******************************************************************************/     
/*void LcdInit()        //LCD初始化子程序
{
  LcdWriteCom(0x38);  //开显示
LcdWriteCom(0x0c);  //开显示不显示光标
LcdWriteCom(0x06);  //写一个指针加1
LcdWriteCom(0x01);  //清屏
LcdWriteCom(0x80);  //设置数据指针起点
} */
void LcdInit()        //LCD初始化子程序
{
LcdWriteCom(0x32);  //将8位总线转为4位总线
LcdWriteCom(0x28);  //在四位线下的初始化
LcdWriteCom(0x0c);  //开显示不显示光标
LcdWriteCom(0x06);  //写一个指针加1
LcdWriteCom(0x01);  //清屏
LcdWriteCom(0x80);  //设置数据指针起点
}

void Delay1ms(uint y)
{
uint x;
for( ; y>0; y--)
{
  for(x=110; x>0; x--);
}
}
/*******************************************************************************
* 函 数 名         : Ds18b20Init
* 函数功能     : 初始化
* 输    入         : 无
* 输    出         : 初始化成功返回1,失败返回0
*******************************************************************************/
uchar Ds18b20Init()
{
uchar i;
DSPORT = 0;    //将总线拉低480us~960us
i = 70;
while(i--);//延时642us
DSPORT = 1;   //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i = 0;
while(DSPORT) //等待DS18B20拉低总线
{
  i++;
  if(i>5)//等待>5MS
  {
   return 0;//初始化失败
  }
  Delay1ms(1);
}
return 1;//初始化成功
}
/*******************************************************************************
* 函 数 名         : Ds18b20_1Init
* 函数功能     : 初始化
* 输    入         : 无
* 输    出         : 初始化成功返回1,失败返回0
*******************************************************************************/
uchar Ds18b20_1Init()
{
uchar i;
DSPORT_1 = 0;    //将总线拉低480us~960us
i = 70;
while(i--);//延时642us
DSPORT_1 = 1;   //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i = 0;
while(DSPORT_1) //等待DS18B20拉低总线
{
  i++;
  if(i>5)//等待>5MS
  {
   return 0;//初始化失败
  }
  Delay1ms(1);
}
return 1;//初始化成功
}
uchar Ds18b20_2Init()
{
uchar i;
DSPORT_2 = 0;    //将总线拉低480us~960us
i = 70;
while(i--);//延时642us
DSPORT_2 = 1;   //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
i = 0;
while(DSPORT_2) //等待DS18B20拉低总线
{
  i++;
  if(i>5)//等待>5MS
  {
   return 0;//初始化失败
  }
  Delay1ms(1);
}
return 1;//初始化成功
}
/*******************************************************************************
* 函 数 名         : Ds18b20WriteByte
* 函数功能     : 向18B20写入一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
  DSPORT = 0;         //每写入一位数据之前先把总线拉低1us
  i++;
  DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
  i=6;
  while(i--); //延时68us,持续时间最少60us
  DSPORT = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  dat >>= 1;
}
}
  
/*******************************************************************************
* 函 数 名         : Ds18b20WriteByte
* 函数功能     : 向18B20_1写入一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void Ds18b20_1WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
  DSPORT_1 = 0;         //每写入一位数据之前先把总线拉低1us
  i++;
  DSPORT_1 = dat & 0x01;  //然后写入一个数据,从最低位开始
  i=6;
  while(i--); //延时68us,持续时间最少60us
  DSPORT_1 = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  dat >>= 1;
}
}
void Ds18b20_2WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
  DSPORT_2 = 0;         //每写入一位数据之前先把总线拉低1us
  i++;
  DSPORT_2 = dat & 0x01;  //然后写入一个数据,从最低位开始
  i=6;
  while(i--); //延时68us,持续时间最少60us
  DSPORT_2 = 1; //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  dat >>= 1;
}
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadByte
* 函数功能     : 读取一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
  DSPORT = 0;//先将总线拉低1us
  i++;
  DSPORT = 1;//然后释放总线
  i++;
  i++;//延时6us等待数据稳定
  bi = DSPORT;  //读取数据,从最低位开始读取
                                               //将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。
  byte = (byte >> 1) | (bi << 7);        
  i = 4;  //读取完之后等待48us再接着读取下一个数
  while(i--);
}   
return byte;
}  
/*******************************************************************************
* 函 数 名         : Ds18b20_1ReadByte
* 函数功能     : 读取一个字节
* 输    入         : com
* 输    出         : 无
*******************************************************************************/

uchar Ds18b20_1ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
  DSPORT_1 = 0;//先将总线拉低1us
  i++;
  DSPORT_1 = 1;//然后释放总线
  i++;
  i++;//延时6us等待数据稳定
  bi = DSPORT_1;  //读取数据,从最低位开始读取
  /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
  byte = (byte >> 1) | (bi << 7);        
  i = 4;  //读取完之后等待48us再接着读取下一个数
  while(i--);
}   
return byte;
}
uchar Ds18b20_2ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
  DSPORT_2 = 0;//先将总线拉低1us
  i++;
  DSPORT_2 = 1;//然后释放总线
  i++;
  i++;//延时6us等待数据稳定
  bi = DSPORT_2;  //读取数据,从最低位开始读取
  /*将byte左移一位,然后与上右移7位后的bi,注意移动之后移掉那位补0。*/
  byte = (byte >> 1) | (bi << 7);        
  i = 4;  //读取完之后等待48us再接着读取下一个数
  while(i--);
}   
return byte;
}
/*******************************************************************************
* 函 数 名         : Ds18b20ChangTemp
* 函数功能     : 让18b20开始转换温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void  Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);  //跳过ROM操作命令   
Ds18b20WriteByte(0x44);     //温度转换命令
// Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}
/*******************************************************************************
* 函 数 名         : Ds18b20_1ChangTemp
* 函数功能     : 让18b20开始转换温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void  Ds18b20_1ChangTemp()
{
Ds18b20_1Init();
Delay1ms(1);
Ds18b20_1WriteByte(0xcc);  //跳过ROM操作命令   
Ds18b20_1WriteByte(0x44);     //温度转换命令
// Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   
}
void  Ds18b20_2ChangTemp()
{
Ds18b20_2Init();
Delay1ms(1);
Ds18b20_2WriteByte(0xcc);  //跳过ROM操作命令   
Ds18b20_2WriteByte(0x44);     //温度转换命令
// Delay1ms(100); //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
   }
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTempCom
* 函数功能     : 发送读取温度命令
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void  Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);  //跳过ROM操作命令
Ds18b20WriteByte(0xbe);  //发送读取温度命令
}
/*******************************************************************************
* 函 数 名         : Ds18b20_1ReadTempCom
* 函数功能     : 发送读取温度命令
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void  Ds18b20_1ReadTempCom()
{
Ds18b20_1Init();
Delay1ms(1);
Ds18b20_1WriteByte(0xcc);  //跳过ROM操作命令
Ds18b20_1WriteByte(0xbe);  //发送读取温度命令
}
void  Ds18b20_2ReadTempCom()
{
Ds18b20_2Init();
Delay1ms(1);
Ds18b20_2WriteByte(0xcc);  //跳过ROM操作命令
Ds18b20_2WriteByte(0xbe);  //发送读取温度命令
}
/*******************************************************************************
* 函 数 名         : Ds18b20ReadTemp
* 函数功能     : 读取温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
int Ds18b20ReadTemp()
{
int temp = 0;
uchar tmh, tml;
Ds18b20ChangTemp();     //先写入转换命令
Ds18b20ReadTempCom();   //然后等待转换完后发送读取温度命令
tml = Ds18b20ReadByte();  //读取温度值共16位,先读低字节
tmh = Ds18b20ReadByte();  //再读高字节
temp = tmh;
temp <<= 8;
temp |= tml;
return temp;
}
  /*******************************************************************************
* 函 数 名         : Ds18b20_1ReadTemp
* 函数功能     : 读取温度
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
int Ds18b20_1ReadTemp()
{
int temp1 = 0;
uchar tmh, tml;
Ds18b20_1ChangTemp();     //先写入转换命令
Ds18b20_1ReadTempCom();   //然后等待转换完后发送读取温度命令
tml = Ds18b20_1ReadByte();  //读取温度值共16位,先读低字节
tmh = Ds18b20_1ReadByte();  //再读高字节
temp1 = tmh;
temp1 <<= 8;
temp1 |= tml;
return temp1;
}
int Ds18b20_2ReadTemp()
{
int temp2 = 0;
uchar tmh, tml;
Ds18b20_2ChangTemp();     //先写入转换命令
Ds18b20_2ReadTempCom();   //然后等待转换完后发送读取温度命令
tml = Ds18b20_2ReadByte();  //读取温度值共16位,先读低字节
tmh = Ds18b20_2ReadByte();  //再读高字节
temp2 = tmh;
temp2 <<= 8;
temp2 |= tml;
return temp2;
}
void main()
{   LcdInit(); //初始化LCD1602
   

while(1)
{   
   LcdDisplay(Ds18b20ReadTemp());
   LcdDisplay_1(Ds18b20_1ReadTemp());
   LcdDisplay_2(Ds18b20_2ReadTemp());
  
}

  
}
/*******************************************************************************
* 函数名         : LcdDisplay()
* 函数功能     : LCD显示读取到的温度
* 输入           : v
* 输出           : 无
*******************************************************************************/
void LcdDisplay(int temp)   //lcd显示
{
   
   unsigned char datas[] = {0, 0, 0, 0, 0}; //定义数组
float tp;  
if(temp< 0)    //当温度值为负数
   {  
    LcdWriteCom(0x80);  //写地址 80表示初始地址
     LcdWriteData('-');    //显示负
  //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
  temp=temp-1;
  temp=~temp;
  tp=temp;
  temp=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算由?.5,还是在小数点后面。

   }
  else
   {  
    LcdWriteCom(0x80);  //写地址 80表示初始地址
     LcdWriteData('+');   //显示正
  tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
  //如果温度是正的那么,那么正数的原码就是补码它本身
  temp=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算加上0.5,还是在小数点后面。
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
//datas[4] = temp % 10;

LcdWriteCom(0x82);    //写地址 80表示初始地址
LcdWriteData('0'+datas[0]); //百位

LcdWriteCom(0x83);   //写地址 80表示初始地址
LcdWriteData('0'+datas[1]); //十位
LcdWriteCom(0x84);  //写地址 80表示初始地址
LcdWriteData('0'+datas[2]); //个位
LcdWriteCom(0x85);  //写地址 80表示初始地址
LcdWriteData('.');   //显示 ‘.’
LcdWriteCom(0x86);   //写地址 80表示初始地址
LcdWriteData('0'+datas[3]); //显示小数点  
//LcdWriteCom(0x87);   //写地址 80表示初始地址
// LcdWriteData('0'+datas[4]); //显示小数点  
}
      
void LcdDisplay_1(int temp1)   //lcd显示
{
   
   unsigned char datas1[] = {0, 0, 0, 0, 0}; //定义数组
float tp;  
if(temp1< 0)    //当温度值为负数
   {
    LcdWriteCom(0xc0);  //写地址 80表示初始地址
     LcdWriteData('-');    //显示负
  //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
  temp1=temp1-1;
  temp1=~temp1;
  tp=temp1;
  temp1=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算由?.5,还是在小数点后面。

   }
  else
   {  
    LcdWriteCom(0xc0);  //写地址 80表示初始地址
     LcdWriteData('+');   //显示正
  tp=temp1;//因为数据处理有小数点所以将温度赋给一个浮点型变量
  //如果温度是正的那么,那么正数的原码就是补码它本身
  temp1=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算加上0.5,还是在小数点后面。
}
datas1[0] = temp1 / 10000;
datas1[1] = temp1 % 10000 / 1000;
datas1[2] = temp1 % 1000 / 100;
datas1[3] = temp1 % 100 / 10;
//datas1[4] = temp1 % 10;

LcdWriteCom(0xc2);    //写地址 80表示初始地址
LcdWriteData('0'+datas1[0]); //百位

LcdWriteCom(0xc3);   //写地址 80表示初始地址
LcdWriteData('0'+datas1[1]); //十位
LcdWriteCom(0xc4);  //写地址 80表示初始地址
LcdWriteData('0'+datas1[2]); //个位
LcdWriteCom(0xc5);  //写地址 80表示初始地址
LcdWriteData('.');   //显示 ‘.’
LcdWriteCom(0xc6);   //写地址 80表示初始地址
LcdWriteData('0'+datas1[3]); //显示小数位  
//LcdWriteCom(0xc7);   //写地址 80表示初始地址
//LcdWriteData('0'+datas1[4]); //显示小数为
}
  void LcdDisplay_2(int temp2)   //lcd显示
{
   
   unsigned char datas2[] = {0, 0, 0, 0, 0}; //定义数组
float tp;  
if(temp2< 0)    //当温度值为负数
   {  
    LcdWriteCom(0xc9);  //写地址 80表示初始地址
     LcdWriteData('-');    //显示负
  //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
  temp2=temp2-1;
  temp2=~temp2;
  tp=temp2;
  temp2=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算由?.5,还是在小数点后面。

   }
  else
   {   
    LcdWriteCom(0xc9);  //写地址 80表示初始地址
     LcdWriteData('+');   //显示正
  tp=temp2;//因为数据处理有小数点所以将温度赋给一个浮点型变量
  //如果温度是正的那么,那么正数的原码就是补码它本身
  temp2=tp*0.0625*100+0.5;
  //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  //算加上0.5,还是在小数点后面。
}
datas2[0] = temp2 / 10000;
datas2[1] = temp2 % 10000 / 1000;
datas2[2] = temp2 % 1000 / 100;
datas2[3] = temp2 % 100 / 10;
//datas2[4] = temp2 % 10;

LcdWriteCom(0xca);    //写地址 80表示初始地址
LcdWriteData('0'+datas2[0]); //百位

LcdWriteCom(0xcb);   //写地址 80表示初始地址
LcdWriteData('0'+datas2[1]); //十位
LcdWriteCom(0xcc);  //写地址 80表示初始地址
LcdWriteData('0'+datas2[2]); //个位
LcdWriteCom(0xcd);  //写地址 80表示初始地址
LcdWriteData('.');   //显示 ‘.’
LcdWriteCom(0xce);   //写地址 80表示初始地址
LcdWriteData('0'+datas2[3]); //显示小数位  
//LcdWriteCom(0xcf);   //写地址 80表示初始地址
//LcdWriteData('0'+datas2[4]); //显示小数为
}


作者: lingshuai2010    时间: 2018-7-12 15:34
楼主你选用的是什么单片机?
作者: 陶陶高中    时间: 2018-7-12 16:31
不错啊
作者: faridzled    时间: 2018-7-13 01:23
thanks for sharing




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1