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

📄 ds18b20.c

📁 采用51单片机控制
💻 C
字号:
//ds18b20 drive program
//for 51 mcu with lcd1602 display
//designed by zhaoliang
//2008-6-15 20:11
#include "reg51.h"
#include <intrins.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     P0
sbit LCD1602_RS=P1^2;   //data command select  1 data  0 command  pin 4 
sbit LCD1602_RW=P1^1;   //read write select   1 read   0 write     pin 5
sbit LCD1602_EN=P1^0;   //LCD enable signal             pin 6


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 char ResultTemperatureH;
unsigned char ResultTemperatureLH,ResultTemperatureLL;
data unsigned char ResultSignal;
  //The signal of temperature;
/********************************************************************/
void main(void)
{ 
 unsigned char i,j;
 Initize_One_Wire_Bus(); 
 LCD_init(); 
 while(TRUE )    
 {
  Read_18B20();
  i=ResultTemperatureH/10;
  j=ResultTemperatureH-(i*10);
  LCD_write_string(0,LINE1,"  DS1B20 TEST  ");
  LCD_write_string(0,LINE2,"XiaorunyiT:   .");
  LCD_write_char(0x0c,LINE2,i|0x30);
  LCD_write_char(0x0d,LINE2,j|0x30);
  LCD_write_char(0x0f,LINE2,(ResultTemperatureLH/10)|0x30);
 }
}
/********************************************************************/
/******************** 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;
/************************************************************************/

⌨️ 快捷键说明

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