当前位置:网站首页>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;}
边栏推荐
- 当 Knative 遇见 WebAssembly
- Master the secrets of software security testing methods, and pinch the security test report with your hands
- 使用Thread类和Runnable接口实现多线程的区别
- Canteen user dish relationship system (C language course design)
- Section 1: (3) logic chip process substrate selection
- 深入解析Kubebuilder
- Monitoring cannot be started after Oracle modifies the computer name
- Gpt-3 is a peer review online when it has been submitted for its own research
- Kivy tutorial of setting the size and background of the form (tutorial includes source code)
- 关于01背包个人的一些理解
猜你喜欢
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
Section 1: (3) logic chip process substrate selection
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
Win11 control panel shortcut key win11 multiple methods to open the control panel
指针与数组在函数中输入实现逆序输出
Basic idea of counting and sorting
Field data acquisition and edge calculation scheme of CNC machine tools
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
5G VoNR+之IMS Data Channel概念
随机推荐
Up to 5million per person per year! Choose people instead of projects, focus on basic scientific research, and scientists dominate the "new cornerstone" funded by Tencent to start the application
Meow, come, come: do you really know if, if else
Introduction to the PureMVC series
STM32 system timer flashing LED
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Analyse approfondie de kubebuilder
What work items do programmers hate most in their daily work?
全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
Factor analysis r practice (with R installation tutorial and code)
[hand torn STL] list
深入解析Kubebuilder
C语言中函数指针与指针函数
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
Depth first traversal template principle of tree and graph
Win11 control panel shortcut key win11 multiple methods to open the control panel
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
ACL2022 | 分解的元学习小样本命名实体识别
谈谈讲清楚这件事的重要性
一图看懂!为什么学校教了你Coding但还是不会的原因...
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压