当前位置:网站首页>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;}边栏推荐
- 指针与数组在函数中输入实现逆序输出
- 未婚夫捐5亿美元给女PI,让她不用申请项目,招150位科学家,安心做科研!
- A simple and beautiful regression table is produced in one line of code~
- A row of code r shows the table of Cox regression model
- JS also exports Excel
- How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
- Advertising attribution: how to measure the value of buying volume?
- Jetson nano configures pytorch deep learning environment / / to be improved
- 每人每年最高500万经费!选人不选项目,专注基础科研,科学家主导腾讯出资的「新基石」启动申报
- DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
猜你喜欢

DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)

How to package the parsed Excel data into objects and write this object set into the database?

Analyse approfondie de kubebuilder

Intel David tuhy: the reason for the success of Intel aoten Technology

What if win11 pictures cannot be opened? Repair method of win11 unable to open pictures

JDBC link Oracle reference code

R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators

System framework of PureMVC

Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str

Monitoring cannot be started after Oracle modifies the computer name
随机推荐
Intel David tuhy: the reason for the success of Intel aoten Technology
Tree map: tree view - draw covid-19 array diagram
Vscode automatically adds a semicolon and jumps to the next line
Tiktok may launch an independent grass planting community platform: will it become the second little red book
DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
两个div在同一行,两个div不换行「建议收藏」
STM32F103实现IAP在线升级应用程序
Markdown编辑器
食堂用户菜品关系系统(C语言课设)
Thread和Runnable创建线程的方式对比
Advertising attribution: how to measure the value of buying volume?
Oracle -- 视图与序列
Section 1: (3) logic chip process substrate selection
为什么很多人对技术债务产生误解
ACL2022 | 分解的元学习小样本命名实体识别
JS variable
Basic idea of counting and sorting
A picture to understand! Why did the school teach you coding but still not
JS variable plus