1 |
#include "main.h" |
1 |
#include "main.h" |
2 |
|
2 |
|
3 |
void SHT25_soft_reset() |
3 |
void SHT25_soft_reset() |
4 |
{ |
4 |
{ |
5 |
i2c_start(); // Start condition |
5 |
i2c_start(); // Start condition |
6 |
i2c_write(0x80); // Device address |
6 |
i2c_write(0x80); // Device address |
7 |
i2c_write(0xFE); // Device command |
7 |
i2c_write(0xFE); // Device command |
8 |
i2c_stop(); // Stop condition |
8 |
i2c_stop(); // Stop condition |
9 |
} |
9 |
} |
10 |
|
10 |
|
- |
|
11 |
SHT_ |
- |
|
12 |
|
11 |
unsigned int8 SHT25_setup() |
13 |
unsigned int8 SHT25_setup() |
12 |
{ |
14 |
{ |
13 |
unsigned int8 reg; |
15 |
unsigned int8 reg; |
14 |
|
16 |
|
15 |
i2c_start(); // Start condition |
17 |
i2c_start(); // Start condition |
16 |
i2c_write(0x80); // Device address |
18 |
i2c_write(0x80); // Device address |
17 |
i2c_write(0xE7); // Device command |
19 |
i2c_write(0xE7); // Device command |
18 |
// i2c_stop(); // Stop condition |
- |
|
19 |
|
20 |
|
20 |
i2c_start(); // Start condition |
21 |
i2c_start(); // Start condition |
21 |
i2c_write(0x81); // Device address |
22 |
i2c_write(0x81); // Device address |
22 |
reg=i2c_read(0); // Device command |
23 |
reg=i2c_read(0); // Device command |
23 |
i2c_stop(); // Stop condition |
24 |
i2c_stop(); // Stop condition |
24 |
|
25 |
|
25 |
return (reg); |
26 |
return (reg); |
26 |
} |
27 |
} |
27 |
|
28 |
|
28 |
float SHT25_get_temp() |
29 |
float SHT25_get_temp() |
29 |
{ |
30 |
{ |
30 |
unsigned int8 MSB, LSB, Check; |
31 |
unsigned int8 MSB, LSB, Check; |
31 |
unsigned int16 data; |
32 |
unsigned int16 data; |
32 |
|
33 |
|
33 |
i2c_start(); |
34 |
i2c_start(); |
34 |
I2C_Write(0x80); |
35 |
I2C_Write(0x80); |
35 |
I2C_write(0xE3); |
36 |
I2C_write(0xE3); |
36 |
i2c_stop(); |
37 |
i2c_stop(); |
37 |
|
38 |
|
38 |
delay_ms(100); |
39 |
delay_ms(100); |
39 |
|
40 |
|
40 |
i2c_start(); |
41 |
i2c_start(); |
41 |
I2C_Write(0x81); |
42 |
I2C_Write(0x81); |
42 |
MSB=i2c_read(1); |
43 |
MSB=i2c_read(1); |
43 |
LSB=i2c_read(1); |
44 |
LSB=i2c_read(1); |
44 |
Check=i2c_read(0); |
45 |
Check=i2c_read(0); |
45 |
i2c_stop(); |
46 |
i2c_stop(); |
46 |
|
47 |
|
47 |
LSB = LSB >> 2; // trow out status bits |
48 |
LSB = LSB >> 2; // trow out status bits |
48 |
|
49 |
|
49 |
data = (((unsigned int16) MSB << 8) + (LSB << 4)); |
50 |
data = (((unsigned int16) MSB << 8) + (LSB << 4)); |
50 |
return(-46.85 + 175.72*((float)data/0xFFFF)); |
51 |
return(-46.85 + 175.72*((float)data/0xFFFF)); |
51 |
} |
52 |
} |
52 |
|
53 |
|
53 |
float SHT25_get_hum() |
54 |
float SHT25_get_hum() |
54 |
{ |
55 |
{ |
55 |
unsigned int8 MSB, LSB, Check; |
56 |
unsigned int8 MSB, LSB, Check; |
56 |
unsigned int16 data; |
57 |
unsigned int16 data; |
57 |
|
58 |
|
58 |
i2c_start(); //RH |
59 |
i2c_start(); //RH |
59 |
I2C_Write(0x80); |
60 |
I2C_Write(0x80); |
60 |
I2C_write(0xE5); |
61 |
I2C_write(0xE5); |
61 |
// i2c_stop(); |
62 |
// i2c_stop(); |
62 |
|
63 |
|
63 |
delay_ms(100); |
64 |
delay_ms(100); |
64 |
|
65 |
|
65 |
i2c_start(); |
66 |
i2c_start(); |
66 |
I2C_Write(0x81); |
67 |
I2C_Write(0x81); |
67 |
MSB=i2c_read(1); |
68 |
MSB=i2c_read(1); |
68 |
LSB=i2c_read(1); |
69 |
LSB=i2c_read(1); |
69 |
Check=i2c_read(0); |
70 |
Check=i2c_read(0); |
70 |
i2c_stop(); |
71 |
i2c_stop(); |
71 |
|
72 |
|
72 |
// printf("%X %X %X\r\n",MSB, LSB, Check); |
73 |
// printf("%X %X %X\r\n",MSB, LSB, Check); |
73 |
|
74 |
|
74 |
LSB = LSB >> 2; // trow out status bits |
75 |
LSB = LSB >> 2; // trow out status bits |
75 |
|
76 |
|
76 |
data = (((unsigned int16) MSB << 8) + (LSB << 4) ); |
77 |
data = (((unsigned int16) MSB << 8) + (LSB << 4) ); |
77 |
return( -6.0 + 125.0*((float)data/0xFFFF)); |
78 |
return( -6.0 + 125.0*((float)data/0xFFFF)); |
78 |
} |
79 |
} |
79 |
|
80 |
|
80 |
|
81 |
|
81 |
void main() |
82 |
void main() |
82 |
{ |
83 |
{ |
83 |
|
84 |
|
84 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
85 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
85 |
setup_adc(ADC_CLOCK_DIV_2); |
86 |
setup_adc(ADC_CLOCK_DIV_2); |
86 |
setup_spi(SPI_SS_DISABLED); |
87 |
setup_spi(SPI_SS_DISABLED); |
87 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
88 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
88 |
setup_timer_1(T1_DISABLED); |
89 |
setup_timer_1(T1_DISABLED); |
89 |
setup_timer_2(T2_DISABLED,0,1); |
90 |
setup_timer_2(T2_DISABLED,0,1); |
90 |
setup_ccp1(CCP_OFF); |
91 |
setup_ccp1(CCP_OFF); |
91 |
setup_comparator(NC_NC_NC_NC); |
92 |
setup_comparator(NC_NC_NC_NC); |
92 |
|
93 |
|
93 |
void SHT25_soft_reset(); |
94 |
void SHT25_soft_reset(); |
94 |
printf("SHT25 humidity and temperature sensor example \r\n",); |
95 |
printf("SHT25 humidity and temperature sensor example \r\n",); |
95 |
delay_ms (500); |
96 |
delay_ms (500); |
96 |
|
97 |
|
97 |
while(TRUE) |
98 |
while(TRUE) |
98 |
{ |
99 |
{ |
99 |
printf("setup: %X \r\n",SHT25_setup()); |
100 |
printf("setup: %X \r\n",SHT25_setup()); |
100 |
delay_ms (500); |
101 |
delay_ms (500); |
101 |
printf("Temp: %f \r\n",SHT25_get_temp()); |
102 |
printf("Temp: %f \r\n",SHT25_get_temp()); |
102 |
delay_ms (500); |
103 |
delay_ms (500); |
103 |
printf("Hum: %f \r\n",SHT25_get_hum()); |
104 |
printf("Hum: %f \r\n",SHT25_get_hum()); |
104 |
delay_ms (1000); |
105 |
delay_ms (1000); |
105 |
} |
106 |
} |
106 |
} |
107 |
} |
107 |
|
108 |
|