Use DS18B20 to measure temperature and display it through LCD1602 Proteus simulation + DS1302 clock module

Publisher:cwk2003Latest update time:2022-08-11 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

main.c 


#include

#include

#include

#include

 

#define uchar unsigned char

#define uint unsigned int

 

 

uchar code Write_addr [] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; // write address

uchar code Read_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; // read address

uchar Time [7] = {0x50, 0x59, 0x23, 0x07, 0x06, 0x02, 0x22}; // Initial value setting

// Seconds Minutes Hours Day Month Week Year

 

//-----------------------------------Variable area---------------------------------

uint Temp; // temperature variable

 

//-----------------------------------Function area---------------------------------

 

void Int_DS1302()

{

flying i;

Write_Ds1302_Byte( 0x8e,0x00 );

for(i = 0;i < 8;i++)

{

Write_Ds1302_Byte( Write_addr [i],Time [i] );

}

Write_Ds1302_Byte( 0x8e,0x80 );

}

 

void Read_DS1302_Time()

{

flying i;

for(i = 0;i<8;i++)

{

Time [i] = Read_Ds1302_Byte( Read_addr [i] );

}

}

 

void Display_Time()

{

LCD_ShowNum(2,15,Time [0]%16,1);

LCD_ShowNum(2,14,Time [0]/16,1);

LCD_ShowChar(2 ,13,':');

LCD_ShowNum(2,12,Time [1]%16,1);

LCD_ShowNum(2,11,Time [1]/16,1);

LCD_ShowChar(2 ,10,':');

LCD_ShowNum(2,9,Time [2]%16,1);

LCD_ShowNum(2,8,Time [2]/16,1);

LCD_ShowChar(2 ,7,'-');

LCD_ShowNum(2,6,Time [3]%16,1);

LCD_ShowNum(2,5,Time [3]/16,1);

LCD_ShowChar(2 ,4,'-');

LCD_ShowNum(2,3,Time [4]%16,1);

LCD_ShowNum(2,2,Time [4]/16,1);

}

 

void main()

{

Int_DS1302();

LCD_initial();

while(1)

{

Read_DS1302_Time();

Temp = Read_Temp();

string(0x82,"Temp");

LCD_ShowChar(1 ,7,':');

LCD_ShowChar(1 ,11,'C');

LCD_ShowNum(1,8,Temp,3); //The first row directly displays the temperature, occupying four cells

//---------------------------------display time----------------------------------------

Display_Time();

}

}


onewire.c


#include "reg52.h"

#include

 

#define uchar unsigned char

#define uint unsigned int

 

sbit DQ = P3^3; //single bus interface

 

// Single bus delay function

void Delay_OneWire(unsigned int t)  //STC89C52RC

{

while(t--);

}

 

//Write a byte to DS18B20 via single bus

void Write_DS18B20(unsigned char dat)

{

unsigned char i;

for(i=0;i<8;i++)

{

DQ = 0;

DQ = that&0x01;

Delay_OneWire(12);

DQ = 1;

that >>= 1;

}

Delay_OneWire(5);

}

 

//Read a byte from DS18B20

unsigned char Read_DS18B20(void)

{

unsigned char i;

unsigned char dat;

  

for(i=0;i<8;i++)

{

DQ = 0;

that >>= 1;

DQ = 1;

if(DQ)

{

that |= 0x80;

}     

Delay_OneWire(5);

}

return that;

}

 

//DS18B20 device initialization

bit init_ds18b20(void)

{

  bit initflag = 0;

 

  DQ = 1;

  Delay_OneWire(12);

  DQ = 0;

  Delay_OneWire(80);

  DQ = 1;

  Delay_OneWire(10); 

    initflag = DQ;     

  Delay_OneWire(5);

  

  return initflag;

}

 

 

uint Read_Temp()

{

uint high,low,Date_T;

init_ds18b20();

Write_DS18B20(0xcc);

Write_DS18B20(0x44);

Delay_OneWire(225); 

init_ds18b20();

Write_DS18B20(0xcc);

Write_DS18B20(0xbe);

low = Read_DS18B20();

high = Read_DS18B20();

Date_T = high<<8;

Date_T = Date_T+low;

Date_T = Date_T * 0.0625+0.5;//

return Date_T;

}


onewire.h


#ifndef __ONEWIRE_H

#define __ONEWIRE_H

 

#define uchar unsigned char

#define uint unsigned int

 

unsigned char rd_temperature(void);  //; ;

unsigned char Read_DS18B20(void);

void Write_DS18B20(unsigned char dat);

bit init_ds18b20(void);

uchar Read_Temp(void);

#endif

LCD.c


#include

#include

 

#define uchar unsigned char

#define uint unsigned int

#define  out P2

sbit rs = P1^5;

sbit rw = P1^4;

sbit e = P1^3;

 

void delay(uint j)

{

fly i=250;

for(;j>0;j--)

{

while(--i);

i=249;

while(--i);

i=250;

}

}

 

void check_busy(void)

{

flying dt;

do

{

dt=0xff;

e=0;

rs=0;

rw=1;

e=1;

dt=out;

}while(dt&0x80);

e=0;

}

 

void write_command(uchar com) //write command

{

check_busy();

e=0;

rs=0;

rw=0;

out=com;

e=1;

_nop_();

e=0;

delay(1);

}

void write_data(uchar dat)//write data 

{

check_busy();

e=0;

rs=1;

rw=0;

out=that;

e=1;

_nop_();

e=0;

delay(1);

}

void LCD_initial(void) //initialization

{

delay(2);

write_command(0x38);

delay(1);

write_command(0x38);

write_command(0x08);

write_command(0x01);

write_command(0x06);

write_command(0x0c);

//delay(1);

}

void string(uchar ad,uchar *s) //display characters

 {

write_command(ad);

while(*s>0)

{

write_data(*s++);

delay(100);

}

}

 

int LCD_Pow(uint x,uint y)//

{

unsigned char i = 0;

int result = 1;

for(i=0;i {

result *= x;

}

return result;

}

 

void LCD_SetCursor(uchar Line,uchar Columu) //Line selection

{

if(Line == 1)

{

write_command(0x80|(Columu-1));

}

else

{

write_command(0x80|( Columu-1)+0x40);

}

}

 

 

// row grid number length

void LCD_ShowNum(uchar Line,uchar Columu,uint Num,uint Length)//Display number

{

uint i = 0;

 

LCD_SetCursor(Line,Columu);

 

for(i=Length;i>0;i--)

{

write_data('0'+Num/LCD_Pow(10,i-1)%10);

}

}

 

void LCD_ShowChar(unsigned char Line, unsigned char Columu, unsigned char Char) //Write a character

{

LCD_SetCursor(Line,Columu);

 

write_data(Char);

}


LCD.h


#ifndef __LCD_H__

#define __LCD_H__

 

#define uchar unsigned char

#define uint unsigned int

 

void check_busy(void);

void write_command(unsigned char com);

void write_data(uchar dat);

void LCD_initial(void);

void string(flyer name ,flyer *s);

void lcd_test(void);

void delay(uint);

void LCD_ShowNum(uchar Line,uchar Columu,uint Num,uint Length);//Display number

void LCD_SetCursor(float Line, float Column);

int LCD_Pow(volatile x,volatile y);

void LCD_ShowChar(unsigned char Line, unsigned char Columu, unsigned char Char); //Write a character

#endif


ds1302.c


#include

#include

 

sbit SCK = P3^1;

sbit SDA = P3^2;

sbit RST = P3^0; // DS1302 reset

 

#define uchar unsigned char

#define uint unsigned int

//uchar code Write_addr [] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //write address

//uchar code Read_addr[] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //read address

//uchar Time [7] = {0x50, 0x59, 0x23, 0x00, 0x00, 0x00, 0x00}; //initial value setting

                 Seconds Minutes Hours Day Month Week Year

 

void Write_Ds1302(unsigned  char temp) 

{

unsigned char i;

for (i=0;i<8;i++)     

SCK=0;

SDA=temp&0x01;

temp>>=1; 

SCK=1;

}

}   

 

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     

{

  RST=0; _nop_();

  SCK=0; _nop_();

  RST=1; _nop_();  

  Write_Ds1302(address);

  Write_Ds1302(that);

  RST=0; 

}

 

unsigned char Read_Ds1302_Byte ( unsigned char address )

{

  unsigned char i,temp=0x00;

  RST=0; _nop_();

  SCK=0; _nop_();

  RST=1; _nop_();

  Write_Ds1302(address);

  for (i=0;i<8;i++)

  {

SCK=0;

temp>>=1;

  if(SDA)

  temp|=0x80;

  SCK=1;

  RST=0; _nop_();

  SCK=0; _nop_();

SCK=1; _nop_();

SDA=0; _nop_();

SDA=1; _nop_();

return (temp);

}


 


ds1302.h


#ifndef __DS1302_H

#define __DS1302_H

 

void Write_Ds1302(unsigned char temp);

void Write_Ds1302_Byte( unsigned char address,unsigned char dat );

unsigned char Read_Ds1302_Byte( unsigned char address );

 

#endif

Simulation diagram

Reference address:Use DS18B20 to measure temperature and display it through LCD1602 Proteus simulation + DS1302 clock module

Previous article:51 MCU connected to ADC0808 to make a simple voltage meter
Next article:Use the timer to make the P1^0 port output a specified duty cycle and periodic rectangular pulse

Recommended ReadingLatest update time:2024-11-23 10:40

LCD1602 LCD screen display introduction
The following is the protues simulation circuit. You can see the second line on the screen clearly, I will tell you how to display it later. The first line is the Chinese character "取" (left and right are written reversely) that I customized for "确定" and "注销". I found that the 1602 can only display Chinese characters f
[Microcontroller]
LCD1602 LCD screen display introduction
Single chip microcomputer controls LCD1602 display to display character strings dynamically
Simulation circuit diagram: Simulation experiment results: C language program code: //------------------------------------------------*/ #include reg52.h //Include the header file. Generally, no modification is required. The header file contains the definition of special function registers. #include intrins.h
[Microcontroller]
Single chip microcomputer controls LCD1602 display to display character strings dynamically
51 Microcontroller Basics LCD1602
Exterior: principle: LCD1602 is an industrial character type liquid crystal that can display 16x2 or 32 characters at the same time. LCD1602 liquid crystal display principle The principle of LCD1602 liquid crystal display is to use the physical properties of liquid crystal to control its display area through voltage
[Microcontroller]
51 Microcontroller Basics LCD1602
PIC LCD1602 Display
#include       //Call the header file, you can go to the PICC18 software to find PIC18FXX2.H __CONFIG(1,XT);         //Crystal oscillator is external 4M __CONFIG(2,WDTDIS);     //Watchdog off __CONFIG(4,LVPDIS);     //Disable low voltage programming #define uint unsigned int #define uchar unsigned char #
[Microcontroller]
Latest Microcontroller Articles
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号