PIC microcontroller temperature sensor DS18B20 C language program code

Publisher:二进制心灵Latest update time:2019-11-18 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*This temperature sensor
is written by myself, the timing has been corrected, and the self-test delay time
uses the Tianxiang temperature conversion module algorithm
*/

#include

__CONFIG(0x3B31);

#define uint unsigned int

#define uchar unsigned char

#define DQ RC1

#define DQ_DIR TRISC1

#define DQ_HIGH() TRISC1=1

#define DQ_LOW() DQ=0;TRISC1=0


unsigned char shi; // tens digit

unsigned char ge; // integer digit

unsigned char shifen; //tenths place

unsigned char baifen; //percentile

unsigned char qianfen; //thousandths

unsigned char wanfen; //ten thousandth place


const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Display array 0


void delayus(char x,char y) //us level delay provided by Huijing

{

char z; //define Z

do { //Execute do once first

  z=y; //Give the value of Y to Z

  do{;}while(--z); //do empty statement, wait for --z, until z=0 ends, do--while statement, delay occurs here

  }     

while (--x); //Similarly, x is in--, and a delay is generated again

}


void delay(uint x)

{

uint a,b;

for(a=x;a>0;a--)

for(b=110;b>0;b--);

}

void display(char shi,char ge,char shifen,char baifen,char qianfen)

{

PORTD=table[shi];

PORTA=0x02;

delay(2);


PORTD=table[ge]|0x80; //or 0x80 to display the decimal point

PORTA=0x04;

delay(2);


PORTD=table[shifen];

PORTA=0x06;

delay(2);


PORTD=table[baifen];

PORTA=0x08;

delay(2);


PORTD=table[qianfen];

PORTA=0x0a;

delay(2);

}


void init(void)

{

TRISA=0;PORTA=0x00;

TRISC=0xf0;PORTC=1;

TRISD=0;

}


void reset(void)

{

char pe=1;

while(pe)

{

DQ_LOW();

delayus(2,81); //delay 502us

DQ_HIGH();

delayus(4,4); //delay 71us

if(DQ==1)pe=1; //Judge whether to respond (pull to low level when responding), set to 1 and resend in a loop if no response

else pe=0; //Otherwise it is a response, set to 0 to exit the loop

delayus(2,81); //delay 502us

}

}


void write_byte(char val)

{

uchar i,temp;

for(i=8;i>0;i--) //Loop 8 times to form a byte

{

temp=val&0x01; //Take out the lowest bit, and take out 1

DQ_LOW();

delayus(1,1); //delay 15us

if(temp==1)DQ_HIGH(); //If the value taken out is 1, pull it to high level and send it out

delayus(3,3); //Delay 45us, if the taken out is 0, also send it

DQ_HIGH(); //Pull up to high level

NOP();NOP(); //Delay 2us

val=val>>1; //Move right once for next extraction

}

}


uchar read_byte(void)

{

uchar i,val=0;

static bit j; //static bit variable, a status bit, cannot be a byte

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

{

val=val>>1; //Move one position first

DQ_LOW(); //Pull to low level

NOP();NOP();NOP();NOP();NOP();NOP(); //Delay 6us

DQ_HIGH();   

NOP();NOP();NOP();NOP(); //Delay 4us

j=DQ; //Read the state of the data line to get a state bit for data processing //So we need to define static bit j;

if(j==1)val=val|0x80; //Data processing: If the value read is 1, put it in the highest bit first, and then shift it back one by one to form a byte

delayus(1,6); //Delay 30us to repeat the above steps

}

return(val); //Construct 1 byte and return

}


void get_temp(void) //01:40:26 //Get temperature, device matching (multiple temperature sensors)

{

uchar TLV,THV,num; //tem1/tem2; there are also 2 bytes of temperature instructions

float aaa;

uint temper;

reset(); //Reset

write_byte(0xCC); //skip ROM

write_byte(0x44); //Temperature conversion, delay required

for(num=100;num>0;num--) //Originally, delay(1000) was used to delay the time for 1 second. But the effect disappears after a short shake.

  display(shi,ge,shifen,baifen,qianfen); //So use the display to replace the delay, display 100 times is about 750ms or more

reset(); //Before each RAM operation, reset 18B20 and match

write_byte(0xCC); //skip ROM

write_byte(0xBE); //Tell it that I will read your temperature next, read the register

TLV=read_byte(); //RAM has 9 bytes (we only need 2 bytes, LSB and MSB), it reads from the lowest bit

THV=read_byte(); //01:45:10 + Ruizhi at 58:52

DQ_HIGH(); //Release the bus

aaa=(THV*256+TLV)*0.0625*1000; //(16-bit temperature data)*0.0625 is the actual temperature (decimal number)

temper=(int)aaa; //Because of the compile-time warning; there is a decimal point, it is a floating point to integer conversion; we use forced conversion to integer to get the value of the decimal point (the decimal point is not easy to get, use multiplication by 100 to get it)

shi=temper/10000; //Distribute the tens to five digital tubes. It feels weird to display four digits, so use five digital tubes; at 1:51:00

ge=temper%10000/1000; //I want to use five digital tubes to display, so it is 10000 five-digit number

shifen=temper%1000/100; //

baifen=temper%100/10; //

qianfen=temper%10; // 

}


void main()

{

init();

while(1)

{

get_temp();

display(shi,ge,shifen,baifen,qianfen); //The compiler will not pass without formal parameters, and it will not pass with class parameters either

}

}



Reference address:PIC microcontroller temperature sensor DS18B20 C language program code

Previous article:PIC microcontroller development board independent keyboard scanning C language code
Next article:PIC microcontroller development board independent keyboard scanning + buzzer sound + digital tube C program code

Recommended ReadingLatest update time:2024-11-16 12:03

Problems encountered when porting ds18b20 to stm32f103
    I have used the DS18B20 temperature sensor on an AVR microcontroller before. Now I need to use it on the STM32F103 platform. I made corresponding modifications based on the original code. The problem I encountered during debugging was that the measured value always showed 127.9375 (the temperature data read was 0x7
[Microcontroller]
DS18B20 and Digital Thermometer
DS18B20 is a temperature detection device with single bus and digital features. Its appearance and structure are relatively fragile. Generally speaking, it is only suitable for detecting room temperature. It cannot withstand slightly worse environmental conditions. In industrial situations, PT100 is generally used bec
[Microcontroller]
DS18B20 and Digital Thermometer
ARM Litian Electronics LPC2148 Detailed explanation of the temperature acquisition experimental procedure based on DS18b20
1#include NXP/iolpc2148.h 2#include stdio.h 3#include "uart.h" //System clock 4#define Fosc 12000000UL //Crystal oscillator clock 5#define Fcclk (Fosc * 5) //System frequency, must be an integer multiple of Fosc (1~32) and = 60MHZ 6#define Fcco (Fcclk * 4) //CCO frequency must be 2, 4, 8, or 16 times of Fcclk, ra
[Microcontroller]
ARM Litian Electronics LPC2148 Detailed explanation of the temperature acquisition experimental procedure based on DS18b20
AVR microcontroller (ATmega16L) DS18B20 temperature sensor driver
  I downloaded the English version of the datasheet from the DALLAS website and started reading from the first line. I was surprised to find that it was not as difficult to understand as I had thought. Except for a few words I didn't know, I could understand the general meaning. I installed Kingsoft PowerWord and look
[Microcontroller]
AVR microcontroller (ATmega16L) DS18B20 temperature sensor driver
Digital Temperature Measurement System Based on ARM and DS18B20
        0 Introduction   Thermal error is the biggest error source of CNC machine tools. Temperature test of CNC machine tools provides a basis for compensating thermal errors of machine tools. The traditional temperature measurement scheme is to transmit analog signals over long distances through cables to data acq
[Microcontroller]
Digital Temperature Measurement System Based on ARM and DS18B20
Design and implementation of digital temperature sensor based on DS18B20
  At present, in many fields of industrial control , temperature monitoring generally uses a temperature measurement circuit composed of thermistors, and temperature measurement is achieved after A/D and D/A conversion. However, due to the instability of thermistors, temperature measurement is easily affected by exter
[Microcontroller]
Design and implementation of digital temperature sensor based on DS18B20
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号