-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds18b20nonblocking.c
89 lines (73 loc) · 2.4 KB
/
ds18b20nonblocking.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
#include "ds18b20nonblocking.h"
uint8_t DS18B20_init(uint8_t pin_number) {
uint8_t no_answer;
PORTD &= ~(1 << pin_number);
DDRD |= (1 << pin_number);
_delay_us(490);
DDRD &= ~(1 << pin_number);
_delay_us(68);
no_answer = (PIND & (1 << pin_number)); //0 - OK, 1 - not OK
_delay_us(422);
return no_answer;
}
uint8_t read_18b20(uint8_t pin_number) {
uint8_t dat = 0;
for(uint8_t i = 0; i < 8; i++) {
DDRD |= (1 << pin_number);
_delay_us(2);
DDRD &= ~(1 << pin_number);
_delay_us(4);
dat = dat >> 1;
if(PIND & (1 << pin_number)) {
dat |= 0x80;
}
_delay_us(62);
}
return dat;
}
void write_18b20(uint8_t dat, uint8_t pin_number) {
for(uint8_t j = 0;j < 8;j++) {
DDRD |= (1 << pin_number);
_delay_us(2);
if(dat & 0x01) {
DDRD &= ~(1 << pin_number);
} else {
DDRD |= (1 << pin_number);
}
dat = dat >> 1;
_delay_us(62);
DDRD &= ~(1 << pin_number);
_delay_us(2);
}
}
struct Tstruct read_temperature_ds18b20(uint8_t pin_number) {
struct Tstruct tempS = {(uint8_t)0, (int8_t)0, (uint8_t)0};
uint8_t Temp_L;
uint8_t Temp_H;
uint8_t NotOK_flag;
uint8_t temp_sign;
int8_t temp_int;
NotOK_flag = DS18B20_init(pin_number);
write_18b20(0xCC, pin_number); // check sensor code
write_18b20(0x44, pin_number); // start temperature conversion
_delay_ms(1000);
NotOK_flag = DS18B20_init(pin_number); // initialization DS18B20
write_18b20(0xCC, pin_number); // check sensor code
write_18b20(0xBE, pin_number); // prepare reading
Temp_L = read_18b20(pin_number); // read the first 2 bytes of scratchpad
Temp_H = read_18b20(pin_number);
temp_sign = 1; // temperature sign flag is 1 (plus)
if(Temp_H &(1 << 3)) { // check the sign bit
signed int tmp = 0;
temp_sign = 0; // sign flag is 0 (minus)
tmp = (Temp_H << 8) | Temp_L;
tmp = -tmp;
Temp_L = tmp;
Temp_H = tmp >> 8;
}
temp_int = (((Temp_H << 4) & 0x70)|(Temp_L >> 4)); // compute the integer value of the temperature
tempS.no_answer = NotOK_flag;
tempS.value = temp_int;
tempS.pos_sign = temp_sign;
return tempS;
}