1399 views|9 replies

6818

Posts

11

Resources
The OP
 

Temperature acquisition of STM32C031 [Copy link]

stm32C031 is used for temperature acquisition, this time sht20 is used. First, the sht20 driver code is:

#include "sht20.h"
#include "main.h"
#include <stdio.h>

unsigned char SHT20_SoftReset(void)
{
	uint8_t buffer =SHT20_SOFT_RESET;
	if(HAL_OK==HAL_I2C_Master_Transmit(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
	{
		printf("成功\r\n");
		return 0;
	}
	else
	{		
		printf("失败\r\n");
		return 1;
	}
}

unsigned char SHT20_Read_Reg(void)
{
	uint8_t buffer =SHT20_READ_REG;
	if(HAL_OK==HAL_I2C_Master_Transmit(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
	{
		if(HAL_OK==HAL_I2C_Master_Receive(&hi2c1, SHT20_Write_Add, &buffer, 1, 1000))
			{
				return buffer;
			}
			else
				{		
					return 1;
				}
	}
	else
	{		
		return 1;
	}

}
unsigned char SHT20_Config(void)//采集温度为14位,采集湿度为12位
{
	uint8_t val;
	val =	(SHT20_Read_Reg()&0x7E)|0x00;
	if(HAL_OK==HAL_I2C_Mem_Write(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_WRITE_REG, (uint16_t)I2C_MEMADD_SIZE_8BIT, &val, 1, 1000))
	{
		return 0;
	}
	else
	{
		return 1;
	}
}


void SHT20_Init()
{
	if(SHT20_SoftReset())
	{
		printf("SHT20 复位失败\r\n");
	}
	else
	{
		printf("SHT20 复位成功\r\n");
	}
	HAL_Delay(100);
	if(SHT20_Config())
	{
		printf("初始化失败\r\n");
	}
	else
	{
		printf("初始化成功\r\n");
	}
}

float SHT20_GetTemperature(void)
{
	uint8_t val[3];
	if(HAL_OK==HAL_I2C_Mem_Read(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_Measurement_T_HM, (uint16_t)I2C_MEMADD_SIZE_8BIT, val, 3, 1000))
	{
		return (-46.85 + (175.72 * (((val[0]<<8)|(val[1]&0xFC))) / 65536.0));
	}
	else
	{
		printf("error\r\n");
	}
	return -1;
}

float SHT20_GetHumidity(void)
{
	uint8_t val[3];
	if(HAL_OK==HAL_I2C_Mem_Read(&hi2c1, (uint16_t)SHT20_Write_Add, (uint16_t)SHT20_Measurement_RH_HM, (uint16_t)I2C_MEMADD_SIZE_8BIT, val, 3, 1000))
	{
		return -6+125*(((val[0]<<8)|(val[1]&0xF0))/65536.0);
	}
	else
	{
		printf("error\r\n");
	}
	return -1;
}

This post is from stm32/stm8

Latest reply

Can the temperature acquisition of STM32C031 be called in other languages?  Details Published on 2023-4-11 16:21
 

6818

Posts

11

Resources
2
 

sht20 header file:

#ifndef __SHT20_H
#define __SHT20_H

#include "main.h"

/*SHT20 */
#define SHT20_ADDRESS  0X40
#define SHT20_Write_Add 0x80   
#define SHT20_Read_Add 0x81
#define SHT20_Measurement_RH_HM  0XE5
#define SHT20_Measurement_T_HM  0XE3
#define SHT20_Measurement_RH_NHM  0XF5  
#define SHT20_Measurement_T_NHM  0XF3   
#define SHT20_READ_REG  0XE7
#define SHT20_WRITE_REG  0XE6
#define SHT20_SOFT_RESET  0XFE

extern I2C_HandleTypeDef hi2c1;
void SHT20_Init(void);
unsigned char SHT20_SoftReset(void);
unsigned char SHT20_Read_Reg(void);
float SHT20_GetTemperature(void);
float SHT20_GetHumidity(void);




#endif

This post is from stm32/stm8
 
 

5998

Posts

6

Resources
3
 

Is there a temperature sensor inside the chip?

This post is from stm32/stm8

Comments

I haven't paid attention to this. The temperature inside the chip can only reflect the condition of the chip, which is quite different from the environment.  Details Published on 2023-4-10 10:14
I haven't paid attention to this. The temperature inside the chip can only reflect the condition of the chip, which is quite different from the environment.  Details Published on 2023-4-10 09:00
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 

6818

Posts

11

Resources
4
 
Qintianqintian0303 posted on 2023-4-10 08:24 Is there a temperature collection function in the film?

I haven't paid attention to this. The temperature inside the chip can only reflect the condition of the chip, which is quite different from the environment.

This post is from stm32/stm8
 
 
 

623

Posts

0

Resources
5
 
Qintianqintian0303 posted on 2023-4-10 08:24 Is there a temperature collection function in the film?

This temperature sampling is different from the temperature sampling we usually talk about.

It is indeed easy to be confused just looking at the datasheet

This post is from stm32/stm8
 
 
 

6062

Posts

4

Resources
6
 

SHT2X 3X 4X is still good if you have a high temperature requirement and want to use a digital temperature chip.

This post is from stm32/stm8
 
 
 

2

Posts

0

Resources
7
 

I am learning 32 now, and I am confused when I see these. I will come back and try it after I learn it. Thanks for sharing, the word count should be enough.

This post is from stm32/stm8

Comments

Yes, you can get points if you reply with more than 20 words.  Details Published on 2023-4-11 07:43
 
 
 

6818

Posts

11

Resources
8
 
Can I come to your house to play? Posted on 2023-4-10 17:00 I am learning 32, and I am confused when I see these. I will come back and try it after I learn it. Thanks for sharing, the word count should be enough

Yes, you can get points if you reply with more than 20 words.

This post is from stm32/stm8
 
 
 

2

Posts

0

Resources
9
 
Can the temperature acquisition of STM32C031 be called in other languages?
This post is from stm32/stm8

Comments

Yes, you can use C++ or Python. You need a corresponding compiler.  Details Published on 2023-4-11 16:31
 
 
 

6818

Posts

11

Resources
10
 
Published this afternoon at 2023-4-11 16:21 Can the temperature acquisition of STM32C031 be called in other languages?

Yes, you can use C++ or Python. You need a corresponding compiler.

This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list