/
ds18b20.c
158 lines (139 loc) · 3.33 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <avr/io.h>
#include <util/delay.h>
#include "ds18b20.h"
#include "bitbang.h"
uint8_t readTempData_ds18b20(int *temperature) {
if(resetSensor() != 0){
return 1;
}
OWWriteByte(0xCC); //skip rom
OWWriteByte(0x44); //convert T
_delay_ms(800);
if(resetSensor() != 0){
return 1;
}
OWWriteByte(0xCC); //skip rom
OWWriteByte(0xBE); //read scratchpad
*temperature = readScratchpad(2);
//*temperature = 0xFC90;
return 0;
}
uint8_t resetSensor() {
uint8_t result;
//_delay_ms(2000);
setdirection(TDATA, 1); //PB1 output
setpin(TDATA, 1); //PB1 high VDD init
_delay_ms(100);
setpin(TDATA, 0);
_delay_us(490);
setdirection(TDATA, 0);
_delay_us(70);
result = (TPIN & (1 << TDATA));
_delay_us(410);
return result;
}
void writeBit(uint8_t bit) {
setdirection(TDATA, 1);
if (bit) {
setpin(TDATA, 0);
_delay_us(6);
setpin(TDATA, 1);
_delay_us(64);
} else {
setpin(TDATA, 0);
_delay_us(60);
setpin(TDATA, 1);
_delay_us(10);
}
}
uint8_t readBit() {
uint8_t result;
setdirection(TDATA, 1);
setpin(TDATA,0);
_delay_us(6);
setpin(TDATA,1);
_delay_us(9);
setdirection(TDATA, 0);
result = TPIN & (1 << TDATA);
_delay_us(55);
return result;
}
void OWWriteByte(int data)
{
int loop;
// Loop to write each bit in the byte, LS-bit first
for (loop = 0; loop < 8; loop++)
{
writeBit(data & 0x01);
// shift the data byte for the next bit
data >>= 1;
}
}
//-----------------------------------------------------------------------------
// Read 1-Wire data byte and return it
//
int OWReadByte(void)
{
int loop, result=0;
for (loop = 0; loop < 8; loop++)
{
// shift the result to get it ready for the next bit
result >>= 1;
// if result is one, then set MS bit
if (readBit())
result |= 0x80;
}
return result;
}
//-----------------------------------------------------------------------------
// Write a 1-Wire data byte and return the sampled result.
//
int OWTouchByte(int data)
{
int loop, result=0;
for (loop = 0; loop < 8; loop++)
{
// shift the result to get it ready for the next bit
result >>= 1;
// If sending a '1' then read a bit else write a '0'
if (data & 0x01)
{
if (readBit())
result |= 0x80;
}
else
writeBit(0);
// shift the data byte for the next bit
data >>= 1;
}
return result;
}
//-----------------------------------------------------------------------------
// Write a block 1-Wire data bytes and return the sampled result in the same
// buffer.
//
void OWBlock(unsigned char *data, int data_len)
{
int loop;
for (loop = 0; loop < data_len; loop++)
{
data[loop] = OWTouchByte(data[loop]);
}
}
int readScratchpad(int bytes){
//int byte[2];
int result = 0;
int intermediate = 0;
for(int i = 0; i < bytes; i++){
intermediate = OWReadByte();
//byte[i] = intermediate;
result |= intermediate << (8*i);
}
/*
resetSensor();
_delay_ms(2000);
OWWriteByte(byte[0]);
OWWriteByte(byte[1]);
*/
return result;
}