Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

DS18B20 hardware schematic diagram and function introduction

DS18B20 is a digital temperature sensor, single bus interface, reading temperature only need an IO port, there are different packaging forms, support different environments. Each DS18B20 and 64-bit ROM serial number, support an IO port mounted multiple DS18B20, distinguish equipment, that is, support cascade, very convenient, do not charge IO port.

The following is introduced in STM32F407 if the drive DS18B20 read temperature, the code is prepared by KEil, in the form of multiple files, the code is divided into three parts:

  1. Ds18b20.c file, mainly is the realization of the core function.
  2. Ds18b20.h file, function declaration.
  3. The main c files. Main function, call DS18B20 read the file, and convert after printing.

Here is the schematic of DS18B20 on the development board:

DS18B20 core code

2.1 DS18B20. C source code

#include "ds18b20.h"

/* Function function: DS18b20IO port initialization hardware connection: PG9 multiplexing means automatic output data general means manual output data */
void DS18B20_Init(void)
{
	/*1. Start the clock */
	RCC->AHB1ENR|=1<<6;// Enable the PORTG clock function

	/*2. Initialize the I/O mode
	DS18B20_OUTPUT(a); }/* Wait for DS18B20 to return 1: DS18B20 does not exist returns 0: DS18B20 exists */
u8 DS18B20_Check(void) 	   
{   
	u8 retry=0;
	DS18B20_INPUT(a)// Set the DS18B20 input mode
	while(DS18B20_IN&&retry<200)
	{
		retry++;
		DelayUs(1);
	};	 
	if(retry>=200)return 1;
	else retry=0;
	while(! DS18B20_IN&&retry<240)
	{
		retry++;
		DelayUs(1);
	};
	if(retry>=240)return 1;	    
	return 0;
}


/* Read a byte from DS18B20 return value: read data */
u8 DS18B20_ReadByte(void)
{        
	u8 i,data=0;
	for(i=0; i<8; i++) {DS18B20_OUTPUT(a);// initialize to output mode
		DS18B20_OUT=0;		 //输出0 
		DelayUs(2);        
		DS18B20_OUT=1;     // Pull up the bus level
		DS18B20_INPUT(a)// Initialize to input mode
		DelayUs(12);
		data>>=1;
		if(DS18B20_IN)
		{
			data|=0x80; 		
		}			
		DelayUs(50);
	}						    
  return data;
}


/* Write a byte to DS18B20 dat: the byte to write */
void DS18B20_WriteByte(u8 dat)     
{             
	u8 i;
	DS18B20_OUTPUT(a);// Initialize IO to output mode
	for(i=0; i<8; i++) {if(dat&0x01) // Start low post
		{
			DS18B20_OUT=0;//输出0
			DelayUs(2);                            
			DS18B20_OUT=1;/ / output 1
			DelayUs(60);             
		}
		else 
		{
			DS18B20_OUT=0;//输出0
			DelayUs(60);             
			DS18B20_OUT=1;/ / output 1
			DelayUs(2);                          
		}
		dat>>=1; }}/* Get temperature value from ds18B20 precision: 0.1c Return value: temperature value (-550~1250) */
short DS18B20_Get_Temp(void)
{
	u16 temp;
	u8 TL,TH;
	DS18B20_OUTPUT(a); DS18B20_OUT=0;		// Outputs 0 // lowers DQ
	DelayUs(750);     / / lower 750 us
	DS18B20_OUT=1;    //DQ=1
	DelayUs(15);      //15US	  

	DS18B20_Check(a);DS18B20_WriteByte(0xcc); // Skip chip ID check
	DS18B20_WriteByte(0x44); // Convert the temperature once

	DS18B20_OUTPUT(a); DS18B20_OUT=0;   // Outputs 0 // lowers DQ
	DelayUs(750);    / / lower 750 us
	DS18B20_OUT=1;	 //DQ=1
	DelayUs(15);     //15US
	DS18B20_Check(a);DS18B20_WriteByte(0xcc);// Skip chip ID check
	DS18B20_WriteByte(0xbe);// Read the converted temperature data successfully
	TL=DS18B20_ReadByte(a);// LSB   
	TH=DS18B20_ReadByte(a);// MSB  
	temp=((u16)TH<<8)|TL;
	return temp;
}

Copy the code

DS18B20. H source


#ifndef _DS18B20_H
#define _DS18B20_H
#include "stm32f4xx.h"

#include "sys.h"
#include "delay.h"
void DS18B20_Init(void);
short DS18B20_Get_Temp(void);
#define DS18B20_IN PGin(9)
#define DS18B20_OUT PGout(9)

#defineDS18B20_INPUT() \ {\ GPIOG->MODER&=~(0x3<<9*2); \ GPIOG->MODER|=0x0<<9*2; \ GPIOG->PUPDR&=~(0x3<<9*2); \ GPIOG->PUPDR|=0x1<<9*2; The \}
		
#defineDS18B20_OUTPUT() \ {\ GPIOG->MODER&=~(0x3<<9*2); \ GPIOG->MODER|=0x1<<9*2; \ GPIOG->OTYPER&=~(0x1<<9); \ GPIOG->OSPEEDR&=~(0x3<<9*2); \ GPIOG->OSPEEDR|=0x2<<9*2; \ GPIOG->ODR|=1<<9; The \}
#endif

Copy the code

The main c source code

#include "stm32f4xx.h" // Device header
#include "led.h"
#include "delay.h"
#include "key.h"
#include "usart.h"
#include "sys.h"
#include "exti.h"
#include "timer.h"
#include "pwm.h"
#include "ds18b20.h"

int main(void)
{
	short temp;
	unsigned short intT,decT; 	  // Integer and fractional parts of the temperature value
	LED_Init(a);KEY_Init(a);USART1_Init(84.115200);
	KEY_EXTI_Init(a);DS18B20_Init(a);while(1)
	{
	  /* Read the temperature information */
		temp=DS18B20_Get_Temp(a); intT = temp >>4;             // Separate out the integral part of the temperature value
		decT = temp & 0xF;            // Separate out the decimal part of the temperature value
		printf("DS18B20: %d.%d *C\r\n", (int)intT,(int)decT);
		DelayMs(1000); }}Copy the code

Temperature reading effect