-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1672.c
110 lines (92 loc) · 1.78 KB
/
ds1672.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
/*! \file ds1672.c
\brief DS1672 RTC Functions
DS1672 RTC Functions
*/
#include "pic.h"
#include "i2c.h"
#define DS1672_WRITE_ADDRESS 0xd0
#define DS1672_READ_ADDRESS 0xd1
#define DS1672_TIMEOUT 500000
char DS1672_Zero(void)
{
char t;
char s;
I2C_Init(90,1000000);
I2C_Start();
t=I2C_TX(0xD0);
if(t!=0) {
if(t==1)
Log_Error((FARROM*)"DS1672: Zero Address NACK\r\n");
if(t==2)
Log_Error((FARROM*)"DS1672: Zero Address Timeout\r\n");
return t;
}
t=I2C_TX(1);
if(t!=0) {
if(t==1)
Log_Error((FARROM*)"DS1672: Zero Postion NACK\r\n");
if(t==2)
Log_Error((FARROM*)"DS1672: Zero Position Position Timeout\r\n");
}
for(s=0;s<6;s++)
{
t=I2C_TX(0);
if(t!=0) {
if(t==1)
Log_Error((FARROM*)"DS1672: TX NACK\r\n");
if(t==2)
Log_Error((FARROM*)"DS1672: TX Timeout\r\n");
}
}
I2C_Stop();
}
unsigned long DS1672_Value(void)
{
unsigned long value;
int t;
unsigned long v;
I2C_Init(100,1000000);
I2C_Start();
t=I2C_TX(0xD1);
if(t!=0) return 0xffffffff;
t=I2C_RX();
if(t==-1) return 0xffffffff;
value=t;
I2C_ACK();
t=I2C_RX();
if(t==-1) return 0xffffffff;
v = t;
value+=v<<8;
I2C_ACK();
t=I2C_RX();
if(t==-1) return 0xffffffff;
v = t;
value+=v<<16;
I2C_ACK();
t=I2C_RX();
if(t==-1) return 0xffffffff;
v = t;
value+=v<<24;
I2C_NACK();
I2C_Stop();
return value;
}
int DS1672_GetNextByte(void)
{
int t;
I2C_Init(100,1000000);
I2C_Start();
t=I2C_TX(DS1672_READ_ADDRESS);
if(t!=0) {
Log_Error((FARROM*)"DS1672: GetNextByte Error sending Address\r\n");
return -1;
}
t=I2C_RX();
if(t==-1) {
Log_Error((FARROM*)"DS1672: GetNextByte Error receiving byte\r\n");
return -1;
}
I2C_NACK();
I2C_Stop();
return t;
}