第一次发很紧张如果有错请及时联系我进行修改
不知道说啥好直接上代码叭!
#include “led.h”
#include “delay.h”
#include “sys.h”
#include “usart.h”
#include “lcd.h”
#include “key.h”
#include “24cxx.h”
#include “myiic.h”
#include “SHT2x.h”
char Str_Buf[32];
int main(void)
{
float temp,Humi;
u16 DS18B20Temp;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
delay_init(); //延时函数初始化
uart_init(9600); //串口初始化为9600
LED_Init(); //初始化与LED连接的硬件接口
LCD_Init(); //LCD初始化
SHT2x_Init(); //SHT2x初始化
POINT_COLOR=RED; //设置字体为红色
LCD_ShowString(60,50,200,16,16,“Mini V3.0 STM32”);
LCD_ShowString(60,70,200,16,16,“SHT20-TEST”);
DS18B20_Init(); //DS18B20初始化
POINT_COLOR=BLUE; //设置字体为蓝色
while(1)
{
DS18B20Temp=DS18B20_Get_Temp();//获取DS18B20温度
temp=SHT2x_GetTempPoll();//获取SHT20 温度
Humi=SHT2x_GetHumiPoll();//获取SHT20 湿度
sprintf(Str_Buf,"SHT20 Temp:%6.2f C",temp);
LCD_ShowString(60,100,200,16,16,(u8 *)Str_Buf);
sprintf(Str_Buf,"SHT20 Humi:%6.2f %%",Humi);
LCD_ShowString(60,120,200,16,16,(u8 *)Str_Buf);
sprintf(Str_Buf,"DS18B20 Temp:%6.2f %%",(float)DS18B20Temp/10.0);
LCD_ShowString(60,140,200,16,16,(u8 *)Str_Buf);
delay_ms(250);
LED0=!LED0;//提示系统正在运行
}
}
#include “SHT2x.h”
#include “delay.h”
#include “myiic.h”
#include
SHT2x_data SHT20;
u8 SHT2x_Init(void)
{
u8 err;
IIC_Init();
err = SHT2x_SoftReset();
return err;
}
u8 SHT2x_SoftReset(void) //SHT20软件复位
{
u8 err=0;
IIC_Start();
IIC_Send_Byte(0x80);
err = IIC_Wait_Ack();
IIC_Send_Byte(0xFE);
err = IIC_Wait_Ack();
IIC_Stop();
return err;
}
float SHT2x_GetTempPoll(void)
{
float TEMP;
u8 ack, tmp1, tmp2;
u16 ST;
u16 i=0;
IIC_Start(); //发送IIC开始信号
IIC_Send_Byte(I2C_ADR_W); //IIC发送一个字节
ack = IIC_Wait_Ack();
IIC_Send_Byte(TRIG_TEMP_MEASUREMENT_POLL);
ack = IIC_Wait_Ack();
do {
delay_ms(100);
IIC_Start(); //发送IIC开始信号
IIC_Send_Byte(I2C_ADR_R);
i++;
ack = IIC_Wait_Ack();
if(i==1000)break;
} while(ack!=0);
tmp1 = IIC_Read_Byte(1);
tmp2 = IIC_Read_Byte(1);
IIC_Read_Byte(0);
IIC_Stop();
ST = (tmp1 << 8) | (tmp2 << 0);
ST &= ~0x0003;
TEMP = ((float)ST * 0.00268127) - 46.85;
return (TEMP);
}
float SHT2x_GetHumiPoll(void)
{
float HUMI;
u8 ack, tmp1, tmp2;
u16 SRH;
u16 i=0;
IIC_Start(); //发送IIC开始信号
IIC_Send_Byte(I2C_ADR_W); //IIC发送一个字节
ack = IIC_Wait_Ack();
IIC_Send_Byte(TRIG_HUMI_MEASUREMENT_POLL);
ack = IIC_Wait_Ack();
do {
delay_ms(100);
IIC_Start(); //发送IIC开始信号
IIC_Send_Byte(I2C_ADR_R);
i++;
ack = IIC_Wait_Ack();
if(i==100)break;
} while(ack!=0);
tmp1 = IIC_Read_Byte(1);
tmp2 = IIC_Read_Byte(1);
IIC_Read_Byte(0);
IIC_Stop();
SRH = (tmp1 << 8) | (tmp2 << 0);
SRH &= ~0x0003;
HUMI = ((float)SRH * 0.00190735) - 6;
return (HUMI);
}
#ifndef SHT2x_H
#define SHT2x_H
#include “sys.h”
#ifdef __cplusplus
extern “C” {
#endif
#define I2C_ADR_W 0x80 //SHT2X IIC 读地址
#define I2C_ADR_R 0x81 //SHT2X IIC 写地址
typedef enum {
TRIG_TEMP_MEASUREMENT_HM = 0xE3, // 触发 温度 测量 ,保持主机 命令
TRIG_HUMI_MEASUREMENT_HM = 0xE5, // 触发 湿度 测量 ,保持主机 命令
TRIG_TEMP_MEASUREMENT_POLL = 0xF3, // 触发 温度 测量 ,非保持主机 命令
TRIG_HUMI_MEASUREMENT_POLL = 0xF5, // 触发 湿度 测量 ,非保持主机 命令
USER_REG_W = 0xE6, // 写寄存器命令
USER_REG_R = 0xE7, // 读寄存器命令
SOFT_RESET = 0xFE // 软件复位命令
} SHT2xCommand;
typedef enum {
SHT2x_RES_12_14BIT = 0x00,
SHT2x_RES_8_12BIT = 0x01, //RH= 8bit, T=12bit
SHT2x_RES_10_13BIT = 0x80, //RH=10bit, T=13bit
SHT2x_RES_11_11BIT = 0x81, //RH=11bit, T=11bit
SHT2x_RES_MASK = 0x81 //Mask for res. bits (7,0) in user reg.
} SHT2xResolution;
typedef enum {
SHT2x_HEATER_ON = 0x04, //heater on
SHT2x_HEATER_OFF = 0x00, //heater off
SHT2x_HEATER_MASK = 0x04 //Mask for Heater bit(2) in user reg.
} SHT2xHeater;
typedef struct{
float TEMP_POLL;
float HUMI_POLL;
} SHT2x_data;
extern SHT2x_data SHT20;
u8 SHT2x_Init(void);
u8 SHT2x_SoftReset(void);
float SHT2x_GetTempPoll(void);
float SHT2x_GetHumiPoll(void);
#ifdef __cplusplus
}
#endif
#endif