打印
[STM32F0]

STM32F0驱动SHT11结果异常,请求帮助

[复制链接]
3349|26
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dsoyy|  楼主 | 2015-12-20 23:40 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
之前51上使用的SHT11代码搬移过来到STM32F0,外接8M晶振,发现SHT11采样结果不对,通过仿真器看到hum采样结果0.100000001,temp:12625470
对比SHT11官方代码,发现时序是吻合的,单步仿真看到IO也是受控的。

实在想不出哪里还有什么差异,对比网上别人的代码,也找不出差异。
硬件上dat引脚加上拉10K,维度我的电源是2.8V。

特此,请求帮助。


沙发
玛尼玛尼哄| | 2015-12-20 23:49 | 只看该作者
STM32F0使用心得

首先肯定是非常感谢21ic给的机会啦,使得我有幸用得利器STM32F0。也感谢论坛中互相共享资料的人儿们。
在F0的使用中,感觉还是蛮顺手的,它的低功耗跟430有得一拼,再者我觉得使用该固件库编程就跟Android编程有点像,都是调用各种库函数进行操作,而无需太注重某些具体实现的细节。我建议初学的话,以以下的学习方式:做某一模块的实验时,先看芯片中有关该模块的pdf,熟悉整个工作原理,然后再看固件库,将固件库与底层相对应,直至搞清各个函数的功能及使用方法,最后用这些固件函数进行相关操作就比较容易了。
下面我就来整理整理F0与其它所不同的特点吧:1)四个低功耗模式;2)简化的通信时钟设置;3)编程的模拟数字噪声过滤功能;4)消费电子控制器(CEC)模块;5)内置硬件RAM奇偶校验;6)硬件触摸控制功能;7)12位模数转换器(ADC)。
来说说我做的一个温湿度检测实验吧。

系统框图
            
系统描述本实验中主要用到PC上位机,主控器STM32F0探索套件和和温湿度传感器SHT11。用主控板与温湿度传感器通信获得相关温湿度数据,再将获得的数据进行处理,得到实际数据。再通过串口将温湿度数据打印至上位机。
温湿度传感器SHT11:

               

SHT11是瑞士Scnsirion公司推出的一款数字温湿度传感器芯片。该芯片广泛应用于暖通空调、汽车、消费电子、自动控制等领域。共主要特点如下:
◆高度集成,将温度感测、湿度感测、信号变换、A/D转换和加热器等功能集成到一个芯片上;
◆提供二线数字串行接口SCK和DATA,接口简单,支持CRC传输校验,传输可靠性高;
◆测量精度可编程调节,内置A/D转换器(分辨率为8~12位,可以通过对芯片内部寄存器编程选择);
◆测量精确度高,由于同时集成温湿度传感器,可以提供温度补偿的湿度测量值和高质量的露点计算功能;
◆封装尺寸超小(7.62 mm×5.08mm×2.5 mm),测量和通信结束后,自动转入低功耗模式;
◆高可靠性,采用CMOSens工艺,测量时可将感测头完全浸于水中
SHT11温湿度传感器采用SMD(LCC)表面贴片封装形式,接口非常简单,引脚名称及排列顺序如图所示。各引脚的功能如下: ◇脚5~8--未连接。 ◇脚1和4--信号地和电源,其工作电压范围是2.4~5.5 V; ◇脚2和脚3--二线串行数字接口,其中DATA为数据线,SCK为时钟线;
                  



使用特权

评论回复
板凳
玛尼玛尼哄| | 2015-12-20 23:49 | 只看该作者
相关函数和头文件见附件:

20.53 KB,


使用特权

评论回复
地板
dsoyy|  楼主 | 2015-12-21 09:55 | 只看该作者
本帖最后由 dsoyy 于 2015-12-21 10:00 编辑
玛尼玛尼哄 发表于 2015-12-20 23:49
相关程序块.zip
20.53 KB,

逻辑是一样的,差异就是nop,你用的是寄存器我用的是库函数。
没道理啊,时序应该没有这么严格
请问你的 _NOP();是怎么定义的?
谢谢

使用特权

评论回复
5
我是土匪| | 2015-12-22 09:25 | 只看该作者
MARK

使用特权

评论回复
6
玛尼玛尼哄| | 2015-12-24 23:09 | 只看该作者
dsoyy 发表于 2015-12-21 09:55
逻辑是一样的,差异就是nop,你用的是寄存器我用的是库函数。
没道理啊,时序应该没有这么严格
请问你的 _N ...

那个应该是某个头文件里定义的,不是我写的,应该是个宏定义,后面也是跟了个汇编的nop指令吧。

使用特权

评论回复
7
dsoyy|  楼主 | 2016-1-2 11:44 | 只看该作者
今天继续测试,用上面的代码也是不能成功,怀疑传感器坏了,接到51板子上,温湿度正常。因此还是代码问题。
下面 发代码和测试方法:
/***********************************************************************************
Project: SHT11 demo program (V2.0)
Filename: SHT11.c
Prozessor: st,m32 family
Compiler: Keil Version 6.14
Autor: MST
Copyrigth: (c) Sensirion AG
***********************************************************************************/

#define SHT11_DATA_PIN                         GPIO_Pin_5
#define SHT11_DATA_PORT                            GPIOA
#define SHT11_DATA_GPIO_CLK                    RCC_AHBPeriph_GPIOA
#define SHT11_DATA_SOURCE               GPIO_PinSource5


#define SHT11_CLK_PIN                         GPIO_Pin_4
#define SHT11_CLK_PORT                            GPIOA
#define SHT11_CLK_GPIO_CLK                    RCC_AHBPeriph_GPIOA
#define SHT11_CLK_SOURCE                GPIO_PinSource4

typedef union
{ unsigned int i;
        float f;
} value;

#define SHT11_DATA_H() GPIO_SetBits(SHT11_DATA_PORT,SHT11_DATA_PIN)
#define SHT11_DATA_L() GPIO_ResetBits(SHT11_DATA_PORT,SHT11_DATA_PIN)
#define SHT11_DATA_R() GPIO_ReadInputDataBit(SHT11_DATA_PORT,SHT11_DATA_PIN)

#define SHT11_CLK_H() GPIO_SetBits(SHT11_CLK_PORT,SHT11_CLK_PIN)
#define SHT11_CLK_L() GPIO_ResetBits(SHT11_CLK_PORT,SHT11_CLK_PIN)
enum {TEMP,HUMI};


#define noACK 0
#define ACK 1
//adr 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

//毫秒级的延时

void delay_uS(uint16 time)
{          
        uint16 i=0;  
        while(time--)
        {
                __nop();__nop();
                //for(i = 10; i> 0;i -- );
        }
}
void delay_ms(uint16 time)
{          
        uint16 i=0;  
        while(time--)
        {
               
                for(i = 12000; i> 0;i -- );
        }
}




//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------

void Init_Sht11_Gpio(void)
{
        //SHT11 CLK输出,//SHT11 dat 输出
       
        GPIO_InitTypeDef GPIO_InitStructure;

     /* GPIO Periph clock enable */
          RCC_AHBPeriphClockCmd(SHT11_DATA_GPIO_CLK, ENABLE);
       


          GPIO_InitStructure.GPIO_Pin = SHT11_DATA_PIN ;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
          GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure);

        RCC_AHBPeriphClockCmd(SHT11_CLK_GPIO_CLK, ENABLE);

          GPIO_InitStructure.GPIO_Pin = SHT11_CLK_PIN ;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
          GPIO_Init(SHT11_CLK_PORT, &GPIO_InitStructure);


}

       
void SHT11_ClkPin_OutPut(char dat)
{
    if(dat)
        {
            GPIO_SetBits(SHT11_CLK_PORT,SHT11_CLK_PIN);
        }
        else
        {
                GPIO_ResetBits(SHT11_CLK_PORT,SHT11_CLK_PIN);
        }
       
}

void SHT11_DataPin_Out(void)
{
        GPIO_InitTypeDef GPIO_InitStructure1;
       

        GPIO_InitStructure1.GPIO_Pin = SHT11_DATA_PIN;
        GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure1);
}
void SHT11_DataPin_in(void)
{
        GPIO_InitTypeDef GPIO_InitStructure1;
        char res = 0;
          /* DATA DIR INPUT */
        RCC_AHBPeriphClockCmd(SHT11_DATA_GPIO_CLK, ENABLE);
          
        GPIO_InitStructure1.GPIO_Pin = SHT11_DATA_PIN;
        GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(SHT11_DATA_PORT, &GPIO_InitStructure1);
       
}



//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
        unsigned char   i,error=0;

        SHT11_DataPin_Out();

        for (i=0x80;i>0;i/=2) //shift bit for masking
        {
                if (i & value)
                {
                        SHT11_DATA_H();//DATA=1; //masking value with i , write to SENSI-BUS
                }
                else
                {
                        SHT11_DATA_L();//DATA=0;
                }
                delay_uS(10);
                SHT11_CLK_H();//clk for SENSI-BUS
                delay_uS(30);//_nop_();_nop_();_nop_(); //pulswith approx. 5 us
                SHT11_CLK_L();//SCK=0;
                delay_uS(10);
        }
        SHT11_DATA_H();
        delay_uS(10);
        SHT11_DataPin_in();       
        SHT11_CLK_H(); //release DATA-line

        error=SHT11_DATA_R();//error=DATA; //check ack (DATA will be pulled down by SHT11)

        SHT11_CLK_L();//SCK=0;

        return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
        unsigned char   i,val=0;
        char res = 0;

    SHT11_DataPin_Out();       
    SHT11_DATA_H();//release DATA-line

    SHT11_DataPin_in();       


        for (i=0x80;i>0;i/=2) //shift bit for masking
        {

                SHT11_CLK_H(); //clk for SENSI-BUS

                if (SHT11_DATA_R())
                        val=(val | i); //read bit
       
                SHT11_CLK_L();//SCK=0;

        }
       
        SHT11_DataPin_Out();       

        if(ack)
                SHT11_DATA_L();
        else
                SHT11_DATA_H();

        delay_uS(10);
        SHT11_CLK_H();//SCK=1; //clk #9 for ack
        delay_uS(30);//_nop_();_nop_();_nop_(); //pulswith approx. 5 us
        SHT11_CLK_L();//SCK=0;
        delay_uS(10);

        SHT11_DATA_H();
        return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
//           _____               ________
// DATA:          |_______|
//                  __       __
// SCK :  _ ___| |___ | |______
{
        char j ;

        SHT11_DataPin_Out();       
    SHT11_DATA_H();//DATA=1;
        SHT11_CLK_L();//SCK=0; //Initial state
        delay_uS(10);//_nop_();
        SHT11_CLK_H();//SCK=1;
        delay_uS(10);//_nop_();
        SHT11_DATA_L();//DATA=0;
        delay_uS(10);//_nop_();
        SHT11_CLK_L();//SCK=0;
        delay_uS(30);//_nop_();_nop_();_nop_();
        SHT11_CLK_H();//SCK=1;
        delay_uS(10);//_nop_();
        SHT11_DATA_H();//DATA=1;
        delay_uS(10);//_nop_();
        SHT11_CLK_L();//SCK=0;

}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
        unsigned char  i;

        SHT11_DataPin_Out();
        SHT11_DATA_H();//DATA=1;
       
        SHT11_CLK_L();//Initial state
        for(i=0;i<9;i++) //9 SCK cycles
        {
                SHT11_CLK_H();//SCK=1;
        delay_uS(10);
                SHT11_CLK_L();//SCK=0;
delay_uS(10);

        }

        s_transstart(); //transmission start
}

//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
        unsigned char  error=0;
        s_connectionreset(); //reset communication
        error+=s_write_byte(RESET); //send RESET-command to sensor
        return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
        unsigned char   error=0;
        s_transstart(); //transmission start
        error=s_write_byte(STATUS_REG_R); //send command to sensor
        *p_value=s_read_byte(ACK); //read status register (8-bit)
        *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
        return error; //error=1 in case of no response form the sensor
}


//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
        unsigned char error=0;
        s_transstart(); //transmission start
        error+=s_write_byte(STATUS_REG_W);//send command to sensor
        error+=s_write_byte(*p_value); //send value of status register
        return error; //error>=1 in case of no response form the sensor
}
long int  count;

//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
        unsigned  error=0;
       
        char dat = 0;
       
        s_transstart(); //transmission start

        switch(mode)
        { //send command to sensor
                case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
                case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
                default : break;
        }
                SHT11_DataPin_in();//while(Read_SHT11_Data_Value());
        for (count=0;count<0xffffffff;count++)
        {
                //delay_uS(1);

                if(SHT11_DATA_R()==0)
                        break;
        }
       
        //wait until sensor has finished the measurement

        dat = SHT11_DATA_R();
        if(dat)
                error+=1; // or timeout (~2 sec.) is reached
  *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
        *(p_value+1) =s_read_byte(ACK); //read the first byte (MSB)
        *p_checksum =s_read_byte(noACK); //read checksum
        return error;
}
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input :  humi [Ticks] (12 bit)
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [癈]
{ const float C1=-2.0468;           // for 12 Bit RH
  const float C2=+0.0367;           // for 12 Bit RH
  const float C3=-0.0000015955;     // for 12 Bit RH
  const float T1=+0.01;             // for 12 Bit RH
  const float T2=+0.00008;          // for 12 Bit RH       

  float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit
  float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  float rh_lin;                     // rh_lin:  Humidity linear
  float rh_true;                    // rh_true: Temperature compensated humidity
  float t_C;                        // t_C   :  Temperature [癈]

  t_C=t*0.01 - 40.1;                //calc. temperature [癈] from 14 bit temp. ticks @ 5V
  rh_lin=C3*rh*rh + C2*rh + C1;     //calc. humidity from ticks to [%RH]
  rh_true=(t_C-25)*(T1+T2*rh)+rh_lin;   //calc. temperature compensated humidity [%RH]
  if(rh_true>100)rh_true=100;       //cut if the value is outside of
  if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  *p_temperature=t_C;               //return temperature [癈]
  *p_humidity=rh_true;              //return humidity[%RH]
}

//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input:   humidity [%RH], temperature [癈]
// output:  dew point [癈]
{ float k,dew_point ;
  
  k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
  dew_point = 243.12*k/(17.62-k);
  return dew_point;
}
value  humi_val,temp_val;

//----------------------------------------------------------------------------------
void GetSht11Value(void)
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
{
       
        float  dew_point;
        unsigned char  error,checksum;
  unsigned int i;
       
        s_connectionreset();

// while(1)
  { error=0;
    error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity
    error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature
        if(error!=0)
        {
                s_connectionreset(); //in case of an error: connection reset
        }
        else
        {
                humi_val.f=(float)humi_val.i;                   //converts integer to float
      temp_val.f=(float)temp_val.i;                   //converts integer to float
      calc_sth11(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
      dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
    }
    for (i=0;i<40000;i++);     //(be sure that the compiler doesn't eliminate this line!)
        }
}

使用特权

评论回复
8
dsoyy|  楼主 | 2016-1-2 11:47 | 只看该作者
这是完整代码。

测试:
s_connectionreset();函数时序,看起来是对的:

1.png (59.93 KB )

1.png

使用特权

评论回复
9
dsoyy|  楼主 | 2016-1-2 11:51 | 只看该作者
s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  
时序分为三部分,
s_transstart();
s_write_byte(MEASURE_TEMP);

  *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
        *(p_value+1) =s_read_byte(ACK); //read the first byte (MSB)
        *p_checksum =s_read_byte(noACK);
分别上时序图:

s_transstart.png (127.31 KB )

s_transstart.png

使用特权

评论回复
10
dsoyy|  楼主 | 2016-1-2 11:53 | 只看该作者
测量湿度数值,在第九个脉冲返回低电平,表示收到了ack

s_write_byte.png (200.42 KB )

s_write_byte.png

使用特权

评论回复
11
dsoyy|  楼主 | 2016-1-2 11:56 | 只看该作者
  *(p_value)  =s_read_byte(ACK);    //read the first byte (MSB)
        *(p_value+1) =s_read_byte(ACK); //read the first byte (MSB)
        *p_checksum =s_read_byte(noACK);
三组读命令,最后一组没有ack

3组命令.png (233.53 KB )

3组命令.png

使用特权

评论回复
12
dsoyy|  楼主 | 2016-1-2 12:47 | 只看该作者
经过调整后,上电第一次是可以正确获取的,、。
该测量函数是定时6秒执行一次,结果后面的数据就很离谱。

估计和代码逻辑有关,继续测试

调整.png (41.98 KB )

调整.png

使用特权

评论回复
13
dsoyy|  楼主 | 2016-1-2 14:36 | 只看该作者
第一次采集是正确的,后面采集数值就不对了。代码上看不出来,逻辑分析仪看逻辑也没问题,真是费解

对比.png (81.7 KB )

对比.png

使用特权

评论回复
14
Ketose| | 2016-1-2 19:49 | 只看该作者
单总线,最重要的是什么?---时序。。。。。
等待函数最好用定时器实现,那样才精确,绝对不会出错。。。。

使用特权

评论回复
15
dsoyy|  楼主 | 2016-1-2 21:17 | 只看该作者
Ketose 发表于 2016-1-2 19:49
单总线,最重要的是什么?---时序。。。。。
等待函数最好用定时器实现,那样才精确,绝对不会出错。。。。 ...

DS18B20是单总线,
SHT11是类似I2C的时序

目前是用延时函数实现的。看到大家的例子都是这样做的,时序应该没有这么严格

使用特权

评论回复
16
dsoyy|  楼主 | 2016-1-4 22:09 | 只看该作者
本帖最后由 dsoyy 于 2016-1-4 22:15 编辑

请帮忙分析一下,找不到原因了
第一次读取正确,第二次就不对了,2此时间间隔足够长6秒。

使用特权

评论回复
17
Ketose| | 2016-1-6 12:44 | 只看该作者
dsoyy 发表于 2016-1-2 21:17
DS18B20是单总线,
SHT11是类似I2C的时序

建议还是用定时器实现,如果有中断处理,你那种延时就不准了。

使用特权

评论回复
18
dsoyy|  楼主 | 2016-1-6 17:04 | 只看该作者
Ketose 发表于 2016-1-6 12:44
建议还是用定时器实现,如果有中断处理,你那种延时就不准了。

屏蔽中断后,还是会出现这样的情况。

使用特权

评论回复
19
Ketose| | 2016-1-6 17:32 | 只看该作者
dsoyy 发表于 2016-1-6 17:04
屏蔽中断后,还是会出现这样的情况。

用逻辑分析仪看下延时函数是否正确。

使用特权

评论回复
20
Brand2| | 2016-1-6 20:52 | 只看该作者
目前是用延时函数实现的

关键就是看延时准吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

144

主题

566

帖子

3

粉丝