/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "usart1.h"
#include "stdio.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define SDA_H() GPIO_SetBits(GPIOB,GPIO_Pin_0)
#define SDA_L() GPIO_ResetBits(GPIOB,GPIO_Pin_0)
#define SCK_H() GPIO_SetBits(GPIOB,GPIO_Pin_1)
#define SCK_L() GPIO_ResetBits(GPIOB,GPIO_Pin_1)
//读SDA数据
#define SDA_R() GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)
/* Private macro -------------------------------------------------------------*/
#define noACK 0 //无应答
#define ACK 1 //应答
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
/* Private variables ---------------------------------------------------------*/
enum {TEMP,HUMI};
uint8_t Test_Timetick = 0;
uint16_t Test_Counter = 0;
/* Private function prototypes -----------------------------------------------*/
void RCC_Config(void);
void USART1_Config(void);
void SHT10_Config(void);
void SHT10_SDAIn(void);
void SHT10_SDAOut(void);
void SHT10_Delay(void);
uint8_t SHT10_WriteByte(uint8_t value);
uint8_t SHT10_ReadByte(uint8_t Ack);
void SHT10_Start(void);
void SHT10_ConReset(void);
uint8_t SHT10_Measure(uint16_t* pValue, uint8_t* pCheckSum, uint8_t mode);
void SHT10_Cal(uint16_t Temp,uint16_t Hum, float* pTempValue,float* pHumValue);
/* Private functions ---------------------------------------------------------*/
/**
* @brief 主程序
* @param 无
* @retval 无
*/
int main(void)
{
//温度结果 16bit
uint16_t TempValue;
//湿度结果 16bit
uint16_t HumValue;
//温度转换结果
float TempResult;
//湿度转换结果
float HumResult;
//校验值
uint8_t CheckValue = 0x00;
//错误
uint8_t error = 0x00;
//初始化RCC
RCC_Config();
USART1_Config();
SHT10_Config();
printf("Start\n");
while(1)
{
if(Test_Timetick)
{
Test_Timetick = 0;
Test_Counter ++;
//延时2000ms
if(Test_Counter > 2000)
{
Test_Counter = 0;
printf("启动转换!\n");
//SHT10 连接
SHT10_ConReset();
//获得温度和湿度数据,16位格式
error += SHT10_Measure(&TempValue,&CheckValue,TEMP);
error += SHT10_Measure(&HumValue,&CheckValue,HUMI);
//温度湿度计算,浮点数形式
SHT10_Cal(TempValue ,HumValue,&TempResult,&HumResult);
//通过串口输出,温度和湿度数据
printf("温度 %2.1fC 湿度 %2.1f%%\n",TempResult,HumResult);
}
}
}
}
|