SHT2x系列数字温湿度传感器
SHT2x系列数字温湿度传感器
SHT2x系列和SHT1x系列相比电压范围缩小了(2.1~3.6V),同时功率明显比之前的系列要小。其它参数基本差不多。最大的区别就是传输模式了。比之前的要完善很多,支持标准的I2C了,调试起来更加顺手了。
<此处有待补充>
本着能不动手就少动手的原则,我在调试的时候用的是一个现成的程序,它是用IO口模拟I2C的。 代码如下:
前一个是头文件,后一个是函数实现。
1 #ifndef I2C_HAL_H
2 #define I2C_HAL_H
3 //==============================================================================
4 // S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland
5 //==============================================================================
6 // Project : SHT2x Sample Code (V1.0)
7 // File : I2C_HAL.h
8 // Author : MST
9 // Controller: NEC V850/SG3 (uPD70F3740)
10 // Compiler : IAR compiler for V850 (3.50A)
11 // Brief : I2C Hardware abstraction layer
12 //==============================================================================
13
14 //---------- Includes ----------------------------------------------------------
15 #include "system.h"
16
17 //---------- Defines -----------------------------------------------------------
18 //I2C ports
19 //The communication on SDA and SCL is done by switching pad direction
20 //For a low level on SCL or SDA, direction is set to output. For a high level on
21 //SCL or SDA, direction is set to input. (pull up resistor active)
22 #define SDA PM3H_bit.no0 //SDA on I/O P38 defines direction (input=1/output=0)
23 #define SDA_CONF P3H_bit.no0 //SDA level on output direction
24 #define SCL PM3H_bit.no1 //SCL on I/O P39 defines direction (input=1/output=0)
25 #define SCL_CONF P3H_bit.no1 //SCL level on output direction
26
27 //---------- Enumerations ------------------------------------------------------
28 // I2C level
29 typedef enum{
30 LOW = 0,
31 HIGH = 1,
32 }etI2cLevel;
33
34 // I2C acknowledge
35 typedef enum{
36 ACK = 0,
37 NO_ACK = 1,
38 }etI2cAck;
39
40 //==============================================================================
41 void I2c_Init ();
42 //==============================================================================
43 //Initializes the ports for I2C interface
44
45 //==============================================================================
46 void I2c_StartCondition ();
47 //==============================================================================
48 // writes a start condition on I2C-bus
49 // input : -
50 // output: -
51 // return: -
52 // note : timings (delay) may have to be changed for different microcontroller
53 // _____
54 // SDA: |_____
55 // _______
56 // SCL : |___
57
58 //==============================================================================
59 void I2c_StopCondition ();
60 //==============================================================================
61 // writes a stop condition on I2C-bus
62 // input : -
63 // output: -
64 // return: -
65 // note : timings (delay) may have to be changed for different microcontroller
66 // _____
67 // SDA: _____|
68 // _______
69 // SCL : ___|
70
71 //===========================================================================
72 u8t I2c_WriteByte (u8t txByte);
73 //===========================================================================
74 // writes a byte to I2C-bus and checks acknowledge
75 // input: txByte transmit byte
76 // output: -
77 // return: error
78 // note: timings (delay) may have to be changed for different microcontroller
79
80 //===========================================================================
81 u8t I2c_ReadByte (etI2cAck ack);
82 //===========================================================================
83 // reads a byte on I2C-bus
84 // input: rxByte receive byte
85 // output: rxByte
86 // note: timings (delay) may have to be changed for different microcontroller
87
88 #endif
1 //==============================================================================
2 // S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland
3 //==============================================================================
4 // Project : SHT2x Sample Code (V1.0)
5 // File : I2C_HAL.c
6 // Author : MST
7 // Controller: NEC V850/SG3 (uPD70F3740)
8 // Compiler : IAR compiler for V850 (3.50A)
9 // Brief : I2C Hardware abstraction layer
10 //==============================================================================
11
12 //---------- Includes ----------------------------------------------------------
13 #include "I2C_HAL.h"
14
15 //==============================================================================
16 void I2c_Init ()
17 //==============================================================================
18 {
19 SDA=LOW; // Set port as output for configuration
20 SCL=LOW; // Set port as output for configuration
21
22 SDA_CONF=LOW; // Set SDA level as low for output mode
23 SCL_CONF=LOW; // Set SCL level as low for output mode
24
25 SDA=HIGH; // I2C-bus idle mode SDA released (input)
26 SCL=HIGH; // I2C-bus idle mode SCL released (input)
27 }
28
29 //==============================================================================
30 void I2c_StartCondition ()
31 //==============================================================================
32 {
33 SDA=HIGH;
34 SCL=HIGH;
35
36 SDA=LOW;
37 DelayMicroSeconds(10); // hold time start condition (t_HD;STA)
38 SCL=LOW;
39 DelayMicroSeconds(10);
40 }
41
42 //==============================================================================
43 void I2c_StopCondition ()
44 //==============================================================================
45 {
46 SDA=LOW;
47 SCL=LOW;
48 SCL=HIGH;
49 DelayMicroSeconds(10); // set-up time stop condition (t_SU;STO)
50 SDA=HIGH;
51 DelayMicroSeconds(10);
52 }
53
54 //==============================================================================
55 u8t I2c_WriteByte (u8t txByte)
56 //==============================================================================
57 {
58 u8t mask,error=0;
59 for (mask=0x80; mask>0; mask>>=1) //shift bit for masking (8 times)
60 { if ((mask & txByte) == 0) SDA=LOW;//masking txByte, write bit to SDA-Line
61 else SDA=HIGH;
62 DelayMicroSeconds(1); //data set-up time (t_SU;DAT)
63 SCL=HIGH; //generate clock pulse on SCL
64 DelayMicroSeconds(5); //SCL high time (t_HIGH)
65 SCL=LOW;
66 DelayMicroSeconds(1); //data hold time(t_HD;DAT)
67 }
68 SDA=HIGH; //release SDA-line
69 SCL=HIGH; //clk #9 for ack
70 DelayMicroSeconds(1); //data set-up time (t_SU;DAT)
71 if(SDA_CONF==HIGH) error=ACK_ERROR; //check ack from i2c slave
72 SCL=LOW;
73 DelayMicroSeconds(20); //wait time to see byte package on scope
74 return error; //return error code
75 }
76
77 //==============================================================================
78 u8t I2c_ReadByte (etI2cAck ack)
79 //==============================================================================
80 {
81 u8t mask,rxByte=0;
82 SDA=HIGH; //release SDA-line
83 for (mask=0x80; mask>0; mask>>=1) //shift bit for masking (8 times)
84 { SCL=HIGH; //start clock on SCL-line
85 DelayMicroSeconds(1); //data set-up time (t_SU;DAT)
86 DelayMicroSeconds(3); //SCL high time (t_HIGH)
87 if (SDA_CONF==1) rxByte=(rxByte | mask); //read bit
88 SCL=LOW;
89 DelayMicroSeconds(1); //data hold time(t_HD;DAT)
90 }
91 SDA=ack; //send acknowledge if necessary
92 DelayMicroSeconds(1); //data set-up time (t_SU;DAT)
93 SCL=HIGH; //clk #9 for ack
94 DelayMicroSeconds(5); //SCL high time (t_HIGH)
95 SCL=LOW;
96 SDA=HIGH; //release SDA-line
97 DelayMicroSeconds(20); //wait time to see byte package on scope
98 return rxByte; //return error code
99 }