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

📄 ds18b20+lcd1602.c

📁 温度的动态显示
💻 C
字号:
//ds18b20 drive program
//for 51 mcu with lcd1602 display
//designed by zhaoliang
//2005-6-15 20:11
#include "reg51.h"
#include <intrins.h>
#include <stdio.h>
#include "stdlib.h"
/*******************************************************************/
//lcd part
#define  LINE1     0
#define  LINE2     1
#define  LINE1_HEAD    0x80
#define  LINE2_HEAD    0xC0
#define  LCD_DELAY_TIME   40
#define  DATA_MODE    0x38
#define  OPEN_SCREEN    0x0C
#define  DISPLAY_ADDRESS   0x80
#define  CLEARSCREEN    LCD_en_command(0x01)
#define  COMMAND_SLOT   LCD1602_RS=LOW; LCD1602_RW=LOW;LCD1602_EN=LOW
#define  DATA_SLOT    LCD1602_RS=HIGH;LCD1602_RW=LOW;LCD1602_EN=LOW
//common part 
#define  HIGH     1
#define  LOW      0
#define  TRUE      1
#define  ZERO      0 
#define  MSB       0x80
//ds18b20 part
#define  SkipRom     0xcc
#define  ConvertTemperature   0x44
#define  ReadScratchpad    0xbe

/*******************************************************************/
//change this part at different board
#define  LCDIO     P2
sbit LCD1602_RS=P0^7;   //data command select  1 data  0 command  pin 4 
sbit LCD1602_RW=P0^6;   //read write select   1 read   0 write     pin 5
sbit LCD1602_EN=P0^5;   //LCD enable signal             pin 6
sbit buzzer=P0^4;

sbit One_Wire_Bus=P3^3;

//function define
/********************************************************************/
void LCD_delay(void);//lcd delay function 
void LCD_en_command(unsigned char command);//write command function
void LCD_en_dat(unsigned char temp);//write data function
void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
void LCD_init(void);//lcd initize function

void One_Wire_Delay(unsigned char delay_time);
void One_Wire_Write_Byte(unsigned char oww_dat);
unsigned char One_Wire_Read_Byte(void);
void Read_18B20(void);
void Initize_One_Wire_Bus(void);
/********************************************************************/
data unsigned char GetScratchpad[2];
code unsigned char decimalH[16]={00,06,12,18,25,31,37,43,50,56,62,68,75,81,87,93};
code unsigned char decimalL[16]={00,25,50,75,00,25,50,75,00,25,50,75,00,25,50,75};
unsigned int ResultTemperatureH;
unsigned int ResultTemperatureLH,ResultTemperatureLL;
data unsigned char ResultSignal;
  //The signal of temperature;
/********************************************************************/

/********************************************************************/
/******************** LCD PART *************************************/
void LCD_delay(void)   
{
 unsigned char i;
 for(i=LCD_DELAY_TIME;i>ZERO;i--);//be sure lcd reset
}
/********************************************************************/  
void LCD_en_command(unsigned char command)
{
 LCDIO=command;
 COMMAND_SLOT;
 LCD_delay();
 LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
 LCDIO=dat;
 DATA_SLOT;
 LCD_delay();
 LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
 unsigned char address;
 if (y == LINE1) 
  address = LINE1_HEAD + x;
 else 
     address = LINE2_HEAD + x;
 LCD_en_command(address); 
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
 LCD_set_xy( x, y ); 
 LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y ); //set address 
    while (*s)  // write character
    {
     LCDIO=*s;
        LCD_en_dat(*s);   
  s ++;
    }
}
/********************************************************************/
void LCD_init(void)
{ 
 CLEARSCREEN;//clear screen 
 LCD_en_command(DATA_MODE);//set 8 bit data transmission mode 
 LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
 LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address 
 CLEARSCREEN;//clear screen
}
/********************************************************************/
void Initize_One_Wire_Bus(void)
{ 
 One_Wire_Bus=0;
 One_Wire_Delay(80);//Bus master pulling low 488us 
 One_Wire_Bus=1;
 One_Wire_Delay(25);//Resister pull up 158us;
}//Intialization the 1-wire devices;
/***********************************************************************/
/******************* ds 18b20 **********************************/
void One_Wire_Delay(unsigned char delay_time)
{ 
 while(delay_time)delay_time--;//Delay time us :=(8+delay_time*6)us;
}
/***********************************************************************/
unsigned char One_Wire_Read_Byte(void)
{ 
 bit temp_bit;
 unsigned char i,result=0;
 for(i=0;i<8;i++)
 { 
  One_Wire_Bus=0;
  One_Wire_Bus=1; 
  temp_bit=One_Wire_Bus;
  One_Wire_Delay(9);//delay 62 us
  if(temp_bit)
   result|=0x01<<i;
 }
 return(result);
  //return the result of the 1-wire device;
}//Read a byte from the 1-wire bus;
/***********************************************************************/
void One_Wire_Write_Byte(unsigned char oww_dat)
{  
 unsigned char i;
 for(i=0;i<8;i++)
 { 
  One_Wire_Bus=0;
  if(oww_dat&0x01)One_Wire_Bus=1; 
  One_Wire_Delay(20);//delay 128 us
  One_Wire_Bus=1;
  oww_dat>>=1;
 }
 One_Wire_Delay(10);
}//Write a byte to the 1-wire bus;
/***********************************************************************/
void Read_18B20(void)
{ 
 unsigned char tempH,tempL;
 Initize_One_Wire_Bus();
 One_Wire_Write_Byte(SkipRom);
 _nop_();
  //There is just one DS1820 on the bus;
 One_Wire_Write_Byte(ConvertTemperature);
 One_Wire_Delay(5);
  //Start to convert temperature;
 Initize_One_Wire_Bus();
 One_Wire_Write_Byte(SkipRom);
 _nop_();
 One_Wire_Write_Byte(ReadScratchpad);
 GetScratchpad[0]=One_Wire_Read_Byte();
  //Master samples the LSB temperature from the scratchpad;
 GetScratchpad[1]=One_Wire_Read_Byte();
  //Master samples the MSB temperature from the scratchpad;
 One_Wire_Delay(120);
 tempH=(GetScratchpad[1]<<4)|(GetScratchpad[0]>>4);  
 tempL=(GetScratchpad[0]&0x0f);
 Initize_One_Wire_Bus();
  //Issue a reset to terminate left parts;
 if(tempH&0x80)
 { 
  tempH=~tempH;
  tempL=~tempL+1;
  ResultSignal=1;
  //Negative temperature;
 }
 ResultTemperatureH=tempH;
 ResultTemperatureLL=decimalL[tempL];
 ResultTemperatureLH=decimalH[tempL];
  //Result of temperature; 
}//Read the byte0 and byte1 from scratchpad;
/************************************************************************/
void main(void)
{ 
 unsigned int i,j,k;
 unsigned int delay;

  SCON=0x50; //串口方式1,允许接收
 TMOD=0x20;  //定时器1定时方式2
 TCON=0x40;  //设定时器1开始计数
 TH1=0xe6;   //12MHz 1200波特率
 TL1=0xe6;
 TI=1;  //发送中断标志 置1
 TR1=1;      //启动定时器 T1
 Initize_One_Wire_Bus(); 
 LCD_init(); 
Read_18B20();
for(k=1;k>0;k--)
            for(delay=0;delay<60000;delay++);//延时,开始18b20初始化,显示85,然后再调变到实际温度
 while(TRUE )    
 {
  Read_18B20();
  i=ResultTemperatureH/10;
  j=ResultTemperatureH-(i*10);
 LCD_write_string(0,LINE1,"   MY  DS18b20");
 LCD_write_string(0,LINE2,"BY LHZ T is:  .");
LCD_write_char(0x0c,LINE2,i|0x30);
LCD_write_char(0x0d,LINE2,j|0x30);
LCD_write_char(0x0f,LINE2,(ResultTemperatureLH/10)|0x30);
  

 printf ("\nDS18B20 test Program on PC \n"); //向PC端发送数据			 
 printf ("You can use PC to display information \n"); //向PC端发送数据	
 	printf("Now the Temperature is : ");	 

  printf ("%u",ResultTemperatureH); //向PC端发送数据
  printf (".%u%u \n ",ResultTemperatureLH,ResultTemperatureLL); //向PC端发送数据
		if(ResultTemperatureH>29)//30度蜂鸣器响
			buzzer=0;
			else
			buzzer=1;
}
}

⌨️ 快捷键说明

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