When you click the mouse, STM32CubeMX reads the data of DS18B20 temperature sensor, and the serial port prints and displays it

summary

The title of all the work to be done in this paper basically includes reading the temperature value of the temperature sensor, and then printing it to the serial assistant through the serial port;
A lot of bloggers' great God's tutorial I always follow the steps to make the program do not report errors and detect the sensor's condition, then find the cause and modify it to debug normally.

I use the experimental board of Puzhong technology. The main control chip is STM32103C8T6. As long as it is STM32 board, the configuration of STM32CubeMX is similar.

Introduction to DS18B20

Although the experiment can be made smoothly without looking at this section, it is still necessary to look at it and know about DS18B20 C why do you write that;
The blogger wrote this part more carefully. After the experiment, you can see what you want to study carefully:

https://blog.csdn.net/liuyy_2000/article/details/113754150

Configure STM32CubeMX build project

  1. Open STM32CubeMX and select the corresponding MCU:

2. Configure clock:
There is a crystal oscillator on the board in Puzhong. Here I use an external clock;

Clock tree configuration (because the ds18b20 uses us level delay, the timer is used. The timer is associated with the configuration clock):

Debugging mode configuration:

Configure the GPIO pin connected to ds18b20 (PB8 I use here):

Configure serial port communication USART1 (the board hardware design of Puzhong technology is only connected to the outside):

Timer configuration:

Generate project configuration:

Then click GENERATE CODE to generate the code;

Coding part

Find the project, open it and compile it first;

Write us delay function
First in Tim Declare delay in H_ Us function

/* USER CODE BEGIN Includes */
void delay_us(uint16_t us);
/* USER CODE END Includes */

Then at Tim Write code in C file

/* USER CODE BEGIN 1 */
void delay_us(uint16_t us)
{
	uint16_t differ=0xffff-us-5;   //Set the starting value of timer counter 

	
	HAL_TIM_Base_Start(&htim1);           //Start timer
	__HAL_TIM_SetCounter(&htim1,differ); 
	while(differ < 0xffff-5)                     //Compensation, judgment            
	{ 
		differ = __HAL_TIM_GetCounter(&htim1);   //Count value of query counter 
	} 
	HAL_TIM_Base_Stop(&htim1);
 
}
/* USER CODE END 1 */

Redirect printf function
First at USART H contains input and output header files and definition variables;

/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */

Then at USART Add printf redirection function in C;

/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *fp)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}
 
int fgetc(FILE *fp)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  return ch;
}
/* USER CODE END 0 */

ds18b20.c and DS18B20 H file

ds18b20.h

#ifndef __DS18B20_H
#define __DS18B20_H 

#include "main.h"

#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr))
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))

#define GPIOA_ODR_Addr    (GPIOA_BASE+12)
#define GPIOB_ODR_Addr    (GPIOB_BASE+12)
#define GPIOC_ODR_Addr    (GPIOC_BASE+12)
#define GPIOD_ODR_Addr    (GPIOD_BASE+12)
#define GPIOE_ODR_Addr    (GPIOE_BASE+12)
#define GPIOF_ODR_Addr    (GPIOF_BASE+12)
#define GPIOG_ODR_Addr    (GPIOG_BASE+12)

#define GPIOA_IDR_Addr    (GPIOA_BASE+8)
#define GPIOB_IDR_Addr    (GPIOB_BASE+8)
#define GPIOC_IDR_Addr    (GPIOC_BASE+8)
#define GPIOD_IDR_Addr    (GPIOD_BASE+8)
#define GPIOE_IDR_Addr    (GPIOE_BASE+8)
#define GPIOF_IDR_Addr    (GPIOF_BASE+8)
#define GPIOG_IDR_Addr    (GPIOG_BASE+8)

#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)

#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)

#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)

#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)

#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)

#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)

#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)

//IO operation function
#define 	 DS18B20_DQ_OUT PBout(8) / / data port
#define 	 DS18B20_ DQ_ In pbin (8) / / data port
   	
uint8_t DS18B20_Init(void);			//Initialize DS18B20
short DS18B20_Get_Temp(void);		//Get temperature
void DS18B20_Start(void);			//Start temperature conversion
void DS18B20_Write_Byte(uint8_t dat);//Write a byte
uint8_t DS18B20_Read_Byte(void);	//Read a byte
uint8_t DS18B20_Read_Bit(void);		//Read one bit
uint8_t DS18B20_Check(void);		//Detect the presence of DS18B20
void DS18B20_Rst(void);				//Reset DS18B20
#endif

ds18b20.c

#include "ds18b20.h"
#include "tim.h"


//IO direction setting
void DS18B20_IO_IN(void){
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.Pin = GPIO_PIN_8;
	GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
	HAL_GPIO_Init(GPIOB,&GPIO_InitStructure);
}

void DS18B20_IO_OUT(void){
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.Pin = GPIO_PIN_8;
	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
	HAL_GPIO_Init(GPIOB,&GPIO_InitStructure);
}


//Reset DS18B20
void DS18B20_Rst(void)	   
{                 
	DS18B20_IO_OUT(); //SET PA0 OUTPUT
    DS18B20_DQ_OUT=0; //Pull down DQ
    delay_us(750);    //Pull down 750us
    DS18B20_DQ_OUT=1; //DQ=1 
	delay_us(15);     //15US
}
//Waiting for a response from DS18B20
//Return 1: the existence of DS18B20 is not detected
//Return 0: exists
uint8_t DS18B20_Check(void)
{   
	uint8_t retry=0;
	DS18B20_IO_IN();//SET PA0 INPUT	 
    while (DS18B20_DQ_IN&&retry<200)
	{
		retry++;
		delay_us(1);
	};	 
	if(retry>=200)return 1;
	else retry=0;
    while (!DS18B20_DQ_IN&&retry<240)
	{
		retry++;
		delay_us(1);
	};
	if(retry>=240)return 1;	    
	return 0;
}
//Read a bit from DS18B20
//Return value: 1 / 0
uint8_t DS18B20_Read_Bit(void) 			 // read one bit
{
    uint8_t data;
	DS18B20_IO_OUT();//SET PA0 OUTPUT
    DS18B20_DQ_OUT=0; 
	delay_us(2);
    DS18B20_DQ_OUT=1; 
	DS18B20_IO_IN();//SET PA0 INPUT
	delay_us(12);
	if(DS18B20_DQ_IN)data=1;
    else data=0;	 
    delay_us(50);           
    return data;
}
//Read a byte from DS18B20
//Return value: read data
uint8_t DS18B20_Read_Byte(void) 
{        
    uint8_t i,j,dat;
    dat=0;
	for (i=1;i<=8;i++) 
	{
        j=DS18B20_Read_Bit();
        dat=(j<<7)|(dat>>1);
    }						    
    return dat;
}
//Write a byte to DS18B20
//dat: bytes to write
void DS18B20_Write_Byte(uint8_t dat)
 {             
    uint8_t j;
    uint8_t testb;
	DS18B20_IO_OUT();
    for (j=1;j<=8;j++) 
	{
        testb=dat&0x01;
        dat=dat>>1;
        if (testb) 
        {
            DS18B20_DQ_OUT=0;
            delay_us(2);                            
            DS18B20_DQ_OUT=1;
            delay_us(60);             
        }
        else 
        {
            DS18B20_DQ_OUT=0; 
            delay_us(60);             
            DS18B20_DQ_OUT=1;
            delay_us(2);                          
        }
    }
}
//Start temperature conversion
void DS18B20_Start(void)// ds1820 start convert
{   						               
    DS18B20_Rst();	   
	DS18B20_Check();	 
    DS18B20_Write_Byte(0xcc);
    DS18B20_Write_Byte(0x44);
} 
//Initialize the IO port DQ of DS18B20 and detect the existence of DS at the same time
//Return 1: does not exist
//Return 0: exists    	 
uint8_t DS18B20_Init(void)
{
	DS18B20_Rst();
	return DS18B20_Check();
}  
//Get the temperature value from ds18b20
//Accuracy: 0.1C
//Return value: temperature value (- 550 ~ 1250) 
short DS18B20_Get_Temp(void)
{
    uint8_t temp;
    uint8_t TL,TH;
	short tem;
    DS18B20_Start ();                   
    DS18B20_Rst();
    DS18B20_Check();	 
    DS18B20_Write_Byte(0xcc);// skip rom
    DS18B20_Write_Byte(0xbe);// convert	    
    TL=DS18B20_Read_Byte(); // LSB   
    TH=DS18B20_Read_Byte(); // MSB  
	    	  
    if(TH>7)
    {
        TH=~TH;
        TL=~TL; 
        temp=0;//The temperature is negative  
    }else temp=1;//The temperature is positive	  	  
    tem=TH; //Get the top eight
    tem<<=8;    
    tem+=TL;//Get the bottom eight
    tem=(float)tem*0.625;//transformation     
	if(temp)return tem; //Return temperature value
	else return -tem;    
} 
 

The above is DS18B20 C and DS18B20 H) contents of documents;
Add it to the project after saving;

Connect DS18B20 C and DS18B20 H file add project


Add first c Documents

Then add h file path

Function realization

In main C Li

Including DS18B20 h

/* USER CODE BEGIN Includes */
#include "ds18b20.h"
/* USER CODE END Includes */

Determine whether the hardware circuit contains sensors

/* USER CODE BEGIN 2 */
  while(DS18B20_Init()){
  	printf("DS18B20 checked failed!!!\r\n");
	  HAL_Delay(500);
  }
    printf("DS18B20 checked success!!!\r\n");
  /* USER CODE END 2 */

In the main cycle, send the temperature value to the serial port every 500ms

 while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		temperature = DS18B20_Get_Temp();
		if(temperature < 0)
			printf("Now the temperature is -%d ℃\r\n",temperature/10);
		else
			printf("Now the temperature is %d ℃\r\n",temperature/10);
		HAL_Delay(500);


  }
  /* USER CODE END 3 */

Compile and verify

Keywords: Single-Chip Microcomputer stm32 ARM STM32CUBEMX

Added by chris270 on Thu, 06 Jan 2022 03:53:12 +0200