/
DS1307.c
executable file
·130 lines (104 loc) · 2.56 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
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
#ifndef _DS1307_C
#define _DS1307_C
#define DS1307_ID 0xD0
//#define SEC 0x00
//#define MINU 0x01
//#define HOUR 0x02
//#define DAT 0x04
//#define MONTH 0x05
//#define YEAR 0x06
#include "DS1307.h"
void initUart(void)
{
uartInit();
// make all rprintf statements use uart for output
rprintfInit(uartSendByte);
// initialize the timer system
uartSetBaudRate(2400);
}
//to initialize the i2c subroutine
void initI2c(void)
{
i2cInit();
i2cSetBitrate(100);
}
//to check the working of ds1307 returns 1 if success else 0
u08 ds1307_check(void)
{
u08 data=0;
return(!i2cMasterSendNI(DS1307_ID, 1, &data));
}
//TO GET THE DATA FROM THE DS1307
unsigned char DS1307_get(unsigned char addr)
{
u08 ret=0;
i2cMasterTransferNI(DS1307_ID, 1, &addr, 1, &ret);
return ret;
}
//TO SET THE TIME
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
u08 data[4];
data[0]=0;data[1]=ss;data[2]=mm;data[3]=hh;
i2cMasterSendNI(DS1307_ID, 4, data);
}
// TO SET THE DATE
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
u08 data[4];
data[0]=4;data[1]=dd;data[2]=mm;data[3]=yy;
i2cMasterSendNI(DS1307_ID, 4, data);
}
// USED TO GET VALUES FROM I2C
void i2cMasterTransferNI(u08 deviceAddr, u08 sendlength, u08* senddata, u08 receivelength, u08* receivedata)
{
// disable TWI interrupt
cbi(TWCR, TWIE);
// send start condition
i2cSendStart();
i2cWaitForComplete();
// if there's data to be sent, do it
if(sendlength)
{
// send device address with write
i2cSendByte( deviceAddr & 0xFE );
i2cWaitForComplete();
// send data
while(sendlength)
{
i2cSendByte( *senddata++ );
i2cWaitForComplete();
sendlength--;
}
}
// if there's data to be received, do it
if(receivelength)
{
// send repeated start condition
i2cSendStart();
i2cWaitForComplete();
// send device address with read
i2cSendByte( deviceAddr | 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();
}
// transmit stop condition
// leave with TWEA on for slave receiving
i2cSendStop();
while( !(inb(TWCR) & BV(TWSTO)) );
// enable TWI interrupt
sbi(TWCR, TWIE);
}
#endif