PIC16F877A DS18B20 Digital Thermometer Experiment

Publisher:月光男孩Latest update time:2019-01-31 Source: eefocusKeywords:PIC16F877A  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

main.c


#include

#include

#include "def.h"

#include "ds18b20.h"

 

__CONFIG(0xFF32);

 

void Delay_ms(u16 xms)

{

  int i,j;

  for(i=0;i

  { for(j=0;j<71;j++) ; }

}

 

void uart_init(void)

{

    TXSTA=0x24; //Turn on the transmit enable bit and high baud rate. The initial value of TRMT can be 0 or 1.

    RCSTA=0x80;

    SPBRG=0x19; //4M crystal oscillator, baud rate 9600, then the initial value of SPBRG is 25

}

 

//printf function redirection

void putch(u8 c)

{

    TXREG = c;

    while(TRMT==0);

}

 

short temperature;

 

void main()

{

uart_init();

Delay_ms(5);

printf("ds18b20 demo\n");

while(1)

{

if(Init_18B20()==0)

{

printf("init ok1!\n");

Skip();

Convert();

Delay_ms(750);

if(Init_18B20()==0)

{

printf("init ok2!\n");

Skip();

Read_SP();

RomCode[0]=Read_18B20();

RomCode[1]=Read_18B20(); 

temperature=ReadTemp(RomCode);

temperature=CalcTemp(temperature);

printf("code=%02x%02x\t->\t%d.%d℃\n",RomCode[0],RomCode[1],temperature/10,temperature%10);

}

}

Delay_ms(3000);

}

}

ds18b20.h


#include

#include "def.h"

 

#ifndef __ds1820_h

#define __ds1820_h

 

external u8 RomCode[2];

 

#define DQ1     RC1=1

#define DQ0     RC1=0

#define DQ_in   TRISC1=1

#define DQ_out  TRISC1=0

#define DQ_val  RC1

 

#define _DINT() of()

#define _EINT() ei()

#define _NOP() asm("nop")

 

 

u8 Init_18B20(void);

short CalcTemp(short a);

void Skip(void);

void Convert(void);

void Read_SP(void);

u8 Read_18B20(void);

short ReadTemp(u8 *r);

 

#endif

ds18b20.c


/*********************************

PIC16F877A@4MHz

*********************************/

#include "ds18b20.h"

#include "def.h"

 

u8 RomCode[2];

 

#define DelayNus _delay

 

u8 Init_18B20(void)

{

  u8 Error;

  

  DQ_out;

  _DINT();

  DQ0;

  DelayNus(500); //Delay at least 480us

  DQ1;

  DelayNus(55); //Release DQ and then read after 120us (60~240)

  DQ_in;

  _NOP();

  if(DQ_val)      

    Error = 1; // Initialization failed

  else

    Error = 0; // Initialization successful

  DQ_out;

  DQ1;

  _UNITE();

  

  DelayNus(400);

  

  return Error;

}

 

void Write_18B20(u8 wdata)

{

  u8 i;

  

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

  {

    _DINT();

    DQ0;

    //DelayNus(6); //Delay 2us

    _NOP();

    _NOP();

    _NOP();

    _NOP();

    _NOP();

    _NOP();

    _NOP();

    _NOP();

    if(wdata & 0X01)    

    DQ1;

    else                

    DQ0;

    wdata >>= 1;

    

    DelayNus(70); //Delay 91us 

    DQ1;

    _UNITE();

    _NOP();

  } 

}

 

 

u8 Read_18B20(void)

{

  u8 i;

  u8 temp = 0;   

  

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

  {

    temp >>= 1;

    _DINT();   

    DQ0;

    _NOP();

    _NOP();

    _NOP();

    _NOP();

 

    DQ1;

    //DelayNus(8); //Delay 9us

    DQ_in;

    //DelayNus(25); //Delay 45us

    if(DQ_val)   

      temp |= 0x80;    

    DQ_out;

    DQ1;

    DelayNus(70); //Delay 10us

    _UNITE();

    _NOP();

  }      

  return  temp;

}

 

void Skip(void)

{

    Write_18B20(0xcc);

}

 

void Convert(void)

{

    Write_18B20(0x44);

}

 

void Read_SP(void)

{

    Write_18B20(0xbe);

}

 

short ReadTemp(u8 *r)

{

    u8 temp_low;

    short temp;

    

    temp_low = r[0]; //read low bit

    temp = r[1]; //read high bit

    temp = (temp<<8) | temp_low;

    return  temp;

}

 

const u8 dottab[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};

//Optimize reading algorithm

short CalcTemp(short a)

{

  u8 b;

  

  if(a<0)

  {

    a=~a;

    a++; 

    b=a;

    a>>=4;

    a*=-10;

    a-=dottab[b&0x0f];    

  }

  else

  {

    b=a;

    a>>=4;

    a*=10;

    a+=dottab[b&0x0f];       

  }

 

  return a;

}

 

 

def.h


#ifndef __DEF__H_

#define __DEF__H_

 

#define u8 unsigned char

#define u16 unsigned int

 

#endif

 

Keywords:PIC16F877A  DS18B20 Reference address:PIC16F877A DS18B20 Digital Thermometer Experiment

Previous article:PIC microcontroller - PWM wave
Next article:PIC16F877A internal EEPROM read and write demonstration Demo

Recommended ReadingLatest update time:2024-11-16 11:51

Detailed explanation of the program code of the temperature control system based on DS18B20 of lpc2148: dat=(j1)
//Read one bit of data (read bit procedure) 1unsigned char tmpreadbit(void) //read a bit 2{ 3 unsigned char dat;  4 DS_OUT(); //Set the port to output 5 DS_L(); //DS=0 6 //Delayn(1); //2uS 7 DS_IN(); //input 8 //Delayn(1); 9 if(DS_R()!=0) 10 dat=1; 11 else  12 dat=0; 13 DelaynUs(50);     14 return (dat); 15} //Read
[Microcontroller]
DS18B20 reading and writing program
#include reg51.h //Header file containing microcontroller registers #include intrins.h //Header file containing _nop_() function definition #define uchar unsigned char #define uint unsigned int sbit date=P3^7; unsigned char code table ={0x3f, 0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39
[Microcontroller]
PIC16F877A drives 12864 LCD
This program is equipped with Jinpeng's 12864 LCD using serial mode head File #ifndef LCD12864_H #define LCD12864_H #include  "main.h" //define control port pin //serial mode //#define SCLK RD4  //#define STD RD5 //#define LCD_EN RD6  //parallel mode #define PSB RA0 #define CS RA1  #define STD RA2 #defi
[Microcontroller]
Design of a smart temperature-sensing water meter
  The temperature-sensing water meter designed in this paper uses the AT89C52 single-chip microcomputer as the core control chip, and adds temperature sensors, flow sensors, peripheral display circuits and E2PROM storage devices. The system uses a double reed switch to design the flow sensor, which improves the measur
[Microcontroller]
Design of a smart temperature-sensing water meter
PIC16F877A microcontroller (IIC bus + AT24C02 chip)
1 Basic principles 1.1 IIC bus It should be noted that the upper four bits are the same for the same type of chip. For example, the upper four bits of the AT24C02 chip are fixed to 1010. 1.2 AT24C02 chip 2 Source code /*---------------------Function: IIC bus AT24C02 --------------------
[Microcontroller]
PIC16F877A microcontroller (IIC bus + AT24C02 chip)
Multi-point temperature measurement system based on DS18B20
1 Temperature Sensor DS18B20 Introduction   DALLAS's single-wire digital temperature sensor DS18B20 is a new "one-wire device" with the characteristics of small size and wide applicable voltage. The unique and economical characteristics of the one-wire bus allow users to easily build a sensor network and introduce a n
[Microcontroller]
Multi-point temperature measurement system based on DS18B20
Atmega16 MCU experiment: DS18B20 and ad acquisition and display program
rem Main.bas file generated by New Project wizard rem rem Created: Sunday September 16, 2018 rem Processor: ATmega16 rem Compiler: BASCOM-AVR rem Write your code here $regfile = "m16def.dat" $crystal = 2000000 Config 1wire = Portb.0 'Define BS18B20 connected to PB0 Config Lcdpin = Pin , Db4 = Porta.4 , Db5 = Porta.5
[Microcontroller]
Atmega16 MCU experiment: DS18B20 and ad acquisition and display program
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号