View difference between Paste ID: bMQ9sC5r and DVMTrbWv
SHOW: | | - or go back to the newest paste.
1-
// DS1302 RTC
1+
2-
// ----------
2+
3
int pin = 2;
4-
// By arduino.cc user "Krodal".
4+
5-
// June 2012
5+
6-
// Open Source / Public Domain
6+
7
8-
// Using Arduino 1.0.1
8+
9
10-
// Documentation: datasheet
10+
11
#define DS1302_SCLK 6    // Arduino pin for the Serial Clock
12
#define DS1302_IO   7    // Arduino pin for the Data I/O
13-
// The DS1302 uses a 3-wire interface:
13+
14-
//    - bidirectional data.
14+
15-
//    - clock
15+
16-
//    - chip select
16+
17-
// It is not I2C, not OneWire, and not SPI.
17+
18-
// So the standard libraries can not be used.
18+
19-
// Even the shiftOut() function is not used, since it
19+
20-
// could be too fast (it might be slow enough,
20+
21-
// but that's not certain).
21+
22
#define DS1302_MINUTES           0x82
23-
// I wrote my own interface code according to the datasheet.
23+
24-
// Any three pins of the Arduino can be used.
24+
25-
//   See the first defines below this comment,
25+
26-
//   to set your own pins.
26+
27
#define DS1302_YEAR              0x8C
28-
// The "Chip Enable" pin was called "/Reset" before.
28+
29
#define DS1302_TRICKLE           0x90
30-
// The chip has internal pull-down registers.
30+
31-
// This keeps the chip disabled, even if the pins of
31+
32-
// the Arduino are floating.
32+
33
#define DS1302_RAMSTART          0xC0
34
#define DS1302_RAMEND            0xFC
35-
// Range
35+
36-
// -----
36+
37-
//      seconds : 00-59
37+
38-
//      minutes : 00-59
38+
39-
//      hour    : 1-12 or 0-23
39+
40-
//      date    : 1-31
40+
41-
//      month   : 1-12
41+
42-
//      day     : 1-7
42+
43-
//      year    : 00-99
43+
44
// is like programming an AVR microcontroller.
45
// But instead of using "(1<<X)", or "_BV(X)",
46-
// Burst mode
46+
47-
// ----------
47+
48-
// In burst mode, all the clock data is read at once.
48+
49-
// This is to prevent a rollover of a digit during reading.
49+
50-
// The read data is from an internal buffer.
50+
51
#define DS1302_D4 4
52-
// The burst registers are commands, rather than addresses.
52+
53-
// Clock Data Read in Burst Mode
53+
54-
//    Start by writing 0xBF (as the address),
54+
55-
//    after that: read clock data
55+
56-
// Clock Data Write in Burst Mode
56+
57-
//    Start by writing 0xBE (as the address),
57+
58-
//    after that: write clock data
58+
59-
// Ram Data Read in Burst Mode
59+
60-
//    Start by writing 0xFF (as the address),
60+
61-
//    after that: read ram data
61+
62-
// Ram Data Write in Burst Mode
62+
63-
//    Start by writing 0xFE (as the address),
63+
64-
//    after that: write ram data
64+
65
#define DS1302_CH DS1302_D7   // 1 = Clock Halt, 0 = start
66
67-
// Ram
67+
68-
// ---
68+
69-
// The DS1302 has 31 of ram, which can be used to store data.
69+
70-
// The contents will be lost if the Arduino is off,
70+
71-
// and the backup battery gets empty.
71+
72-
// It is better to store data in the EEPROM of the Arduino.
72+
73-
// The burst read or burst write for ram is not implemented
73+
74-
// in this code.
74+
75
#define DS1302_ROUT0 DS1302_D0
76
#define DS1302_ROUT1 DS1302_D1
77-
// Trickle charge
77+
78-
// --------------
78+
79-
// The DS1302 has a build-in trickle charger.
79+
80-
// That can be used for example with a lithium battery
80+
81-
// or a supercap.
81+
82-
// Using the trickle charger has not been implemented
82+
83-
// in this code.
83+
84
85
// Structure for the first 8 registers.
86
// These 8 bytes can be read at once with
87
// the 'clock burst' command.
88
// Note that this structure contains an anonymous union.
89
// It might cause a problem on other compilers.
90
typedef struct ds1302_struct
91
{
92
uint8_t Seconds:
93
  4;      // low decimal digit 0-9
94
uint8_t Seconds10:
95
  3;    // high decimal digit 0-5
96
uint8_t CH:
97
  1;           // CH = Clock Halt
98
uint8_t Minutes:
99
  5;
100
uint8_t Minutes10:
101
  2;
102
uint8_t reserved1:
103
  1;
104
  union
105
  {
106
    struct
107
    {
108
uint8_t Hour:
109
      4;
110
uint8_t Hour10:
111
      1;
112
uint8_t reserved2:
113
      1;
114
uint8_t hour_12_24:
115
      1; // 0 for 24 hour format
116
    } 
117
    h24;
118
    struct
119
    {
120
uint8_t Hour:
121
      4;
122
uint8_t Hour10:
123
      1;
124
uint8_t AM_PM:
125
      1;      // 0 for AM, 1 for PM
126
uint8_t reserved2:
127
      1;
128
uint8_t hour_12_24:
129
      1; // 1 for 12 hour format
130
    } 
131
    h12;
132
  };
133
uint8_t Date:
134
  4;
135
uint8_t Date10:
136
  2;
137
uint8_t reserved3:
138
  2;
139
uint8_t Month:
140
  4;
141
uint8_t Month10:
142
  1;
143
uint8_t reserved4:
144
  3;
145
uint8_t Day:
146
  3;
147
uint8_t reserved5:
148
  5;
149
uint8_t Year:
150
  4;
151
uint8_t Year10:
152
  4;
153
uint8_t reserved6:
154
  7;
155
uint8_t WP:
156
  1;             // WP = Write Protect
157
};
158
159
160
// global variables
161
volatile byte increaseHour = 0;
162
volatile byte increaseHour10 = 0;
163
uint32_t lastMillis = 0;
164
165
166
void setup()
167
{      
168
  ds1302_struct rtc;
169
170
171
  Serial.begin(9600);
172
  Serial.println(F("DS1302 Real Time Clock"));
173
  Serial.println(F("June 2012"));
174
175
176
  // Start by clearing the Write Protect bit
177
  // Otherwise the clock data cannot be written
178
  // The whole register is written,
179
  // but the WP-bit is the only bit in that register.
180
  DS1302_write (DS1302_ENABLE, 0);
181
182
  // Disable Trickle Charger.
183
  DS1302_write (DS1302_TRICKLE, 0x00);
184
185
  // Remove the next define,
186
  // after the right date and time are set.
187
#define SET_DATE_TIME_JUST_ONCE
188
#ifdef SET_DATE_TIME_JUST_ONCE  
189
  // Set a time and date
190
  // This also clears the CH (Clock Halt) bit,
191
  // to start the clock.
192
193
  // Fill the structure with zeros to make
194
  // any unused bits zero
195
  memset ((char *) &rtc, 0, sizeof(rtc));
196
197
  rtc.Seconds = 0;
198
  rtc.Seconds10 = 0;
199
  rtc.CH = 0;        // 1 for Clock Halt, 0 to run;
200
  rtc.Minutes = 0;
201
  rtc.Minutes10 = 0;
202
  rtc.h24.Hour = 0;
203
  rtc.h24.Hour10 = 0;
204
  rtc.h24.hour_12_24 = 0; // 0 for 24 hour format
205
  rtc.Date = 0;
206
  rtc.Date10 = 0;
207
  rtc.Month = 0;
208
  rtc.Month10 = 0;
209
  rtc.Day = 0;
210
  rtc.Year = 0;
211
  rtc.Year10 = 0;
212
  rtc.WP = 0;  
213
214
  // Write all clock data at once (burst mode).
215
  DS1302_clock_burst_write( (uint8_t *) &rtc);
216
#endif
217
218
219
//interrupt voor knop
220
pinMode(pin, INPUT);
221
//attachInterrupt(0, uurErbij, RISING);
222
223
}
224
225
226
void loop(){
227
  
228
  ds1302_struct rtc;
229
  char buffer[80];     // the code uses 70 characters.
230
 
231
  if (millis() - lastMillis > 1000) {
232
    lastMillis = millis();
233
    // Read all clock data at once (burst mode).
234
    DS1302_clock_burst_read( (uint8_t *) &rtc);
235
 
236
    sprintf(buffer, "Time = %02d:%02d:%02d, ", \
237
      (rtc.h24.Hour10 * 10) + rtc.h24.Hour, \
238
      (rtc.Minutes10 * 10) + rtc.Minutes, \
239
      (rtc.Seconds10 * 10) + rtc.Seconds);
240
    Serial.print(buffer);
241
 
242
    sprintf(buffer, "Date(day of month) = %d, Month = %d, " \
243
      "Day(day of week) = %d, Year = %d", \
244
      (rtc.Date10 * 10) + rtc.Date, \
245
      (rtc.Month10 * 10) + rtc.Month, \
246
      rtc.Day, \
247
      2000 + (rtc.Year10 * 10) + rtc.Year);
248
    Serial.println(buffer);
249
  }
250
  if (increaseHour) {
251
    DS1302_clock_burst_read( (uint8_t *) &rtc);
252
    rtc.h24.Hour++;
253
    DS1302_clock_burst_write( (uint8_t *) &rtc);
254
    increaseHour = 0;
255
  }
256
  
257
    if (increaseHour10) {
258
    DS1302_clock_burst_read( (uint8_t *) &rtc);
259
    rtc.h24.Hour10++;
260
    rtc.h24.Hour=0;
261
262
    DS1302_clock_burst_write( (uint8_t *) &rtc);
263
    increaseHour10 = 0;
264
  }
265
266
  
267
  // sfqsfqs
268
  
269
   int value = digitalRead(pin);
270
   //uur = rtc.h24.Hours;
271
272
  if(value != previousValue){
273
274
    if (rtc.h24.Hour == 9) {
275
    Serial.println("sdqf");
276
  }
277
      Serial.println(value);
278
      if(value){
279
       uurErbij();
280
 }
281
  };
282
  previousValue = value;
283
  
284
285
}
286
287
288
// --------------------------------------------------------
289
// DS1302_clock_burst_read
290
//
291
// This function reads 8 bytes clock data in burst mode
292
// from the DS1302.
293
//
294
// This function may be called as the first function,
295
// also the pinMode is set.
296
//
297
void DS1302_clock_burst_read( uint8_t *p)
298
{
299
  int i;
300
301
  _DS1302_start();
302
303
  // Instead of the address,
304
  // the CLOCK_BURST_READ command is issued
305
  // the I/O-line is released for the data
306
  _DS1302_togglewrite( DS1302_CLOCK_BURST_READ, true);  
307
308
  for (i=0; i<8; i++)
309
  {
310
    *p++ = _DS1302_toggleread();
311
  }
312
  _DS1302_stop();
313
}
314
315
316
// --------------------------------------------------------
317
// DS1302_clock_burst_write
318
//
319
// This function writes 8 bytes clock data in burst mode
320
// to the DS1302.
321
//
322
// This function may be called as the first function,
323
// also the pinMode is set.
324
//
325
void DS1302_clock_burst_write( uint8_t *p)
326
{
327
  int i;
328
329
  _DS1302_start();
330
331
  // Instead of the address,
332
  // the CLOCK_BURST_WRITE command is issued.
333
  // the I/O-line is not released
334
  _DS1302_togglewrite( DS1302_CLOCK_BURST_WRITE, false);  
335
336
  for (i=0; i<8; i++)
337
  {
338
    // the I/O-line is not released
339
    _DS1302_togglewrite( *p++, false);  
340
  }
341
  _DS1302_stop();
342
}
343
344
345
// --------------------------------------------------------
346
// DS1302_read
347
//
348
// This function reads a byte from the DS1302
349
// (clock or ram).
350
//
351
// The address could be like "0x80" or "0x81",
352
// the lowest bit is set anyway.
353
//
354
// This function may be called as the first function,
355
// also the pinMode is set.
356
//
357
uint8_t DS1302_read(int address)
358
{
359
  uint8_t data;
360
361
  // set lowest bit (read bit) in address
362
  bitSet (address, DS1302_READBIT);  
363
364
  _DS1302_start();
365
  // the I/O-line is released for the data
366
  _DS1302_togglewrite (address, true);  
367
  data = _DS1302_toggleread ();
368
  _DS1302_stop();
369
370
  return (data);
371
}
372
373
374
// --------------------------------------------------------
375
// DS1302_write
376
//
377
// This function writes a byte to the DS1302 (clock or ram).
378
//
379
// The address could be like "0x80" or "0x81",
380
// the lowest bit is cleared anyway.
381
//
382
// This function may be called as the first function,
383
// also the pinMode is set.
384
//
385
void DS1302_write(int address, uint8_t data)
386
{
387
  // clear lowest bit (read bit) in address
388
  bitClear (address, DS1302_READBIT);  
389
390
  _DS1302_start();
391
  // don't release the I/O-line
392
  _DS1302_togglewrite (address, false);
393
  // don't release the I/O-line
394
  _DS1302_togglewrite (data, false);
395
  _DS1302_stop();  
396
}
397
398
399
// --------------------------------------------------------
400
// _DS1302_start
401
//
402
// A helper function to setup the start condition.
403
//
404
// I don't use an 'init' function.
405
// But now the pinMode is set every time.
406
// That's not a big deal, and it's valid.
407
void _DS1302_start(void)
408
{
409
  digitalWrite (DS1302_CE, LOW); // default, not enabled
410
  pinMode (DS1302_CE, OUTPUT);  
411
412
  digitalWrite (DS1302_SCLK, LOW); // default, clock low
413
  pinMode (DS1302_SCLK, OUTPUT);
414
415
  pinMode (DS1302_IO, OUTPUT);
416
417
  digitalWrite (DS1302_CE, HIGH); // start the session
418
  delayMicroseconds(4);           // tCC = 4us
419
}
420
421
422
// --------------------------------------------------------
423
// _DS1302_stop
424
//
425
// A helper function to finish the communication.
426
//
427
void _DS1302_stop(void)
428
{
429
  // Set CE low
430
  digitalWrite (DS1302_CE, LOW);
431
432
  delayMicroseconds(4);           // tCWH = 4us
433
}
434
435
436
// --------------------------------------------------------
437
// _DS1302_toggleread
438
//
439
// A helper function for reading a byte with bit toggle
440
//
441
// This function assumes that the SCLK is still high.
442
//
443
uint8_t _DS1302_toggleread(void)
444
{
445
  uint8_t i, data;
446
447
  data = 0;
448
  for (i = 0; i <= 7; i++)
449
  {
450
    // Issue a clock pulse for the next databit.
451
    // If the 'togglewrite' function was used before
452
    // this function, the SCLK is already high.
453
    digitalWrite (DS1302_SCLK, HIGH);
454
    delayMicroseconds(1);
455
456
    // Clock down, data is ready after some time.
457
    digitalWrite (DS1302_SCLK, LOW);
458
    delayMicroseconds(1);        // tCL=1000ns, tCDD=800ns
459
460
    // read bit, and set it in place in 'data' variable
461
    bitWrite (data, i, digitalRead(DS1302_IO));
462
  }
463
  return (data);
464
}
465
466
467
// --------------------------------------------------------
468
// _DS1302_togglewrite
469
//
470
// A helper function for writing a byte with bit toggle
471
//
472
// The 'release' parameter is for a read after this write.
473
// It will release the I/O-line and will keep the SCLK high.
474
//
475
void _DS1302_togglewrite(uint8_t data, uint8_t release)
476
{
477
  int i;
478
479
  for (i = 0; i <= 7; i++)
480
  {
481
    // set a bit of the data on the I/O-line
482
    digitalWrite (DS1302_IO, bitRead(data, i));  
483
    delayMicroseconds(1);     // tDC = 200ns
484
485
    // clock up, data is read by DS1302
486
    digitalWrite (DS1302_SCLK, HIGH);    
487
    delayMicroseconds(1);     // tCH = 1000ns, tCDH = 800ns
488
489
    if (release && i == 7)
490
    {
491
      // If this write is followed by a read,
492
      // the I/O-line should be released after
493
      // the last bit, before the clock line is made low.
494
      // This is according the datasheet.
495
      // I have seen other programs that don't release
496
      // the I/O-line at this moment,
497
      // and that could cause a shortcut spike
498
      // on the I/O-line.
499
      pinMode (DS1302_IO, INPUT);
500
      digitalWrite (DS1302_IO, LOW); // remove any pull-up  
501
    }
502
    else
503
    {
504
      digitalWrite (DS1302_SCLK, LOW);
505
      delayMicroseconds(1);       // tCL=1000ns, tCDD=800ns
506
    }
507
  }
508
}
509
void uurErbij(){
510
  increaseHour = 1;
511
  Serial.println("bleep");
512
}
513
void uur10Erbij(){
514
  increaseHour10 = 1;
515
  Serial.println("bleepbleep");
516
}