拜托各位大神了,各位有用过的吗,肯定大神们帮忙啊,卡在这里很久很久了。
//******************************************************************************
//******************************************************************************
//SHT75程序段
char Error; //用于SHT75应答信号错误标志
char TH_Array[3]; //存放温度湿度值及CRC-8 Checksum calculation
#define Measure_TEMP 0x03 //温度
#define Measure_HUMI 0x05 //湿度
//SCK连接至P6.2脚,SCK方向一直为输出
#define SHT_SCK_DIR_OUT P6DIR |=BIT2
#define SHT_SCK_1 P6OUT |=BIT2
#define SHT_SCK_0 P6OUT &=~BIT2
//DATA连接至P6.0脚,DATA有输出也有输入
#define SHT_DATA_DIR_OUT P6DIR |=BIT0
#define SHT_DATA_1 P6OUT |=BIT0
#define SHT_DATA_0 P6OUT &=~BIT0
#define SHT_DATA_DIR_IN P6DIR &=~BIT0
#define SHT_DATA_IN P6IN&BIT0
/*******************************************
函数名称:Start()
功 能:Transmission Start sequence
参 数: 无
返回值 :无
*******************************************/
void Start()
{
SHT_SCK_DIR_OUT;
SHT_DATA_DIR_OUT;
SHT_DATA_1;
SHT_SCK_0;
_NOP();
SHT_SCK_1;
_NOP();
SHT_DATA_0;
_NOP();
SHT_SCK_0;
_NOP();
_NOP();
_NOP();
SHT_SCK_1;
_NOP();
SHT_DATA_1;
_NOP();
SHT_SCK_0;
}
/*******************************************
函数名称:Write_SHT75(char Value)
功 能:写一字节,用于写控制命令
参 数: Value,0x03:温度测试,0x05:湿度测试
返回值 :无
*******************************************/
void Write_SHT75(char Value)
{
unsigned char i;
SHT_DATA_DIR_OUT;
for(i = 0x80;i>0;i/=2)
{
if (i&Value)
{
SHT_DATA_1;
}
else
{
SHT_DATA_0;
}
_NOP();
SHT_SCK_1;
_NOP();
_NOP();
_NOP();
SHT_SCK_0;
}
SHT_DATA_1;
_NOP();
SHT_SCK_1;
SHT_DATA_DIR_IN;
Error=SHT_DATA_IN;//读取ack值,为0正常,读1报错。
SHT_SCK_0;
}
/*******************************************
函数名称:Read_SHT75()
功 能:读一字节用于读取12位的湿度值和14位的温度值
参 数: ack,1应答CRC-8 checksum,0应答正常的温度湿度读取
返回值 :无
*******************************************/
char Read_SHT75(char ack)
{
unsigned char i,val = 0;
SHT_DATA_DIR_OUT;
SHT_DATA_1;
_NOP();
SHT_DATA_DIR_IN;
_NOP();
for (i = 0x80;i>0;i/=2)
{
SHT_SCK_1;
if(SHT_DATA_IN)
{
val=(val|i);
}
SHT_SCK_0;
}
_NOP();
SHT_DATA_DIR_OUT;
if(ack)
{
SHT_DATA_1;
}
else
{
SHT_DATA_0;
}
_NOP();
SHT_SCK_1;
_NOP();
_NOP();
_NOP();
SHT_SCK_0;
SHT_DATA_1;
return val;
}
/*******************************************
函数名称:Measure_SHT75(char Mode)
功 能:温度,湿度值读取
参 数:Mode ,0x00:温度测试,0x01:湿度测试
返回值 :None
*******************************************/
void Measure_SHT75(char Mode)
{
long int Wait_i;
Start();
switch(Mode)
{
case 0x00:Write_SHT75(Measure_TEMP);break;
case 0x01:Write_SHT75(Measure_HUMI);break;
default: break;
}
for (Wait_i = 0;Wait_i < 3638000;Wait_i++)
{
if(SHT_DATA_IN==0)
{
break;
}
}
TH_Array[0]=Read_SHT75(0);
TH_Array[1]=Read_SHT75(0);
TH_Array[2]=Read_SHT75(1);
}
此帖出自
小平头技术问答
#define DHT11_OUTPUT P2DIR|=BIT0
#define DHT11_INPUT P2DIR&=~BIT0
#define DHT11_H P2OUT|=BIT0
#define DHT11_L P2OUT&=~BIT0
#define DHT11_IN (P2IN&BIT0)
- uchar start_DHT11(void)
- {
- uchar TData_H_temp,TData_L_temp,RHData_H_temp,RHData_L_temp,checktemp;
- uchar presence=0,flag=0;
- uint count;
- DHT11_OUTPUT;
- DHT11_L;
- delay_ms(20); //拉低18ms以上
- DHT11_H;
- delay_us(40);
- DHT11_INPUT;
- presence=DHT11_IN;
- if(!presence)
- {
- count=2;
- while((DHT11_IN)&&count++);//等待高电平
- RHData_H_temp = DHT11_ReadChar();
- RHData_L_temp = DHT11_ReadChar();
- TData_H_temp = DHT11_ReadChar();
- TData_L_temp = DHT11_ReadChar();
- CheckData_temp = DHT11_ReadChar();
- DHT11_OUTPUT;
- DHT11_H;
- checktemp = (RHData_H_temp + RHData_L_temp + TData_H_temp + TData_L_temp);
- if (checktemp == CheckData_temp) {
- DHT11RH_Data_H = RHData_H_temp;
- DHT11RH_Data_L = RHData_L_temp;
- DHT11T_Data_H = TData_H_temp;
- DHT11T_Data_L = TData_L_temp;
- flag=1;
- }
- }
- return flag;
- }
复制代码一周热门 更多>