W800開發板溫濕度傳感器數據異常

發布於 2022-04-14 17:38:03

使用IIC讀取溫濕度傳感器的數據時,讀出來的數據異常,不是正確的溫濕度值。
按照貴公司提供的傳感器手冊:
image.png
讀取數據,邏輯分析儀顯示,IIC傳輸是沒問題的.
寫入0x00:
image.png

延時20ms:
image.png

讀取0x00寄存器的數據:
image.png

代碼如下:
image.png

從寄存器讀出的數據都是錯的。。。。。轉化過後是這樣的數據,不對:
image.png

查看更多

關注者
0
被浏覽
1.7k
3 個回答
isme
isme 認證專家 2022-04-15
冰鎮大西瓜

我重新弄了一個驅動,板子自身也有發熱的情況,可能本身硬件精度就不是很好。

/***************************************************************************** 
* 
* File Name : main.c
* 
* Description: main 
* 
* Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd. 
* All rights reserved. 
* 
* Author : dave
* 
* Date : 2014-6-14
*****************************************************************************/ 
#include "wm_include.h"
#include "wm_i2c.h"
#include "stdio.h"
#include "wm_gpio_afsel.h"

#define I2C_FREQ        (5000)
#define I2C_ADDR        (0x80)

u8 buf[4];

void cht8305_iic_init(void)
{
    wm_i2c_scl_config(WM_IO_PA_01);
    wm_i2c_sda_config(WM_IO_PA_04);
    
    tls_i2c_init(I2C_FREQ);
}
 
void cht8305_read_reg(u8 reg_addr,u8 len)
{
    tls_i2c_write_byte(I2C_ADDR, 1); 
    tls_i2c_wait_ack();    
    tls_i2c_write_byte(reg_addr, 0);
    tls_i2c_wait_ack();    
    if (reg_addr == 0x00 || reg_addr == 0x01)
    {
        tls_os_time_delay(5); // 10ms delay
    }
    tls_i2c_write_byte(I2C_ADDR|0x01,1);   

    for (int i = 0; i < len-1; i++) {
        buf[i] = tls_i2c_read_byte(1,0);
    }

    buf[len - 1] = tls_i2c_read_byte(0, 1); //Give NACK
}
void cht8305_get_temp_humi(float *t, float *h)
{
    cht8305_read_reg(0x00, 4);
    unsigned int th, tl, hh, hl;
    th = buf[0];
    tl = buf[1];
    hh = buf[2];
    hl = buf[3];
    (*t) = (th << 8 | tl) * 165.0 / 65535.0 - 40.0;
    (*h) = (hh << 8 | hl) * 100.0 / 65535.0;
}

void UserMain(void)
{
    printf("\n user task \n");
    cht8305_iic_init();
    float  ct8305_temp ;
    float  ct8305_humi;
    while(1)
    {
        cht8305_get_temp_humi(&ct8305_temp,&ct8305_humi);
        printf("upload_hygrotherm ct8305_temp %f,ct8305_humi %f\r\n",ct8305_temp,ct8305_humi);
        tls_os_time_delay(100); // delay 200ms
    }

#if DEMO_CONSOLE
    CreateDemoTask();
#endif
//用戶自己的task
}

image.png
image.png

Mrzhao
Mrzhao 認證專家 2022-04-15
一個嵌入式愛好者。CSDN主頁:https://blog.csdn.net/qq_37280428?spm=1011.2415.3001.5343

目前得出的結論是硬件有問題。

大信
大信 認證專家 2022-04-15
硬件開發,軟件開發,系統開發,工程架構,方案設計

這種溫度傳感器,板子周圍需要開隔離槽,並且盡量遠離處理器

撰寫答案

請登錄後再發布答案,點擊登錄

發布
問題

分享
好友

手機
浏覽

掃碼手機浏覽