-
Notifications
You must be signed in to change notification settings - Fork 0
/
oneWire.c
77 lines (65 loc) · 1.44 KB
/
oneWire.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
#include "mcc_generated_files/mcc.h"
#include "oneWire.h"
void ow_init() {
DQ_SetDigitalOutput();
DQ_SetDigitalMode();
DQ_SetPullup();
DQ_SetPushPull();
}
owByte ow_reset() {
owByte presence = OW_FALSE;
DQ_SetLow();
OW_DELAY_US(OW_RST_PULSE);
DQ_SetHigh();
OW_DELAY_US(OW_PRES_DETECT_HIGH);
presence = DQ_GetValue();
OW_DELAY_US(OW_PRES_DETECT_LOW);
return !presence;
}
owByte ow_readBit() {
DQ_SetLow();
OW_DELAY_US(1);
DQ_SetHigh();
OW_DELAY_US(OW_READ_DATA_VALID);
return DQ_GetValue();
}
void ow_writeBit(owByte bitval) {
DQ_SetLow();
OW_DELAY_US(1);
if(bitval==1) DQ_SetHigh();
OW_DELAY_US(OW_TIME_SLOT);
DQ_SetHigh();
}
owByte ow_readByte() {
owByte i;
owByte result = 0;
for(i=0; i<8 ; i++) {
if(ow_readBit())
result = result | (0x01 << i);
OW_DELAY_US(OW_TIME_SLOT);
}
return result;
}
void ow_writeByte(owByte val) {
owByte i;
owByte temp;
for(i=0; i<8 ; i++) {
temp = val >> i;
temp = temp & 0x01;
ow_writeBit(temp);
}
}
owByte crc8(owByte *addr, owByte len) {
owByte i, crc, inbyte, mix;
crc = 0;
while(len--) {
inbyte = *addr++;
for(i=8 ; i ; i--) {
mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if(mix) crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}