-
Notifications
You must be signed in to change notification settings - Fork 0
/
DS1624.c
executable file
·137 lines (116 loc) · 3.89 KB
/
DS1624.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef _DS1624_C
#define _DS1624_C
#define Addr1624 0x90
#include "DS1624.h"
/*
uC DS1624 DATA(MSB FIRST) COMMENTS
TX RX START Bus Master Initiates a Start condition.
TX RX Addr Bus Master sends DS1624 address; R/W =”0”;
RX TX ACK DS1624 generates acknowledge bit.
TX RX ACh Bus Master sends Access Config command protocol.
RX TX ACK DS1624 generates acknowledge bit. 1
TX RX 00h Bus Master sets up DS1624 for continuous conversion.
RX TX ACK DS1624 generates acknowledge bit. 2, 4
TX RX STOP Bus Master initiates the STOP condition.
*/
void ds1624_config(void)
{
u08 data[2];
data[0]=0xAC;data[1]=0x00;
i2cMasterSendNI(Addr1624, 2, data);
}
/*
uC DS1624 DATA(MSB FIRST) COMMENTS
TX RX START Bus Master initiates a Start condition.
TX RX Addr Bus Master sends DS1624 address;R/W =0;
RX TX ACK DS1624 generates acknowledge bit.
TX RX EEh Bus Master sends Start Convert Tcommand protocol.
RX TX ACK DS1624 generates acknowledge bit. 1
TX RX STOP Bus Master initiates the STOP condition.
*/
void ds1624_startconvert(void)
{
u08 data[1];
data[0]=0xEE;
i2cMasterSendNI(Addr1624, 1, data);
}
// used to format and print temperature value
u16 ds1624_readtemp(void)
{
u08 ret[2];
s16 T;
readtemp(Addr1624, 1, 2, ret);
T = (s16)((ret[0]<<8) | ret[1]);
return T;
/*rprintf("Temp is: ");
rprintfNum(10, 4, FALSE , ' ', T>>8);
rprintf(".");
rprintfNum(10, 4, FALSE, '0', (10000*((u32)(T&0x00FF)))/256 );*/
}
// to check if DS1624 is acknowledging or not
u08 ds1624_check(void)
{
u08 data=0;
return(!i2cMasterSendNI(Addr1624, 1, &data));
}
/*
uC DS1624 DATA(MSB FIRST) COMMENTS
TX RX START Bus Master initiates a Start condition.
TX RX Addr Bus Master sends DS1624 address;R/W =0;
RX TX ACK DS1624 generates acknowledge bit.
TX RX AAh Bus Master sends Read Temp commandprotocol.
RX TX ACK DS1624 generates acknowledge bit. 1
TX RX START Bus Master initiates a Repeated Startcondition.
TX RX Addr Bus Master sends DS1624 address;R/W =1;
RX TX ACK DS1624 generates acknowledge bit.
RX TX <data> DS1624 sends the MSB byte ofTemperature.
TX RX ACK Bus Master generates acknowledge bit.
RX TX <data> DS1624 sends the LSB byte ofTemperature.
TX RX NACK Bus Master sends “NO ACKNOWLEDGE” bit.
TX RX STOP Bus Master initiates the STOP condition.
*/
void readtemp(u08 deviceAddr, u08 sendlength,u08 receivelength, u08* receivedata)
{
// disable TWI interrupt
cbi(TWCR, TWIE);
// send start condition
i2cSendStart();
i2cWaitForComplete();
// if there's data to be sent, do it
// send device address with write
i2cSendByte( Addr1624 & 0xFE );
i2cWaitForComplete();
i2cSendByte(0xAA);
i2cWaitForComplete();
// if there's data to be received, do it
if(receivelength)
{
// send repeated start condition
i2cSendStart();
i2cWaitForComplete();
// send device address with read
i2cSendByte( Addr1624 | 0x01 );
i2cWaitForComplete();
// accept receive data and ack it
while(receivelength > 1)
{
i2cReceiveByte(TRUE);
i2cWaitForComplete();
*receivedata++ = i2cGetReceivedByte();
// decrement length
receivelength--;
}
// accept receive data and nack it (last-byte signal)
i2cReceiveByte(FALSE);
i2cWaitForComplete();
*receivedata++ = i2cGetReceivedByte();
//rprintfu08(*receivedata);
}
// transmit stop condition
// leave with TWEA on for slave receiving
i2cSendStop();
while( !(inb(TWCR) & BV(TWSTO)) );
// enable TWI interrupt
sbi(TWCR, TWIE);
}
#endif