⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sht10.c

📁 温湿度变送器程序
💻 C
字号:



#include "cdltwk.h"



bit rht_flag=0;
bit sht_dam_flag=0;             //=1, 温湿度传感器损坏标志
bit shtbit_flag=0;              //=1, 湿度8位分辨率, 温度12位分辨率
                                //=0, 湿度12位分辨率,温度14位分辨率


int out_temp=250;                 //室外温度
int out_rh=0;                   //室外湿度
int wb_rh=0;                    //做过温度补偿的室外湿度

int uint_rht=0;                //从SHT10读出的温湿度整型数
uchar wait_code=0;              //=0, 空闲状态
                                //=1, 下一周期准备启动温度采集
                                //=2, 等待温度转换结束
                                //=3, 下一周期准备启动湿度采集
                                //=4, 等待湿度转换结束
int tempp;
uchar temp[6]={0x20,0x20,0x20,0x20,0x20,0x20};
uchar rh[6]={0x20,0x20,0x20,0x20,0x20,0x20};

void temp_order();
void rh_order();
void sht10_statr();
bit write_sht10(uchar dt);
void rdsht10_state();
void sht10_samp();
void uart_send_byte(uchar udata);
void uart_send1(uchar *send_buf,uchar n);
//---------------------------------------------------------------
//filename : void sht10_statr()
//功    能 : 发送 SHT_10 起始位
//           当SCK 时钟高电平时DATA 翻转为低电平,紧接着SCK变为
//           低电平,随后是在SCK 时钟高电平时DATA 翻转为高电平
//---------------------------------------------------------------
//
void sht10_statr()
{
    sdat=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sclk=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sdat=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sclk=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sclk=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sdat=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    sclk=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
}
//
//---------------------------------------------------------------
//filename : write_sht10(uchar dt)
//功    能 : 向SHT10发送1个字节的数据, 发送是高位在前,低位在后
//           在发送完最低位后等待SHT10的应答信号,如果SHT10正确
//           收到命令,会把数据线拉低
//
//           在CPU发出第8个sclk时钟的下降沿之后,SHT10将sdat下拉
//           为低电平,在CPU发出第9个sclk时钟的下降沿之后,SHT10
//           将释放sdat(恢复高电平)
//
//返回数据 : 0 无应答
//           1 有应答
//---------------------------------------------------------------
//

bit write_sht10(uchar dt)
{
    uchar i;

    for(i=0;i<8;i++){sclk=0; sdat=(bit)(dt&0x80); dt<<= 1; sclk=1;}//发送数据
    sclk=0;_nop_();_nop_();_nop_();_nop_(); sdat=1;_nop_();_nop_();_nop_();
    i=240;
    while(i)         //等待SHT10的应答信号
    {   if(!sdat)    //检测到SHT10的应答信号
        {   sclk=1; _nop_();_nop_();_nop_(); sclk=0;_nop_();_nop_();
            sht_dam_flag=0;  //温湿度传感器正常标志

            return(1);
        }
        --i;
    }
    sht_dam_flag=1;  //温湿度传感器损坏标志
    return(0);       //没有检测到SHT10的应答信号
}

//
//-------------------------------------------------------------------
//filename : read_sht10()
//功    能 : 从SHT10中读出1个字节的数据, 读出顺序是高位在前,低位在后
//           CPU在收到SHT10发出的最低位(LSB)后,通过保持确认位ack高电
//           平,来中止通讯(就是保持sdat为高电平)
//-------------------------------------------------------------------
//
void read_sht10()
{
    uchar i,j;
    for(i=0;i<8;i++)
    {   sclk=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
        j<<=1; j|=sdat;
        sclk=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    }
    sdat=0;  sclk=1;     //发出1个字节的确认信号
    _nop_(); _nop_(); _nop_();
    sclk=0;  sdat=1;
    uint_rht=j;
    uint_rht = uint_rht << 8;

    j = 0;
    for(i=0;i<8;i++)
    {   sclk=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
        j<<=1; j|=sdat;
        sclk=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
    }
    uint_rht+=j;
}
//
//-------------------------------------------------------------------
//filename : rdsht10_state()
//功    能 : 读SHT10状态寄存器,判断SHT10分辨率
//-------------------------------------------------------------------
/*
void rdsht10_state()
{
    uchar i,j;
    i=80;
    while(i)
    {   sht10_statr();
        if(write_sht10(0x07))   //收到应答信号
        {   for(i=0;i<8;i++)
            {   sclk=1;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
                j<<=1; j|=sdat;
                sclk=0;  _nop_(); _nop_(); _nop_();_nop_();_nop_();_nop_();
            }
            shtbit_flag=(bit)(j&0x01);
            return;
        }
        else --i;
    }
    sht_dam_flag=1; //温湿度传感器损坏标志
}


*/
//---------------------------------------------------------------
//filename : read_temp()
//功    能 : 向SHT10发送1个字节的数据, 发送是高位在前,低位在后
//           在发送完最低位后等待SHT10的应答信号,如果SHT10正确
//           收到命令,会把数据线拉低
//
//           在CPU发出第8个sclk时钟的下降沿之后,SHT10将sdat下拉
//           为低电平,在CPU发出第9个sclk时钟的下降沿之后,SHT10
//           将释放sdat(恢复高电平)
//
//返回数据 : 0 无应答
//           1 有应答
//---------------------------------------------------------------
//
//=1, 下一周期准备启动温度采集
//=2, 等待温度转换结束
//=3, 下一周期准备启动湿度采集
//=4, 等待湿度转换结束

void temp_order()
{
    uchar i=10;
    read_sht_flag=0;
	LED = 1;
    
    while(i)
    {   sht10_statr();
        if(write_sht10(0x03)){wait_code=2;return;}//等待温度转换结束
        else --i;
    }
    wait_code=3; //温度转换命令失败,准备启动湿度转换
}

void rh_order()
{
    uchar i=10;
    read_sht_flag=0;
	LED = 1;
    while(i)
    {   sht10_statr();
        if(write_sht10(0x05)){wait_code=4;return;}//等待湿度转换结束
        else --i;
    }
    wait_code=1; //湿度转换命令失败,准备启动温度转换
}

void sht10_samp()
{
    //uint k;
    if(wait_code==0||wait_code>4)wait_code=1;
    if(wait_code==1){if(read_sht_flag)temp_order();}
    else if(wait_code==3){if(read_sht_flag)rh_order();}
    else
    {   if(!sdat)
        {   read_sht10();
            if(wait_code==2)
            {   /*temp[0]=uint_rht/1000 + 0X30;  //原值
                temp[1]=uint_rht/100%10 + 0X30;
                temp[2]=uint_rht/10%10 + 0X30;
                temp[3]=uint_rht%10 + 0X30;
				uart_send1(temp,5);*/
			    out_temp=uint_rht/10-400;//温度在-20度-70度有效
				mach_t();
                wait_code=3;

                /*temp[0]=out_temp/100%10 + 0X30; //温度值
                temp[1]=out_temp/10%10 + 0X30;
				temp[2]= 0x2e;
                temp[3]=out_temp%10 + 0X30;
				uart_send1(temp,5);*/

				/*tempp = (out_temp + 200)*32/75 + 96; //电压值
				temp[0]=tempp/100 + 0X30;
				temp[1]= 0x2e;
                temp[2]=tempp/10%10 + 0X30;
                temp[3]=tempp%10 + 0X30;
				uart_send1(temp,5);*/
				/*temp[0]=out_temp/1000 + 0X30;
                temp[1]=out_temp/100%10 + 0X30;
                temp[2]=out_temp/10%10 + 0X30;
                temp[3]=out_temp%10 + 0X30;
				uart_send1(temp,4);*/
            }
            else
            {   /*temp[0]=uint_rht/1000 + 0X30;  //原值
                temp[1]=uint_rht/100%10 + 0X30;
                temp[2]=uint_rht/10%10 + 0X30;
                temp[3]=uint_rht%10 + 0X30;
				uart_send1(temp,5);*/
                out_rh=(ulong)uint_rht*81/200-(ulong)uint_rht*(ulong)uint_rht*7/250000-40;
				mach_rh();
                /*k=uint_rht/125+1;
                if(out_temp>250)k=(k*(out_temp-250))/1000+out_rh;
                else wb_rh=out_rh-(k*(250-out_temp))/1000;*/
                wait_code=1;

                /*rh[0]=out_rh/100%10 + 0X30;        //湿度值
                rh[1]=out_rh/10%10 + 0X30;
				rh[2]= 0x2e;
                rh[3]=out_rh%10 + 0X30;
				uart_send1(rh,5);*/
				/*rh[0]=out_rh/1000 + 0X30;
                rh[1]=out_rh/100%10 + 0X30;
                rh[2]=out_rh/10%10 + 0X30;
                rh[3]=out_rh%10 + 0X30;
				uart_send1(rh,4);*/
            }
        }
    }
}
/*******************************************************************************
函数名称:uart_send_byte()
功能:串口发送函数
函数参数:data:发送的数据
******************************************************************************
void uart_send_byte(uchar udata)
{
    TI = 0;                         // 清零串口发送中断标志
    SBUF = udata;
    while (TI == 0);
    TI = 0;                         // 清零串口发送中断标志
}*/
/*******************************************************************************
函数名称:uart_send()
功能:串口发送函数
函数参数:*send_buf:发送的数据
         n:数据个数
******************************************************************************
void uart_send1(uchar *send_buf,uchar n)
{
    while (n--)
    {
        uart_send_byte(*send_buf);
        send_buf++;
    }
}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -