-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTC.c
73 lines (61 loc) · 1.25 KB
/
RTC.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
/*
* File: RTC.c
* Author: palan
*
* Created on December 26, 2023, 5:12 PM
*/
#include "RTC.h"
#include "i2c.h"
#include <xc.h>
/*
* DS1307 Slave address
* D0 - Write Mode
* D1 - Read Mode
*/
void init_ds1307(void)
{
unsigned char dummy;
/* Setting the CH bit of the RTC to Stop the Clock */
dummy = read_ds1307(SEC_ADDR);
write_ds1307(SEC_ADDR, dummy | 0x80);
/* Seting 12 Hr Format */
dummy = read_ds1307(HOUR_ADDR);
write_ds1307(HOUR_ADDR, dummy & 0xBF);
/*
* Control Register of DS1307
* Bit 7 - OUT
* Bit 6 - 0
* Bit 5 - OSF
* Bit 4 - SQWE
* Bit 3 - 0
* Bit 2 - 0
* Bit 1 - RS1
* Bit 0 - RS0
*
* Seting RS0 and RS1 as 11 to achive SQW out at 32.768 KHz
*/
write_ds1307(CNTL_ADDR, 0x93);
/* Clearing the CH bit of the RTC to Start the Clock */
dummy = read_ds1307(SEC_ADDR);
write_ds1307(SEC_ADDR, dummy & 0x7F);
}
void write_ds1307(unsigned char address, unsigned char data)
{
i2c_start();
i2c_write(SLAVE_WRITE_RTC);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
unsigned char read_ds1307(unsigned char address)
{
unsigned char data;
i2c_start();
i2c_write(SLAVE_WRITE_RTC);
i2c_write(address);
i2c_rep_start();
i2c_write(SLAVE_READ_RTC);
data = i2c_read();
i2c_stop();
return data;
}