-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTCC.c
More file actions
56 lines (48 loc) · 1.2 KB
/
RTCC.c
File metadata and controls
56 lines (48 loc) · 1.2 KB
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
/* Author: Steve Gunn
* Licence: This work is licensed under the Creative Commons Attribution License.
* View this license at http://creativecommons.org/about/licenses/
*/
#include "i2c.h"
#include "RTCC.h"
void init_clock(void)
{
/* TODO: Code to initiliase interface and start clock */
init_i2c_master();
i2c_start();
i2c_tx(RTCC_ADDR | I2C_WRITE);
i2c_tx(0x00);
i2c_tx(1<<7);
i2c_stop();
}
void set_time(rtcc t)
{
/* TODO: Code to set time to value in t */
i2c_start();
i2c_tx(RTCC_ADDR | I2C_WRITE);
i2c_tx(0x00);
i2c_tx(1<<7 | t.ten_seconds << 4 | t.seconds);
i2c_tx(t.ten_minutes << 4 | t.minutes);
i2c_tx(t.ten_hours << 4 | t.hours);
i2c_stop();
}
rtcc get_time()
{
/* TODO: Code to get time from RTCC and return it in an rtcc struct */
rtcc now;
uint8_t rtcc_mem[8];
i2c_start();
i2c_tx(RTCC_ADDR | I2C_WRITE);
i2c_tx(0x00);
i2c_start();
i2c_tx(RTCC_ADDR | I2C_READ);
for (int i=0; i<7; i++){
rtcc_mem[i]=i2c_rx_ack();
}
rtcc_mem[7]=i2c_rx_nack();
i2c_stop();
now.seconds=rtcc_mem[0] & 0x0F;
now.ten_seconds= (rtcc_mem[0] & 0x70) >> 4;
now.ten_hours = (rtcc_mem[2] & 0x30) >> 4;
now.day=rtcc_mem[3] & 0x07;
now.ten_date=(rtcc_mem[4] & 0x30) >> 4;
}