/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
67 by Tim Marston
added realtime clock test
1
#include <../Wire/Wire.h>
2
#include "DS1307.h"
3
4
DS1307::DS1307()
5
{
6
  Wire.begin();
7
}
8
9
DS1307 RTC=DS1307();
10
11
// PRIVATE FUNCTIONS
12
13
// Aquire data from the RTC chip in BCD format
14
// refresh the buffer
15
void DS1307::read(void)
16
{
17
  // use the Wire lib to connect to tho rtc
18
  // reset the resgiter pointer to zero
19
  Wire.beginTransmission(DS1307_CTRL_ID);
20
  Wire.write( (unsigned char )0 );
21
  Wire.endTransmission();
22
23
  // request the 7 bytes of data (secs, min, hr, dow, day, mth, yr)
24
  Wire.requestFrom(DS1307_CTRL_ID, 7);
25
  for(int i=0; i<7; i++)
26
  {
27
    // store data in raw bcd format
28
    rtc_bcd[i]=Wire.read();
29
  }
30
}
31
32
// update the data on the IC from the bcd formatted data in the buffer
33
void DS1307::save(void)
34
{
35
  Wire.beginTransmission(DS1307_CTRL_ID);
36
  Wire.write( (unsigned char)0 ); // reset register pointer
37
  for(int i=0; i<7; i++)
38
  {
39
    Wire.write(rtc_bcd[i]);
40
  }
41
  Wire.endTransmission();
42
}
43
44
45
// PUBLIC FUNCTIONS
46
void DS1307::get(int *rtc, boolean refresh)   // Aquire data from buffer and convert to int, refresh buffer if required
47
{
48
  if(refresh) read();
49
  for(int i=0;i<7;i++)  // cycle through each component, create array of data
50
  {
51
	rtc[i]=get(i, 0);
52
  }
53
}
54
55
int DS1307::get(int c, boolean refresh)  // aquire individual RTC item from buffer, return as int, refresh buffer if required
56
{
57
  if(refresh) read();
58
  int v=-1;
59
  switch(c)
60
  {
61
  case DS1307_SEC:
62
    v=(10*((rtc_bcd[DS1307_SEC] & DS1307_HI_SEC)>>4))+(rtc_bcd[DS1307_SEC] & DS1307_LO_BCD);
63
	break;
64
  case DS1307_MIN:
65
    v=(10*((rtc_bcd[DS1307_MIN] & DS1307_HI_MIN)>>4))+(rtc_bcd[DS1307_MIN] & DS1307_LO_BCD);
66
	break;
67
  case DS1307_HR:
68
    v=(10*((rtc_bcd[DS1307_HR] & DS1307_HI_HR)>>4))+(rtc_bcd[DS1307_HR] & DS1307_LO_BCD);
69
	break;
70
  case DS1307_DOW:
71
    v=rtc_bcd[DS1307_DOW] & DS1307_LO_DOW;
72
	break;
73
  case DS1307_DAY:
74
    v=(10*((rtc_bcd[DS1307_DAY] & DS1307_HI_DAY)>>4))+(rtc_bcd[DS1307_DAY] & DS1307_LO_BCD);
75
	break;
76
  case DS1307_MTH:
77
    v=(10*((rtc_bcd[DS1307_MTH] & DS1307_HI_MTH)>>4))+(rtc_bcd[DS1307_MTH] & DS1307_LO_BCD);
78
	break;
79
  case DS1307_YR:
80
    v=(10*((rtc_bcd[DS1307_YR] & DS1307_HI_YR)>>4))+(rtc_bcd[DS1307_YR] & DS1307_LO_BCD)+DS1307_BASE_YR;
81
	break;
82
  } // end switch
83
  return v;
84
}
85
86
void DS1307::set(int c, int v)  // Update buffer, then update the chip
87
{
88
  switch(c)
89
  {
90
  case DS1307_SEC:
91
    if(v<60 && v>-1)
92
    {
93
	//preserve existing clock state (running/stopped)
94
	int state=rtc_bcd[DS1307_SEC] & DS1307_CLOCKHALT;
95
	rtc_bcd[DS1307_SEC]=state | (((v / 10)<<4) + (v % 10));
96
    }
97
    break;
98
  case DS1307_MIN:
99
    if(v<60 && v>-1)
100
    {
101
	rtc_bcd[DS1307_MIN]=((v / 10)<<4) + (v % 10);
102
    }
103
    break;
104
  case DS1307_HR:
105
  // TODO : AM/PM  12HR/24HR
106
    if(v<24 && v>-1)
107
    {
108
	rtc_bcd[DS1307_HR]=((v / 10)<<4) + (v % 10);
109
    }
110
    break;
111
  case DS1307_DOW:
112
    if(v<8 && v>-1)
113
    {
114
	rtc_bcd[DS1307_DOW]=v;
115
    }
116
    break;
117
  case DS1307_DAY:
118
    if(v<31 && v>-1)
119
    {
120
	rtc_bcd[DS1307_DAY]=((v / 10)<<4) + (v % 10);
121
    }
122
    break;
123
  case DS1307_MTH:
124
    if(v<13 && v>-1)
125
    {
126
	rtc_bcd[DS1307_MTH]=((v / 10)<<4) + (v % 10);
127
    }
128
    break;
129
  case DS1307_YR:
130
    if(v<13 && v>-1)
131
    {
132
	rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
133
    }
134
    break;
135
  } // end switch
136
  save();
137
}
138
139
void DS1307::stop(void)
140
{
141
	// set the ClockHalt bit high to stop the rtc
142
	// this bit is part of the seconds byte
143
    rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] | DS1307_CLOCKHALT;
144
    save();
145
}
146
147
void DS1307::start(void)
148
{
149
	// unset the ClockHalt bit to start the rtc
150
	// this bit is part of the seconds byte
151
    rtc_bcd[DS1307_SEC]=rtc_bcd[DS1307_SEC] & ~DS1307_CLOCKHALT;
152
	save();
153
}