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