-
Notifications
You must be signed in to change notification settings - Fork 3
/
onewire.h
124 lines (99 loc) · 2.21 KB
/
onewire.h
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
#ifndef ONEWIRE_H
#define ONEWIRE_H
#include <util/delay.h>
#include "avrlibdefs.h"
// 1-wire connection pin
#define P_DS PA7
#define THERM_CMD_CONVERTTEMP 0x44
#define THERM_CMD_RSCRATCHPAD 0xbe
#define THERM_CMD_WSCRATCHPAD 0x4e
#define THERM_CMD_CPYSCRATCHPAD 0x48
#define THERM_CMD_RECEEPROM 0xb8
#define THERM_CMD_RPWRSUPPLY 0xb4
#define THERM_CMD_SEARCHROM 0xf0
#define THERM_CMD_READROM 0x33
#define THERM_CMD_MATCHROM 0x55
#define THERM_CMD_SKIPROM 0xcc
#define THERM_CMD_ALARMSEARCH 0xec
#define MAX_DEVICES 2
// reset 1-wire network and determine the presence of any 1-wire slave device
u08 ow_reset(void)
{
u08 pr, sreg;
sreg = SREG; cli();
cbi(PORTA, P_DS);
sbi(DDRA, P_DS);
_delay_us(480);
cbi(DDRA, P_DS);
_delay_us(70);
pr = !(inb(PINA) & (1<<P_DS));
SREG = sreg;
_delay_us(410);
return(pr);
}
// write bit to 1-wire network
static inline void write_bit(u08 bitval)
{
u08 sreg;
sreg= SREG; cli();
cbi(PORTA, P_DS);
sbi(DDRA, P_DS);
_delay_us(10);
if(bitval) cbi(DDRA, P_DS);
_delay_us(70);
cbi(DDRA, P_DS);
SREG = sreg;
}
// write byte to 1-wire network
void write_byte(u08 val)
{
for(u08 i = 0; i < 8; i++)
write_bit( (val>>i) & 0b01 );
sbi(PORTA, P_DS); // raise the pin level immediately (for parasite power feed)
sbi(DDRA, P_DS);
_delay_us(120);
}
// read bit from 1-wire network
static inline u08 read_bit(void)
{
u08 sreg, r;
sreg = SREG; cli();
cbi(PORTA, P_DS);
sbi(DDRA, P_DS);
nop();nop();nop();nop();nop();nop();
cbi(DDRA, P_DS);
_delay_us(10);
r = inb(PINA) & (1<<P_DS);
SREG = sreg;
return r;
}
// read byte from 1-wire network
u08 read_byte(void)
{
u08 value = 0;
for(u08 i = 0; i < 8; i++)
{
if(read_bit()) value |= (0b01 << i);
_delay_us(120);
}
return(value);
}
// CRC8 calculation
u08 calc_crc(u08* buff, u08 num_vals)
{
u08 shift_reg = 0, data_bit, sr_lsb, fb_bit;
for(u08 i = 0; i < num_vals; i++)
{
for(u08 j = 0; j < 8; j++)
{
data_bit = (*buff >> j) & 0b01;
sr_lsb = shift_reg & 0b01;
fb_bit = (data_bit ^ sr_lsb) & 0b01;
shift_reg = shift_reg >> 1;
if(fb_bit) shift_reg = shift_reg ^ 0x8C;
}
buff++;
}
return(shift_reg);
}
#endif