-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1721.c
112 lines (88 loc) · 1.98 KB
/
ds1721.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
#include <util/twi.h>
#include "common.h"
#include "ds1721.h"
#include "task.h"
void i2c_init()
{
TWSR = 0x00;
// TWBR = 1; // (F_CPU / 100000UL - 16) / 2;
TWBR = 47;
TWCR = _BV(TWEN);
}
void i2c_wait_finish(void)
{
// loop_until_bit_is_set(TWCR, TWINT);
while (!(TWCR & (1 << TWINT)));
}
void i2c_send_start(void)
{
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
i2c_wait_finish();
}
void i2c_send_stop(void)
{
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
// loop_until_bit_is_clear(TWCR, TWSTO);
while (TWCR & (1 << TWSTO));
}
void i2c_send_byte(unsigned char c)
{
TWDR = c;
TWCR = _BV(TWINT) | _BV(TWEN);
i2c_wait_finish();
}
unsigned char i2c_read_byte(unsigned char ACK)
{
TWCR = _BV(TWINT) | _BV(TWEN) | (ACK << TWEA);
loop_until_bit_is_set(TWCR, TWINT);
TWCR = _BV(TWINT) | _BV(TWEN);
return TWDR;
}
void ds1721_init()
{
i2c_init();
i2c_send_start();
i2c_send_byte(DS1721_ADDR | DS1721_WRITE);
i2c_send_byte(DS1721_CMD_INIT);
i2c_send_stop();
_delay_us(10);
}
float ds1721_read()
{
unsigned char hi, lo;
unsigned char i;
short tmp = 1;
float rev;
float frac[] = { 0.0625, 0.125, 0.25, 0.5 };
i2c_send_start();
i2c_send_byte(DS1721_ADDR | DS1721_WRITE);
i2c_send_byte(DS1721_CMD_READ);
i2c_send_start(); /* restart */
i2c_send_byte(DS1721_ADDR | DS1721_READ);
hi = i2c_read_byte(1);
lo = i2c_read_byte(1);
// _delay_us(70); /* WHAT ?! */
// i2c_send_stop();
// task_sm.piezo0_count = 5;
////////////////
////////////////
if (hi & 0x80)
{ /* negative */
tmp = (hi << 8) + lo;
tmp = ~tmp;
tmp++;
hi = tmp >> 8;
lo = tmp & 0x00ff;
tmp = -1;
}
rev = hi;
lo >>= 4;
for (i=0; i<4; i++)
{
if (lo & _BV(i))
{
rev += frac[i];
}
}
return rev * tmp;
}