-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1307.c
106 lines (70 loc) · 1.69 KB
/
ds1307.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
#include "config.h"
#include <avr/io.h>
#include "i2c.h"
#include "ds1307.h"
/***************************************************
Function To Read Internal Registers of DS1307
---------------------------------------------
address : Address of the register
data: value of register is copied to this.
Returns:
0= Failure
1= Success
***************************************************/
uint8_t DS1307Read(uint8_t address,uint8_t *data)
{
uint8_t res; //result
//Start
I2CStart();
//SLA+W (for dummy write to set register pointer)
res=I2CWriteByte(0b11010000); //DS1307 address + W
//Error
if(!res) return FALSE;
//Now send the address of required register
res=I2CWriteByte(address);
//Error
if(!res) return FALSE;
//Repeat Start
I2CStart();
//SLA + R
res=I2CWriteByte(0b11010001); //DS1307 Address + R
//Error
if(!res) return FALSE;
//Now read the value with NACK
res=I2CReadByte(data,0);
//Error
if(!res) return FALSE;
//STOP
I2CStop();
return TRUE;
}
/***************************************************
Function To Write Internal Registers of DS1307
---------------------------------------------
address : Address of the register
data: value to write.
Returns:
0= Failure
1= Success
***************************************************/
uint8_t DS1307Write(uint8_t address,uint8_t data)
{
uint8_t res; //result
//Start
I2CStart();
//SLA+W
res=I2CWriteByte(0b11010000); //DS1307 address + W
//Error
if(!res) return FALSE;
//Now send the address of required register
res=I2CWriteByte(address);
//Error
if(!res) return FALSE;
//Now write the value
res=I2CWriteByte(data);
//Error
if(!res) return FALSE;
//STOP
I2CStop();
return TRUE;
}