标题:
温湿度传感SHT3x源程序 STM32F100RB芯片
[打印本页]
作者:
BaBaTu
时间:
2020-3-25 09:12
标题:
温湿度传感SHT3x源程序 STM32F100RB芯片
温湿度传感SHT3x
单片机源程序如下:
//-- Includes -----------------------------------------------------------------
#include "system.h"
#include "sht3x.h"
//-- Static function prototypes -----------------------------------------------
static void EvalBoardPower_Init(void);
static void Led_Init(void);
static void UserButton_Init(void);
static void LedBlueOn(void);
static void LedBlueOff(void);
static void LedGreenOn(void);
static void LedGreenOff(void);
static u8t ReadUserButton(void);
//-----------------------------------------------------------------------------
int main(void)
{
etError error; // error code
u32t serialNumber;// serial number
regStatus status; // sensor status
ft temperature; // temperature [癈]
ft humidity; // relative humidity [%RH]
bt heater; // heater, false: off, true: on
SystemInit();
Led_Init();
UserButton_Init();
EvalBoardPower_Init();
SHT3X_Init(0x45); // Address: 0x44 = Sensor on EvalBoard connector
// 0x45 = Sensor on EvalBoard
// wait 50ms after power on
DelayMicroSeconds(50000);
error = SHT3x_ReadSerialNumber(&serialNumber);
if(error != NO_ERROR){} // do error handling here
// demonstrate a single shot measurement with clock-stretching
error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_CLKSTRETCH, 50);
if(error != NO_ERROR){} // do error handling here
// demonstrate a single shot measurement with polling and 50ms timeout
error = SHT3X_GetTempAndHumi(&temperature, &humidity, REPEATAB_HIGH, MODE_POLLING, 50);
if(error != NO_ERROR){} // do error handling here
// loop forever
while(1)
{
error = NO_ERROR;
// loop while no error
while(error == NO_ERROR)
{
// read status register
error |= SHT3X_ReadStatus(&status.u16);
if(error != NO_ERROR) break;
// check if the reset bit is set after a reset or power-up
if(status.bit.ResetDetected)
{
//override default temperature and humidity alert limits (red LED)
error = SHT3X_SetAlertLimits( 70.0f, 50.0f, // high set: RH [%], T [癈]
68.0f, 48.0f, // high clear: RH [%], T [癈]
32.0f, -2.0f, // low clear: RH [%], T [癈]
30.0f, -4.0f); // low set: RH [%], T [癈]
if(error != NO_ERROR) break;
// clear reset and alert flags
error = SHT3X_ClearAllAlertFlags();
if(error != NO_ERROR) break;
//start periodic measurement, with high repeatability and 1 measurements per second
error = SHT3X_StartPeriodicMeasurment(REPEATAB_HIGH, FREQUENCY_1HZ);
if(error != NO_ERROR) break;
//switch green LED on
LedGreenOn();
}
// read measurment buffer
error = SHT3X_ReadMeasurementBuffer(&temperature, &humidity);
if(error == NO_ERROR)
{
// flash blue LED to signalise new temperature and humidity values
LedBlueOn();
DelayMicroSeconds(10000);
LedBlueOff();
}
else if (error == ACK_ERROR)
{
// there were no new values in the buffer -> ignore this error
error = NO_ERROR;
}
else break;
// read heater status
heater = status.bit.HeaterStatus ? TRUE : FALSE;
// if the user button is not pressed ...
if(ReadUserButton() == 0)
{
// ... and the heater is on
if(heater)
{
// switch off the sensor internal heater
error |= SHT3X_DisableHeater();
if(error != NO_ERROR) break;
}
}
else
// if the user button is pressed ...
{
// ... and the heater is off
if(!heater)
{
// switch on the sensor internal heater
error |= SHT3X_EnableHeater();
if(error != NO_ERROR) break;
}
}
// wait 100ms
DelayMicroSeconds(100000);
}
// in case of an error ...
// ... switch green and blue LED off
LedGreenOff();
LedBlueOff();
// ... try first a soft reset ...
error = SHT3X_SoftReset();
// ... if the soft reset fails, do a hard reset
if(error != NO_ERROR)
{
SHT3X_HardReset();
}
// flash green LED to signalise an error
LedGreenOn();
DelayMicroSeconds(10000);
LedGreenOff();
}
}
//-----------------------------------------------------------------------------
static void EvalBoardPower_Init(void) /* -- adapt this code for your platform -- */
{
RCC->APB2ENR |= 0x00000008; // I/O port B clock enabled
GPIOB->CRH &= 0x0FFF0FFF; // set push-pull output for Vdd & GND pins
GPIOB->CRH |= 0x10001000; //
GPIOB->BSRR = 0x08008000; // set Vdd to High, set GND to Low
}
//-----------------------------------------------------------------------------
static void Led_Init(void) /* -- adapt this code for your platform -- */
{
RCC->APB2ENR |= 0x00000010; // I/O port C clock enabled
GPIOC->CRH &= 0xFFFFFF00; // set general purpose output mode for LEDs
GPIOC->CRH |= 0x00000011; //
GPIOC->BSRR = 0x03000000; // LEDs off
}
//-----------------------------------------------------------------------------
static void UserButton_Init(void) /* -- adapt this code for your platform -- */
{
RCC->APB2ENR |= 0x00000004; // I/O port A clock enabled
GPIOA->CRH &= 0xFFFFFFF0; // set general purpose input mode for User Button
GPIOA->CRH |= 0x00000004; //
}
//-----------------------------------------------------------------------------
static void LedBlueOn(void) /* -- adapt this code for your platform -- */
{
GPIOC->BSRR = 0x00000100;
}
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
温湿度传感官方.zip
(33.84 KB, 下载次数: 31)
2020-3-25 09:12 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1