Advertisement
Guest User

Untitled

a guest
Feb 8th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. int pin = 2;
  4. int previousValue;
  5.  
  6.  
  7.  
  8.  
  9.  
  10. // Set your own pins with these defines !
  11. #define DS1302_SCLK 6    // Arduino pin for the Serial Clock
  12. #define DS1302_IO   7    // Arduino pin for the Data I/O
  13. #define DS1302_CE   8    // Arduino pin for the Chip Enable
  14.  
  15.  
  16.  
  17. // Register names.
  18. // Since the highest bit is always '1',
  19. // the registers start at 0x80
  20. // If the register is read, the lowest bit should be '1'.
  21. #define DS1302_SECONDS           0x80
  22. #define DS1302_MINUTES           0x82
  23. #define DS1302_HOURS             0x84
  24. #define DS1302_DATE              0x86
  25. #define DS1302_MONTH             0x88
  26. #define DS1302_DAY               0x8A
  27. #define DS1302_YEAR              0x8C
  28. #define DS1302_ENABLE            0x8E
  29. #define DS1302_TRICKLE           0x90
  30. #define DS1302_CLOCK_BURST       0xBE
  31. #define DS1302_CLOCK_BURST_WRITE 0xBE
  32. #define DS1302_CLOCK_BURST_READ  0xBF
  33. #define DS1302_RAMSTART          0xC0
  34. #define DS1302_RAMEND            0xFC
  35. #define DS1302_RAM_BURST         0xFE
  36. #define DS1302_RAM_BURST_WRITE   0xFE
  37. #define DS1302_RAM_BURST_READ    0xFF
  38.  
  39.  
  40.  
  41. // Defines for the bits, to be able to change
  42. // between bit number and binary definition.
  43. // By using the bit number, using the DS1302
  44. // is like programming an AVR microcontroller.
  45. // But instead of using "(1<<X)", or "_BV(X)",
  46. // the Arduino "bit(X)" is used.
  47. #define DS1302_D0 0
  48. #define DS1302_D1 1
  49. #define DS1302_D2 2
  50. #define DS1302_D3 3
  51. #define DS1302_D4 4
  52. #define DS1302_D5 5
  53. #define DS1302_D6 6
  54. #define DS1302_D7 7
  55.  
  56.  
  57. // Bit for reading (bit in address)
  58. #define DS1302_READBIT DS1302_D0 // READBIT=1: read instruction
  59.  
  60. // Bit for clock (0) or ram (1) area,
  61. // called R/C-bit (bit in address)
  62. #define DS1302_RC DS1302_D6
  63.  
  64. // Seconds Register
  65. #define DS1302_CH DS1302_D7   // 1 = Clock Halt, 0 = start
  66.  
  67. // Hour Register
  68. #define DS1302_AM_PM DS1302_D5 // 0 = AM, 1 = PM
  69. #define DS1302_12_24 DS1302 D7 // 0 = 24 hour, 1 = 12 hour
  70.  
  71. // Enable Register
  72. #define DS1302_WP DS1302_D7   // 1 = Write Protect, 0 = enabled
  73.  
  74. // Trickle Register
  75. #define DS1302_ROUT0 DS1302_D0
  76. #define DS1302_ROUT1 DS1302_D1
  77. #define DS1302_DS0   DS1302_D2
  78. #define DS1302_DS1   DS1302_D2
  79. #define DS1302_TCS0  DS1302_D4
  80. #define DS1302_TCS1  DS1302_D5
  81. #define DS1302_TCS2  DS1302_D6
  82. #define DS1302_TCS3  DS1302_D7
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement