1881 views|2 replies

274

Posts

8

Resources
The OP
 

Chapter 5: Reading DHT12 data [Copy link]

 

1. Connect DHT12

The DHT12 temperature and humidity sensor uses the I2C interface for communication, so you need to use the I2C interface on the RVB2601 development board. By default, the I2C interface on the board is connected to the onboard ES7210 and ES8156 through a jumper cap, so you need to remove the jumper cap. The positions of I2C, power, and ground are as follows.

2. Code Writing

RVB2601 provides I2C driver functions. When using it, you need to import the header file: __#include <drv/iic.h>__. The functions you need to use include:

  1. csi_error_t csi_iic_init(csi_iic_t *iic, uint32_t idx), initializes a handle of an I2C device, where iic is a variable created by the user and idx is the number of the I2C device, starting from 0. Remember to set the PIN function before initialization ;
  2. csi_error_t csi_iic_mode(csi_iic_t *iic, csi_iic_mode_t mode), configure the working mode of the I2C device. Here it is used as a host, so it needs to be configured as IIC_MODE_MASTER ;
  3. csi_error_t csi_iic_addr_mode(csi_iic_t *iic, csi_iic_addr_mode_t addr_mode), set the address mode of I2C communication, including 7-bit address and 10-bit address, here use 7-bit address, IIC_ADDRESS_7BIT ;
  4. csi_error_t csi_iic_speed(csi_iic_t *iic, csi_iic_speed_t speed), set the I2C communication speed, here use the standard speed, IIC_BUS_SPEED_STANDARD ;
  5. int32_t csi_iic_master_send(csi_iic_t iic,uint32_t devaddr,const void data,uint32_t size,uint32_t timeout), send data. Note that the second parameter devaddr is a 7-bit address. For example, the address 0xB8 given in the DHT12 manual contains the lowest read/write flag bit. Therefore, 0xB8 needs to be shifted left by one bit to get 0x5C .
  6. int32_t csi_iic_master_receive(csi_iic_t iic, uint32_t devaddr, void data, uint32_t size, uint32_t timeout),读取数据。

The operation code is as follows:

static csi_iic_t g_iic;

void dht12_init()
{
    csi_pin_set_mux(PA8, PA8_IIC0_SCL);
    csi_pin_set_mux(PA9, PA9_IIC0_SDA);

    csi_iic_init(&g_iic, 0);
    csi_iic_mode(&g_iic, IIC_MODE_MASTER);
    csi_iic_addr_mode(&g_iic, IIC_ADDRESS_7BIT);
    csi_iic_speed(&g_iic, IIC_BUS_SPEED_STANDARD);
}

void dht12_get_data()
{
    unsigned char buf[10]={0};
    char str_data[50];
    csi_iic_master_send(&g_iic,0x5c,buf,1,1000);
    csi_iic_master_receive(&g_iic,0x5c,buf,5,1000);
    sprintf(str_data,"T:%d.%dtH:%d.%d",buf[2],buf[3],buf[0],buf[1]);
    LOGD(TAG,"%s",str_data);    
}

The following figure is a screenshot of the display

The driver function of RVB2601 is very simple to use, and the use of I2C is also very simple. It is just stuck at the slave address, which should be a 7-bit address.

Latest reply

It is really easy to use. But the jumper is a bit troublesome. Thanks for sharing.  Details Published on 2022-5-4 15:32
 
 

6555

Posts

0

Resources
2
 

The second parameter devaddr of sending data is a 7-bit address. This is a good reminder. It is the easiest place to make mistakes. Thank you.

 
 
 

6818

Posts

11

Resources
3
 
It is really easy to use. But the jumper is a bit troublesome. Thanks for sharing.
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list