-
Notifications
You must be signed in to change notification settings - Fork 20
/
DS18B20.c
80 lines (69 loc) · 1.82 KB
/
DS18B20.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "DS18B20.h"
#include "onewire.h"
/*
send message to every sensor on the bus to take a reading
*/
void broadcastConvert() {
//broadcast that temp conversions should begin, all at once so saves time
onewireInit();
onewireWriteByte(0xCC);
onewireWriteByte(0x44);
while (1) {
if (onewireReadBit())
break;
}
}
/*
retrieve temperatures from sensors
*/
float getTemperature(unsigned char* address) {
//get temperature from the device with address address
float temperature;
unsigned char scratchPad[9] = {0,0,0,0,0,0,0,0,0};
onewireInit();
onewireWriteByte(0x55);
unsigned char i;
for (i = 0; i < 8; i++)
onewireWriteByte(address[i]);
onewireWriteByte(0xBE);
for (i = 0; i < 2; i++) {
scratchPad[i] = onewireReadByte();
}
onewireInit();
temperature = ((scratchPad[1] * 256) + scratchPad[0])*0.0625;
return temperature;
}
int getTemperatureInt(unsigned char* address) {
//get temperature from the device with address address
int temperature;
unsigned char scratchPad[9] = {0,0,0,0,0,0,0,0,0};
onewireInit();
onewireWriteByte(0x55);
unsigned char i;
for (i = 0; i < 8; i++)
onewireWriteByte(address[i]);
onewireWriteByte(0xBE);
for (i = 0; i < 2; i++) {
scratchPad[i] = onewireReadByte();
}
onewireInit();
temperature = ((scratchPad[1] * 256) + scratchPad[0]);
return temperature;
}
/*
retrieve address of sensor and print to terminal
*/
void printSingleAddress() {
onewireInit();
//attach one sensor to port 25 and this will print out it's address
unsigned char address[8]= {0,0,0,0,0,0,0,0};
onewireWriteByte(0x33);
unsigned char i;
for (i = 0; i<8; i++)
address[i] = onewireReadByte();
for (i = 0; i<8; i++)
printf("0x%x,",address[i]);
//check crc
unsigned char crc = onewireCRC(address, 7);
printf("crc = %x \r\n",crc);
}