佚名通过本文主要向大家介绍了温湿度变送器sht11,sht11温湿度传感器,sht11湿度传感器,sht11,sht11中文资料等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题:自己写了温湿度SHT11的协议但测量出的值错误,不知道哪里错了。
描述:
#ifndef __I2C_H__
#define __I2C_H__
#include "config.h"
//P6.2 SCL
//P6.3 SDA
#define SDA BIT3
#define SCL BIT2
#define SDA_1 P6OUT |= SDA //SDA = 1
#define SDA_0 P6OUT &=~ SDA //SDA = 0
#define SCL_1 P6OUT |= SCL //SCL = 1
#define SCL_0 P6OUT &=~ SCL //SCL = 0
#define SDA_IN P6DIR &=~ SDA; //I/O口为输入
#define SDA_OUT P6DIR |= SDA //I/0口为输出
#define SDA_VAL ((P6IN >> 3) & 0x01) //Read SDA
//command r/w
#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
#define bitselect 0x01 //选择温度与湿度的低位读
#define noACK 0
#define ACK 1
#define HUMIDITY 2
#define TEMPERATURE 1
void SHT11_Init(void);
void SHT11_start(void);
bool SHT11_writebyte(uint8 value);
uint8 SHT11_readebyte(bool ack);
void SHT11_connectreset(void);
bool SHT11_softreset(void);
bool SHT11_wstareg(uint8 *p_value);
bool SHT11_measure(uint8 *p_value, uint8 *p_checksum,bool mode);
float SHT11_calculate(bool mode);
#endif
#include "config.h"
float TEMP,RH;
/*********************************************************
函数名称:SHT11_Init
函数功能:初始化SHT端口
入口参数:无
出口参数:无
备注:
**********************************************************/
void SHT11_Init()
{
P6SEL&=~(SCL+SDA); //选择P6.2 P6.2 为普通IO端口,输出 P6.3输入
P6DIR|=SCL;
SDA_IN;
}
/*********************************************************
函数名称:SHT11_start
函数功能:发送开始时序
入口参数:无
出口参数:无
备注:
**********************************************************/
void SHT11_start()
{
SDA_OUT;
SDA_1;
SCL_0;
delay_us(1);
SCL_1;
delay_us(1);
SDA_0;
delay_us(1);
SCL_0;
delay_us(1);delay_us(1);delay_us(1);
SCL_1;
delay_us(1);
SDA_1;
delay_us(1);
SCL_0;
SDA_IN;
}
/*********************************************************
函数名称:SHT11_writebyte
函数功能:向SHT11发送一个字节数据
入口参数:value
出口参数:0:SHT11有应答信号 1:SHT11无应答信号
备注:
**********************************************************/
bool SHT11_writebyte(uint8 value)
{
uint8 i,error=0;
SDA_OUT;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
if(i&value)
SDA_1; //masking value with i , write to SENSI-BUS
else
SDA_0;
SCL_1; //clk for SENSI-BUS
delay_us(1);delay_us(1);delay_us(1); //pulswith approx. 5 us
SCL_0;
}
SDA_1; //release DATA-line
SDA_IN; //Change SDA to be input
SCL_1; //clk #9 for ack
error=SDA_VAL; //check ack (DATA will be pulled down by SHT11)
SDA_OUT;
SCL_0;
if(error) return 1; //error=1 in case of no acknowledge
return 0;
}
/*********************************************************
函数名称:SHT11_readebyte
函数功能:从SHT11读取一个字节数据
入口参数:ack 1:给SHT11一个应答,继续通信 0:不再通信
出口参数:val
备注:
**********************************************************/
uint8 SHT11_readebyte(bool ack)
{
uint8 i,val=0;
SDA_OUT;
SDA_1; //release DATA-line
SDA_IN;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
SCL_1; //clk for SENSI-BUS
if(SDA_VAL) val=(val|i); //read bit
SCL_0;
}
SDA_OUT;
if(ack) &n
描述:
c语言函数传感器数据代码
代码如下:#ifndef __I2C_H__
#define __I2C_H__
#include "config.h"
//P6.2 SCL
//P6.3 SDA
#define SDA BIT3
#define SCL BIT2
#define SDA_1 P6OUT |= SDA //SDA = 1
#define SDA_0 P6OUT &=~ SDA //SDA = 0
#define SCL_1 P6OUT |= SCL //SCL = 1
#define SCL_0 P6OUT &=~ SCL //SCL = 0
#define SDA_IN P6DIR &=~ SDA; //I/O口为输入
#define SDA_OUT P6DIR |= SDA //I/0口为输出
#define SDA_VAL ((P6IN >> 3) & 0x01) //Read SDA
//command r/w
#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
#define bitselect 0x01 //选择温度与湿度的低位读
#define noACK 0
#define ACK 1
#define HUMIDITY 2
#define TEMPERATURE 1
void SHT11_Init(void);
void SHT11_start(void);
bool SHT11_writebyte(uint8 value);
uint8 SHT11_readebyte(bool ack);
void SHT11_connectreset(void);
bool SHT11_softreset(void);
bool SHT11_wstareg(uint8 *p_value);
bool SHT11_measure(uint8 *p_value, uint8 *p_checksum,bool mode);
float SHT11_calculate(bool mode);
#endif
#include "config.h"
float TEMP,RH;
/*********************************************************
函数名称:SHT11_Init
函数功能:初始化SHT端口
入口参数:无
出口参数:无
备注:
**********************************************************/
void SHT11_Init()
{
P6SEL&=~(SCL+SDA); //选择P6.2 P6.2 为普通IO端口,输出 P6.3输入
P6DIR|=SCL;
SDA_IN;
}
/*********************************************************
函数名称:SHT11_start
函数功能:发送开始时序
入口参数:无
出口参数:无
备注:
**********************************************************/
void SHT11_start()
{
SDA_OUT;
SDA_1;
SCL_0;
delay_us(1);
SCL_1;
delay_us(1);
SDA_0;
delay_us(1);
SCL_0;
delay_us(1);delay_us(1);delay_us(1);
SCL_1;
delay_us(1);
SDA_1;
delay_us(1);
SCL_0;
SDA_IN;
}
/*********************************************************
函数名称:SHT11_writebyte
函数功能:向SHT11发送一个字节数据
入口参数:value
出口参数:0:SHT11有应答信号 1:SHT11无应答信号
备注:
**********************************************************/
bool SHT11_writebyte(uint8 value)
{
uint8 i,error=0;
SDA_OUT;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
if(i&value)
SDA_1; //masking value with i , write to SENSI-BUS
else
SDA_0;
SCL_1; //clk for SENSI-BUS
delay_us(1);delay_us(1);delay_us(1); //pulswith approx. 5 us
SCL_0;
}
SDA_1; //release DATA-line
SDA_IN; //Change SDA to be input
SCL_1; //clk #9 for ack
error=SDA_VAL; //check ack (DATA will be pulled down by SHT11)
SDA_OUT;
SCL_0;
if(error) return 1; //error=1 in case of no acknowledge
return 0;
}
/*********************************************************
函数名称:SHT11_readebyte
函数功能:从SHT11读取一个字节数据
入口参数:ack 1:给SHT11一个应答,继续通信 0:不再通信
出口参数:val
备注:
**********************************************************/
uint8 SHT11_readebyte(bool ack)
{
uint8 i,val=0;
SDA_OUT;
SDA_1; //release DATA-line
SDA_IN;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
SCL_1; //clk for SENSI-BUS
if(SDA_VAL) val=(val|i); //read bit
SCL_0;
}
SDA_OUT;
if(ack) &n