-
Notifications
You must be signed in to change notification settings - Fork 11
/
DS18B20.c
102 lines (89 loc) · 2.18 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
/*
* DS18B20.c
*
* Created on: Dec 30, 2015
* Author: alex
*/
#include "sys.h"
#include "DS18B20.h"
#include "delay.h"
#include <math.h>
unsigned char DS18B20_data[DATA_SIZE];
#define DQ P15
//Initialization function
u8 DS18B20_reset(void) {
u8 x = 0;
DQ = 1; // Release
delay_5us(5); //Slight delay
DQ = 0; // drive bus low
delay_5us(120); // wait 480us-960us delay
DQ = 1; // Release bus
delay_5us(15); // wait 70us
x = DQ; // 0 = device present, 1 = no device present
delay_5us(20); // delay 410us
return x;
}
//Reading a byte
unsigned char ReadOneChar(void) {
unsigned char i = 0;
unsigned char dat = 0;
for (i = 8; i > 0; i--) {
DQ = 0;
delay_5us(1);
DQ = 1;
delay_5us(2);
dat >>= 1;
if (DQ)
dat |= 0x80;
delay_5us(11);
}
return (dat);
}
//Write a byte
void WriteOneChar(unsigned char d) {
unsigned char i = 0;
for (i = 8; i > 0; i--) {
if (d & 1) {
DQ = 0;
delay_5us(2);
DQ = 1;
delay_5us(10);
} else {
DQ = 0;
delay_5us(10);
DQ = 1;
delay_5us(2);
}
d >>= 1;
}
// delay_5us(4);
}
//Read temperature
u8 DS18B20_readTemp(void)
{
u8 x = 0,
i;
x |= DS18B20_reset();
WriteOneChar(0xCC); // Skip read serial number column number of operations
WriteOneChar(0x44); // Start temperature conversion
x |= DS18B20_reset() << 4;
WriteOneChar(0xCC); //Skip read serial number column number of operations
WriteOneChar(0xBE); //Read the temperature register, etc. (a total of 9 registers readable) is the temperature of the first two
for (i=0; i<DATA_SIZE; i++)
DS18B20_data[i]=ReadOneChar();
return x;
}
float DS18B20_decodeTemp() {
float temp = 0;
int i;
for (i = 0; i < 8; i++) {
temp += ((DS18B20_data[0] >> i)&0x01) * powf(2, i - 4);
}
for (i = 0; i < 3; i++) {
temp = temp + ((DS18B20_data[1] >> i)&0x01) * powf(2, i + 4);
}
if ((DS18B20_data[1] >> 3)&0x01) {
temp = -temp;
}
return temp;
}