当前位置:网站首页>Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
Stm32f103ze+sht30 detection of ambient temperature and humidity (IIC simulation sequence)
2022-07-07 04:55:00 【Hua Weiyun】
One 、 Introduction to the environment
Engineering compilation software : keil5
Temperature and humidity sensor : SHT30
MCU : STM32F103ZET6
The program adopts modular programming ,iic The timing is a module (iic.c and iic.h),SHT30 For a module (sht30.c and sht30.h);IIC The timing is realized by analog timing ,IO Macro definition method is adopted for all interfaces , It is convenient to quickly migrate to other platforms .
Two 、SHT30 Introduce
characteristic :
\1. Humidity and temperature sensors
\2. Full calibration 、 Linearization and temperature
\3. Compensated digital output , Wide power supply voltage range , from 2.4 V To 5.5 V
\4. I2C Interface , The communication speed is up to 1MHz And two user selectable addresses
\5. Typical accuracy ± 2% Relative humidity and ± 0.3°C
\6. Start up and measurement time is very fast
\7. miniature 8 The needle DFN encapsulation
This is a SHT30 Of 7 position IIC Device address :
3、 ... and 、 Equipment operation effect
This is the data printed by serial port :
Four 、 Device code
4.1 main.c
#include "stm32f10x.h"#include "led.h"#include "delay.h"#include "key.h"#include "usart.h"#include "iic.h"#include "sht3x.h"int main(){ float Humidity; float Temperature; USART1_Init(115200); USART1_Printf(" The equipment is running normally ....\r\n"); IIC_Init(); Init_SHT30(); while(1) { // Read the temperature and humidity SHT3x_ReadData(&Humidity,&Temperature); USART1_Printf(" temperature :%0.2f, humidity :%0.2f\r\n",Temperature,Humidity); delay_ms(1000); }}
4.2 sht30.c
#include "sht3x.h"const int16_t POLYNOMIAL = 0x131;/**************************************************************** The name of the function : SHT30_reset* say bright : SHT30 Reset * ginseng Count : nothing * return return value : nothing ***************************************************************/void SHT30_reset(void){ u8 r_s; IIC_Start(); // Send the start signal IIC_WriteOneByteData(SHT30_AddrW); // Write the device address r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:1\r\n"); IIC_WriteOneByteData(0x30); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:2\r\n"); IIC_WriteOneByteData(0xA2); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:3\r\n"); IIC_Stop(); // Stop signal delay_ms(50); }/**************************************************************** The name of the function : Init_SHT30* say bright : initialization SHT30, Set the measurement cycle * ginseng Count : nothing * return return value : nothing ***************************************************************/void Init_SHT30(void){ u8 r_s; IIC_Start(); // Send the start signal IIC_WriteOneByteData(SHT30_AddrW); // Write the device address r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:1\r\n"); IIC_WriteOneByteData(0x22); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:2\r\n"); IIC_WriteOneByteData(0x36); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("Init_SHT30_error:3\r\n"); IIC_Stop(); // Stop signal delay_ms(200);}/**************************************************************** The name of the function : SHT3x_CheckCrc* say bright : Check the correctness of the data * ginseng Count : data: Data read nbrOfBytes: Quantity to be verified checksum: The read calibration comparison value * return return value : Verification results ,0- success 1- Failure ***************************************************************/u8 SHT3x_CheckCrc(char data[], char nbrOfBytes, char checksum){ char crc = 0xFF; char bit = 0; char byteCtr ; //calculates 8-Bit checksum with given polynomial for(byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) { crc ^= (data[byteCtr]); for ( bit = 8; bit > 0; --bit) { if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL; else crc = (crc << 1); } } if(crc != checksum) return 1; else return 0; }/**************************************************************** The name of the function : SHT3x_CalcTemperatureC* say bright : Temperature calculation * ginseng Count : u16sT: Read the raw temperature data * return return value : Calculated temperature data ***************************************************************/float SHT3x_CalcTemperatureC(unsigned short u16sT){ float temperatureC = 0; // variable for result u16sT &= ~0x0003; // clear bits [1..0] (status bits) //-- calculate temperature [℃] -- temperatureC = (175 * (float)u16sT / 65535 - 45); //T = -45 + 175 * rawValue / (2^16-1) return temperatureC; }/**************************************************************** The name of the function : SHT3x_CalcRH* say bright : Humidity calculation * ginseng Count : u16sRH: Read humidity raw data * return return value : Calculated humidity data ***************************************************************/float SHT3x_CalcRH(unsigned short u16sRH){ float humidityRH = 0; // variable for result u16sRH &= ~0x0003; // clear bits [1..0] (status bits) //-- calculate relative humidity [%RH] -- humidityRH = (100 * (float)u16sRH / 65535); // RH = rawValue / (2^16-1) * 10 return humidityRH; }// Read temperature and humidity data void SHT3x_ReadData(float *Humidity,float *Temperature){ char data[3]; //data array for checksum verification u8 SHT3X_Data_Buffer[6]; //byte 0,1 is temperature byte 4,5 is humidity u8 r_s=0; u8 i; unsigned short tmp = 0; uint16_t dat; IIC_Start(); // Send the start signal IIC_WriteOneByteData(SHT30_AddrW); // Write the device address r_s=IIC_GetACK();// Get answers if(r_s)printf("SHT3X_error:1\r\n"); IIC_WriteOneByteData(0xE0); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("SHT3X_error:2\r\n"); IIC_WriteOneByteData(0x00); // Writing data r_s=IIC_GetACK();// Get answers if(r_s)printf("SHT3X_error:3\r\n"); //IIC_Stop(); // Stop signal // DelayMs(30); // wait for // Read sht30 Sensor data IIC_Start(); // Send the start signal IIC_WriteOneByteData(SHT30_AddrR); r_s=IIC_GetACK();// Get answers if(r_s)printf("SHT3X_error:7\r\n"); for(i=0;i<6;i++) { SHT3X_Data_Buffer[i]=IIC_ReadOneByteData(); // receive data if(i==5) { IIC_SendACK(1); // Send non reply signal break; } IIC_SendACK(0); // Send reply signal } IIC_Stop(); // Stop signal // /* check tem */ data[0] = SHT3X_Data_Buffer[0]; data[1] = SHT3X_Data_Buffer[1]; data[2] = SHT3X_Data_Buffer[2]; tmp=SHT3x_CheckCrc(data, 2, data[2]); if( !tmp ) /* value is ture */ { dat = ((uint16_t)data[0] << 8) | data[1]; *Temperature = SHT3x_CalcTemperatureC( dat ); } // /* check humidity */ data[0] = SHT3X_Data_Buffer[3]; data[1] = SHT3X_Data_Buffer[4]; data[2] = SHT3X_Data_Buffer[5]; tmp=SHT3x_CheckCrc(data, 2, data[2]); if( !tmp ) /* value is ture */ { dat = ((uint16_t)data[0] << 8) | data[1]; *Humidity = SHT3x_CalcRH( dat ); }}
4.3 iic.c
#include "iic.h"/* The functionality :IIC Interface initialization hardware connection :SDA:PB7SCL:PB6*/void IIC_Init(void){ RCC->APB2ENR|=1<<3;//PB GPIOB->CRL&=0x00FFFFFF; GPIOB->CRL|=0x33000000; GPIOB->ODR|=0x3<<6;}/* The functionality :IIC Bus start signal */void IIC_Start(void){ IIC_SDA_OUTMODE(); // initialization SDA For output mode IIC_SDA_OUT=1; // The data cable is pulled high IIC_SCL=1; // Pull the clock cable high DelayUs(4); // Level holding time IIC_SDA_OUT=0; // Pull down the data cable DelayUs(4); // Level holding time IIC_SCL=0; // Pull down the clock line }/* The functionality :IIC Bus stop signal */void IIC_Stop(void){ IIC_SDA_OUTMODE(); // initialization SDA For output mode IIC_SDA_OUT=0; // Pull down the data cable IIC_SCL=0; // Pull down the clock line DelayUs(4); // Level holding time IIC_SCL=1; // Pull the clock cable high DelayUs(4); // Level holding time IIC_SDA_OUT=1; // The data cable is pulled high }/* The functionality : Get the reply signal and return return value :1 It means failure ,0 It means success */u8 IIC_GetACK(void){ u8 cnt=0; IIC_SDA_INPUTMODE();// initialization SDA Is the input mode IIC_SDA_OUT=1; // Data online pull DelayUs(2); // Level holding time IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs data DelayUs(2); // Level holding time , Wait for the slave to send data IIC_SCL=1; // Pull the clock cable high , Tell the slave , The host now starts reading data while(IIC_SDA_IN) // Waiting for the slave to respond { cnt++; if(cnt>250)return 1; } IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs data return 0;}/* The functionality : The master sends the response signal function parameter to the slave :0 To answer ,1 It means no response */void IIC_SendACK(u8 stat){ IIC_SDA_OUTMODE(); // initialization SDA For output mode IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs to send data if(stat)IIC_SDA_OUT=1; // The data cable is pulled high , Send non reply signal else IIC_SDA_OUT=0; // Pull down the data cable , Send reply signal DelayUs(2); // Level holding time , Wait for the clock line to stabilize IIC_SCL=1; // Pull the clock cable high , Tell the slave , The host data has been sent DelayUs(2); // Level holding time , Wait for the slave to receive data IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs data }/* The functionality :IIC send out 1 Byte data function parameters : Data to be sent */void IIC_WriteOneByteData(u8 data){ u8 i; IIC_SDA_OUTMODE(); // initialization SDA For output mode IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs to send data for(i=0;i<8;i++) { if(data&0x80)IIC_SDA_OUT=1; // The data cable is pulled high , send out 1 else IIC_SDA_OUT=0; // Pull down the data cable , send out 0 IIC_SCL=1; // Pull the clock cable high , Tell the slave , The host data has been sent DelayUs(2); // Level holding time , Wait for the slave to receive data IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs to send data DelayUs(2); // Level holding time , Wait for the clock line to stabilize data<<=1; // First mover }}/* The functionality :IIC receive 1 Bytes of data are returned return value : Data received */u8 IIC_ReadOneByteData(void){ u8 i,data; IIC_SDA_INPUTMODE();// initialization SDA Is the input mode for(i=0;i<8;i++) { IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs data DelayUs(2); // Level holding time , Wait for the slave to send data IIC_SCL=1; // Pull the clock cable high , Tell the slave , The host is now reading data data<<=1; if(IIC_SDA_IN)data|=0x01; DelayUs(2); // Level holding time , Wait for the clock line to stabilize } IIC_SCL=0; // Pull down the clock line , Tell the slave , The host needs data ( Must be pulled down , Otherwise, it will be recognized as a stop signal ) return data;}
边栏推荐
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- Introduction to namespace Basics
- 【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
- On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
- Time complexity & space complexity
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- 为什么很多人对技术债务产生误解
- STM32 system timer flashing LED
- 5G VoNR+之IMS Data Channel概念
- B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
猜你喜欢
Field data acquisition and edge calculation scheme of CNC machine tools
Win11 control panel shortcut key win11 multiple methods to open the control panel
Tree map: tree view - draw covid-19 array diagram
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
namespace基础介绍
A row of code r shows the table of Cox regression model
offer如何选择该考虑哪些因素
acwing 843. N-queen problem
Flex layout and usage
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
随机推荐
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
The worse the AI performance, the higher the bonus? Doctor of New York University offered a reward for the task of making the big model perform poorly
抖音或将推出独立种草社区平台:会不会成为第二个小红书
架构实战训练营|课后作业|模块 6
sscanf,sscanf_ S and its related usage "suggested collection"
组织实战攻防演练的5个阶段
Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
Appium practice | make the test faster, more stable and more reliable (I): slice test
B站大佬用我的世界搞出卷积神经网络,LeCun转发!爆肝6个月,播放破百万
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Thesis landing strategy | how to get started quickly in academic thesis writing
Lessons and thoughts of the first SQL injection
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
《原动力 x 云原生正发声 降本增效大讲堂》第三讲——Kubernetes 集群利用率提升实践
A line of R code draws the population pyramid
MySQL forgot how to change the password
Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)