DS18B20 Test Program

Publisher:雅致小筑Latest update time:2016-08-23 Source: eefocusKeywords:DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#include

#define uint unsigned int
#define uchar unsigned char

/************DS18B20**********/

sbit DQ=P3^7;

uchar temph=0,templ=0;
uint t=0;
float tt=0;

void delay_b20(uint z)//ds18b20 delay 
{
 while(z--);
}

void b20_reset(void)//ds18b20复位 
{
 uchar ready=0;
 DQ=1;
 delay_b20(8);
 DQ=0;
 delay_b20(80);
 DQ=1;
 delay_b20(14);
 ready=DQ;
 delay_b20(20);
}

/*uchar b20_r_bit(void)//读ds18b201Bit
{
 uchar i;
 DQ=0;
 DQ=1;
 for(i=0;i<3;i++);
 return(DQ);
}

void b20_w_bit(uchar bitval)//写ds18b201Bit
{
 DQ=0;
 if(bitval==1)
 DQ=1;
 delay_b20(5);
 DQ=1;
}

void b20_w_byte(uchar val)//write one byte to ds18b20
{
 uchar i,temp;
 for(i=0;i<8;i++)
 {
  temp=val>>1;
  temp=temp&0x01;
  b20_w_bit(temp);
  delay_b20(5);
 }
}

uchar b20_r_byte(void)//读出ds18b20一字节 
{
 uchar i,j,date;
 j=1;
 date=0;
 for(i=0;i<8;i++)
 {
  if(b20_r_bit())
  {
   date=date+(j<   }
  delay_b20(4);
 }
 return(date);
}*/

uchar b20_r_byte(void)//read one byte from DS18B20 
{
 uchar i=0;
 uchar dat=0;
 for(i=0;i<8;i++)
 {
  DQ=0;
  dat>>=1;
  DQ=1;
  if(DQ)
  dat|=0x80;
  delay_b20(4);
 }
 return(dat);
}

void b20_w_byte(uchar dat)//write DS18B20 one byte 
{
 uchar i=0;
 for(i=0;i<8;i++)
 {
  DQ=0;
  DQ=dat&0x01;
  delay_b20(5);
  DQ=1;
  dat>>=1;
 }
}

uint b20_gettemp(void)//获取DS18B20上温度 
{
 b20_reset();
 b20_w_byte(0xcc);
 b20_w_byte(0x44);
 //delay_b20(125);
 b20_reset();
 b20_w_byte(0xcc);
 b20_w_byte(0xbe);
 templ=b20_r_byte();
 temph=b20_r_byte();
 t=temph;
 t<<=8;
 t=t|templ;
 tt=t*0.0625;
 t=tt*10+0.5;
 //delay_b20(200);
 return(t);
}

/*************LCD12864*************/

sbit beer=P2^0;

#define P0 P0

uchar code table1[]={"electronic clock"};
uchar code table2[]={"time:"}; 
uchar code table3[]={"date:"};
uchar code table4[]={"temperature"};
sbit lcd_rs=P1^0;//define LCD pins 
sbit lcd_rw=P1^1;
sbit lcd_en=P1^2;
sbit lcd_psb=P2^4;
sbit lcd_rst=P2^3;

void delay(uint z) //延时 
{
 uint x,y;
 for(x=110;x>0;x--);
  for(y=z;y>0;y--);
}

void lcd_w_com(uchar com)//写命令 
{
 lcd_rs=0;
 lcd_rw=0;
 lcd_en=0;
 P0=com;
 delay(5);
 lcd_en=1;
 delay(5);
 lcd_en=0;
}

void lcd_w_data(uchar date)// 写数据 
{
 lcd_rs=1;
 lcd_rw=0;
 lcd_en=0;
 P0=date;
 delay(5);
 lcd_en=1;
 delay(5);
 lcd_en=0;
}

void lcd_init()//LCD initialization 
{
 lcd_rst=0;
 delay(10);
 lcd_rst=1;
 delay(10);
 lcd_psb=1;

 P3=0xff;
 key5=1;
 key6=1;

 lcd_w_com(0x30);
 lcd_w_com(0x0c);
 lcd_w_com(0x14);
 lcd_w_com(0x01);
}

void lcd_pos(uchar x,uchar y)//Select write position 
{
 uchar pos;
 if(x==0)
  {x=0x80;}
 else if(x==1)
  {x=0x90;}
 else if(x==2)
  {x=0x88;}
 else if(x==3)
  {x=0x98;}
 pos=x+y;
 lcd_w_com(pos);
}

void clr_screen()//清屏 
{
 lcd_w_com(0x34);
 delay(5);
 lcd_w_com(0x30);
 delay(5);
 lcd_w_com(0x01);
}

void disp_chinese(uchar code *s)
{
 while(*s>0)
 {
  lcd_w_data(*s);
  s++;
  delay(500);
 }
}

void disp_temp()
{
 uint i=0;
 uchar a=0,b=0,c=0;

 /*uint shi,ge,xiaoshu; 
 uchar a=0,b=0,c=0;
 lcd_pos(3,3);
 i=b20_gettemp();

 shi=i/100;
 ge=i/10%10;
 xiaoshu=i%10;

 lcd_pos(3,3);
 lcd_w_data (wendu[shi]);
 lcd_w_data (wendu[ge]);
 lcd_w_data (0x2e);
 lcd_w_data (wendu[xiaoshu]);
 disp_chinese("℃");*/

 lcd_pos(3,3);
 i=b20_gettemp();
 a=i/100;
 lcd_w_data(a+0x30);
 b=i/10-a*10;
 lcd_w_data(b+0x30);
 lcd_w_data(0x2e);
 c=i-a*100-b*10;
 lcd_w_data(c+0x30);
 disp_chinese("℃");
}

void main()
{
 lcd_init();
 while(1)
 {
  disp_title();
  disp_temp();
 }
}

Keywords:DS18B20 Reference address:DS18B20 Test Program

Previous article:DS1302 Test Procedure
Next article:AT89S52 keyboard program

Recommended ReadingLatest update time:2024-11-17 02:46

DS18B20 temperature measurement program based on ATMEGA16
Download the complete program source code: http://www.51hei.com/f/avrds.rar main function: /* Program function: 18B20 temperature measurement results are displayed on 1602 LCD Author: Zhu Bo Date: January 31, 2012 Description: PA5 is the input port 18B20 parameters: temper
[Microcontroller]
DS18B20 temperature measurement program based on ATMEGA16
Assembly program for connecting DS18b20 and 51 microcontroller
This assembly program is only suitable for the connection between a single DS18B20 and a 51 microcontroller, and the crystal oscillator is about 12MHZ DQ: DS18B20 data bus pin FLAG1: flag bit, when "1" means that DS18B20 is detected TEMPER_NUM: save the read temperature data TEMPER_LEQU36H TEMPER_HEQU35H DQBITP1.7
[Microcontroller]
Temperature display alarm system based on AT89S52 and DS18B20
  introduction   Temperature is one of the most basic environmental parameters. People's living environment is closely related to temperature, and temperature measurement is also of great concern to people. Therefore, it is of great significance to study the temperature measurement method and device, and temperature
[Microcontroller]
Temperature display alarm system based on AT89S52 and DS18B20
Temperature alarm program of 51 single chip microcomputer + 1602 + DS18B20
The temperature display is made by 51 single chip microcomputer, and the temperature is displayed on LCD1602 LCD screen. Then the temperature threshold can be adjusted by pressing the button. If the temperature is higher or lower than the set temperature, the buzzer will sound... The microcontroller source progr
[Microcontroller]
Temperature alarm program of 51 single chip microcomputer + 1602 + DS18B20
Design of a simple temperature-controlled intelligent fan control system
0 Introduction In recent years, with the continuous improvement of people's living standards and technological levels, household appliances have become increasingly refined in terms of style and function, and are developing in the direction of health, safety, multi-function, and energy saving. Electric fans are
[Microcontroller]
Design of a simple temperature-controlled intelligent fan control system
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号