From 6100a908f25ec6eac446aadd5180f347a71e3db5 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Sun, 14 Oct 2018 17:51:10 +0800 Subject: [PATCH] update ds18b20.c and sht20.c source code --- program/modules/sht20/sht20.c | 93 +++++++++++++++++++++++++++------------------- 1 files changed, 54 insertions(+), 39 deletions(-) diff --git a/program/modules/sht20/sht20.c b/program/modules/sht20/sht20.c index 0ff2101..5395bbe 100644 --- a/program/modules/sht20/sht20.c +++ b/program/modules/sht20/sht20.c @@ -1,3 +1,15 @@ +/********************************************************************************* + * Copyright: (C) 2018 LingYun IoT System Studio + * All rights reserved. + * + * Filename: sht20.c + * Description: This file is temperature and relative humidity sensor SHT20 code + * + * Version: 1.0.0(2018/10/14) + * Author: Guo Wenxue <guowenxue@gmail.com> + * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" + * + ********************************************************************************/ #include <stdio.h> #include <fcntl.h> @@ -27,7 +39,6 @@ static inline void msleep(unsigned long ms); static inline void dump_buf(const char *prompt, uint8_t *buf, int size); int sht2x_init(void); -int sht2x_softreset(int fd); int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size); int sht2x_get_temp_humidity(int fd, float *temp, float *rh); @@ -43,12 +54,6 @@ { printf("SHT2x initialize failure\n"); return 1; - } - - if( sht2x_softreset(fd) < 0 ) - { - printf("SHT2x softreset failure\n"); - return 2; } if( sht2x_get_serialnumber(fd, serialnumber, 8) < 0) @@ -115,23 +120,6 @@ #ifdef I2C_API_RDWR /* Use I2C userspace driver read/write API */ -int sht2x_init(void) -{ - int fd; - - if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) - { - printf("i2c device open failed: %s\n", strerror(errno)); - return -1; - } - - /* set I2C mode and SHT2x slave address */ - ioctl(fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ - ioctl(fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ - - return fd; -} - int sht2x_softreset(int fd) { uint8_t buf[4]; @@ -151,6 +139,29 @@ msleep(50); return 0; +} + +int sht2x_init(void) +{ + int fd; + + if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) + { + printf("i2c device open failed: %s\n", strerror(errno)); + return -1; + } + + /* set I2C mode and SHT2x slave address */ + ioctl(fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ + ioctl(fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ + + if( sht2x_softreset(fd) < 0 ) + { + printf("SHT2x softreset failure\n"); + return -2; + } + + return fd; } int sht2x_get_temp_humidity(int fd, float *temp, float *rh) @@ -235,20 +246,6 @@ #elif (defined I2C_API_IOCTL) /* Use I2C userspace driver read/write API */ -int sht2x_init(void) -{ - int fd; - - if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) - { - printf("i2c device open failed: %s\n", strerror(errno)); - return -1; - } - - return fd; -} - - int sht2x_softreset(int fd) { struct i2c_msg msg; @@ -282,6 +279,25 @@ return 0; } + +int sht2x_init(void) +{ + int fd; + + if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) + { + printf("i2c device open failed: %s\n", strerror(errno)); + return -1; + } + + if( sht2x_softreset(fd) < 0 ) + { + printf("SHT2x softreset failure\n"); + return -2; + } + + return fd; +} int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size) @@ -363,7 +379,6 @@ return 0; } - int sht2x_get_temp_humidity(int fd, float *temp, float *rh) -- Gitblit v1.9.1