找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 12653|回復(fù): 9
收起左側(cè)

DS1302 Arduino庫(kù)文件下載

[復(fù)制鏈接]
ID:315242 發(fā)表于 2018-4-24 12:53 | 顯示全部樓層 |閱讀模式
DS1302庫(kù)文件 Arduino
0.jpg

單片機(jī)源程序如下:
  1. /*
  2.   DS1302.cpp - Arduino library support for the DS1302 Trickle Charge Timekeeping Chip
  3.   Copyright (C)2010 Henning Karlsen. All right reserved
  4.   
  5.   You can find the latest version of the library at

  6.   This library has been made to easily interface and use the DS1302 RTC with
  7.   the Arduino.

  8.   If you make any modifications or improvements to the code, I would appreciate
  9.   that you share the code with me so that I might include it in the next release.

  10.   This library is free software; you can redistribute it and/or
  11.   modify it under the terms of the GNU Lesser General Public
  12.   License as published by the Free Software Foundation; either
  13.   version 2.1 of the License, or (at your option) any later version.

  14.   This library is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   Lesser General Public License for more details.

  18.   You should have received a copy of the GNU Lesser General Public
  19.   License along with this library; if not, write to the Free Software
  20.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21. */
  22. #include "DS1302.h"

  23. #define REG_SEC                0
  24. #define REG_MIN                1
  25. #define REG_HOUR        2
  26. #define REG_DATE        3
  27. #define REG_MON                4
  28. #define REG_DOW                5
  29. #define REG_YEAR        6
  30. #define REG_WP                7
  31. #define REG_TCR                8

  32. /* Public */

  33. Time::Time()
  34. {
  35.         this->year = 2010;
  36.         this->mon  = 1;
  37.         this->date = 1;
  38.         this->hour = 0;
  39.         this->min  = 0;
  40.         this->sec  = 0;
  41.         this->dow  = 5;
  42. }

  43. DS1302_RAM::DS1302_RAM()
  44. {
  45.         for (int i=0; i<31; i++)
  46.                 cell[i]=0;
  47. }

  48. DS1302::DS1302(uint8_t ce_pin, uint8_t data_pin, uint8_t sclk_pin)
  49. {
  50.         _ce_pin = ce_pin;
  51.         _data_pin = data_pin;
  52.         _sclk_pin = sclk_pin;

  53.         pinMode(_ce_pin, OUTPUT);
  54.         pinMode(_sclk_pin, OUTPUT);
  55. }

  56. Time DS1302::getTime()
  57. {
  58.         Time t;
  59.         _burstRead();
  60.         t.sec        = _decode(_burstArray[0]);
  61.         t.min        = _decode(_burstArray[1]);
  62.         t.hour        = _decodeH(_burstArray[2]);
  63.         t.date        = _decode(_burstArray[3]);
  64.         t.mon        = _decode(_burstArray[4]);
  65.         t.dow        = _burstArray[5];
  66.         t.year        = _decodeY(_burstArray[6])+2000;
  67.         return t;
  68. }

  69. void DS1302::setTime(uint8_t hour, uint8_t min, uint8_t sec)
  70. {
  71.         if (((hour>=0) && (hour<24)) && ((min>=0) && (min<60)) && ((sec>=0) && (sec<60)))
  72.         {
  73.                 _writeRegister(REG_HOUR, _encode(hour));
  74.                 _writeRegister(REG_MIN, _encode(min));
  75.                 _writeRegister(REG_SEC, _encode(sec));
  76.         }
  77. }

  78. void DS1302::setDate(uint8_t date, uint8_t mon, uint16_t year)
  79. {
  80.         if (((date>0) && (date<=31)) && ((mon>0) && (mon<=12)) && ((year>=2000) && (year<3000)))
  81.         {
  82.                 year -= 2000;
  83.                 _writeRegister(REG_YEAR, _encode(year));
  84.                 _writeRegister(REG_MON, _encode(mon));
  85.                 _writeRegister(REG_DATE, _encode(date));
  86.         }
  87. }

  88. void DS1302::setDOW(uint8_t dow)
  89. {
  90.         if ((dow>0) && (dow<8))
  91.                 _writeRegister(REG_DOW, dow);
  92. }

  93. char *DS1302::getTimeStr(uint8_t format)
  94. {
  95.         char *output= "xxxxxxxx";
  96.         Time t;
  97.         t=getTime();
  98.         if (t.hour<10)
  99.                 output[0]=48;
  100.         else
  101.                 output[0]=char((t.hour / 10)+48);
  102.         output[1]=char((t.hour % 10)+48);
  103.         output[2]=58;
  104.         if (t.min<10)
  105.                 output[3]=48;
  106.         else
  107.                 output[3]=char((t.min / 10)+48);
  108.         output[4]=char((t.min % 10)+48);
  109.         output[5]=58;
  110.         if (format==FORMAT_SHORT)
  111.                 output[5]=0;
  112.         else
  113.         {
  114.         if (t.sec<10)
  115.                 output[6]=48;
  116.         else
  117.                 output[6]=char((t.sec / 10)+48);
  118.         output[7]=char((t.sec % 10)+48);
  119.         output[8]=0;
  120.         }
  121.         return output;
  122. }

  123. char *DS1302::getDateStr(uint8_t slformat, uint8_t eformat, char divider)
  124. {
  125.         char *output= "xxxxxxxxxx";
  126.         int yr, offset;
  127.         Time t;
  128.         t=getTime();
  129.         switch (eformat)
  130.         {
  131.                 case FORMAT_LITTLEENDIAN:
  132.                         if (t.date<10)
  133.                                 output[0]=48;
  134.                         else
  135.                                 output[0]=char((t.date / 10)+48);
  136.                         output[1]=char((t.date % 10)+48);
  137.                         output[2]=divider;
  138.                         if (t.mon<10)
  139.                                 output[3]=48;
  140.                         else
  141.                                 output[3]=char((t.mon / 10)+48);
  142.                         output[4]=char((t.mon % 10)+48);
  143.                         output[5]=divider;
  144.                         if (slformat==FORMAT_SHORT)
  145.                         {
  146.                                 yr=t.year-2000;
  147.                                 if (yr<10)
  148.                                         output[6]=48;
  149.                                 else
  150.                                         output[6]=char((yr / 10)+48);
  151.                                 output[7]=char((yr % 10)+48);
  152.                                 output[8]=0;
  153.                         }
  154.                         else
  155.                         {
  156.                                 yr=t.year;
  157.                                 output[6]=char((yr / 1000)+48);
  158.                                 output[7]=char(((yr % 1000) / 100)+48);
  159.                                 output[8]=char(((yr % 100) / 10)+48);
  160.                                 output[9]=char((yr % 10)+48);
  161.                                 output[10]=0;
  162.                         }
  163.                         break;
  164.                 case FORMAT_BIGENDIAN:
  165.                         if (slformat==FORMAT_SHORT)
  166.                                 offset=0;
  167.                         else
  168.                                 offset=2;
  169.                         if (slformat==FORMAT_SHORT)
  170.                         {
  171.                                 yr=t.year-2000;
  172.                                 if (yr<10)
  173.                                         output[0]=48;
  174.                                 else
  175.                                         output[0]=char((yr / 10)+48);
  176.                                 output[1]=char((yr % 10)+48);
  177.                                 output[2]=divider;
  178.                         }
  179.                         else
  180.                         {
  181.                                 yr=t.year;
  182.                                 output[0]=char((yr / 1000)+48);
  183.                                 output[1]=char(((yr % 1000) / 100)+48);
  184.                                 output[2]=char(((yr % 100) / 10)+48);
  185.                                 output[3]=char((yr % 10)+48);
  186.                                 output[4]=divider;
  187.                         }
  188.                         if (t.mon<10)
  189.                                 output[3+offset]=48;
  190.                         else
  191.                                 output[3+offset]=char((t.mon / 10)+48);
  192.                         output[4+offset]=char((t.mon % 10)+48);
  193.                         output[5+offset]=divider;
  194.                         if (t.date<10)
  195.                                 output[6+offset]=48;
  196.                         else
  197.                                 output[6+offset]=char((t.date / 10)+48);
  198.                         output[7+offset]=char((t.date % 10)+48);
  199.                         output[8+offset]=0;
  200.                         break;
  201.                 case FORMAT_MIDDLEENDIAN:
  202.                         if (t.mon<10)
  203.                                 output[0]=48;
  204.                         else
  205.                                 output[0]=char((t.mon / 10)+48);
  206.                         output[1]=char((t.mon % 10)+48);
  207.                         output[2]=divider;
  208.                         if (t.date<10)
  209.                                 output[3]=48;
  210.                         else
  211.                                 output[3]=char((t.date / 10)+48);
  212.                         output[4]=char((t.date % 10)+48);
  213.                         output[5]=divider;
  214.                         if (slformat==FORMAT_SHORT)
  215.                         {
  216.                                 yr=t.year-2000;
  217.                                 if (yr<10)
  218.                                         output[6]=48;
  219.                                 else
  220.                                         output[6]=char((yr / 10)+48);
  221.                                 output[7]=char((yr % 10)+48);
  222.                                 output[8]=0;
  223.                         }
  224.                         else
  225.                         {
  226.                                 yr=t.year;
  227.                                 output[6]=char((yr / 1000)+48);
  228.                                 output[7]=char(((yr % 1000) / 100)+48);
  229.                                 output[8]=char(((yr % 100) / 10)+48);
  230.                                 output[9]=char((yr % 10)+48);
  231.                                 output[10]=0;
  232.                         }
  233.                         break;
  234.         }
  235.         return output;
  236. }

  237. char *DS1302::getDOWStr(uint8_t format)
  238. {
  239.         char *output= "xxxxxxxxx";
  240.         Time t;
  241.         t=getTime();
  242.         switch (t.dow)
  243.         {
  244.                 case MONDAY:
  245.                         output="Monday";
  246.                         break;
  247.                 case TUESDAY:
  248.                         output="Tuesday";
  249.                         break;
  250.                 case WEDNESDAY:
  251.                         output="Wednesday";
  252.                         break;
  253.                 case THURSDAY:
  254.                         output="Thursday";
  255.                         break;
  256.                 case FRIDAY:
  257.                         output="Friday";
  258.                         break;
  259.                 case SATURDAY:
  260.                         output="Saturday";
  261.                         break;
  262.                 case SUNDAY:
  263.                         output="Sunday";
  264.                         break;
  265.         }     
  266.         if (format==FORMAT_SHORT)
  267.                 output[3]=0;
  268.         return output;
  269. }

  270. char *DS1302::getMonthStr(uint8_t format)
  271. {
  272.         char *output= "xxxxxxxxx";
  273.         Time t;
  274.         t=getTime();
  275.         switch (t.mon)
  276.         {
  277.                 case 1:
  278.                         output="January";
  279.                         break;
  280.                 case 2:
  281.                         output="February";
  282.                         break;
  283.                 case 3:
  284.                         output="March";
  285.                         break;
  286.                 case 4:
  287.                         output="April";
  288.                         break;
  289.                 case 5:
  290.                         output="May";
  291.                         break;
  292.                 case 6:
  293.                         output="June";
  294.                         break;
  295.                 case 7:
  296.                         output="July";
  297.                         break;
  298.                 case 8:
  299.                         output="August";
  300.                         break;
  301.                 case 9:
  302.                         output="September";
  303.                         break;
  304.                 case 10:
  305.                         output="October";
  306.                         break;
  307.                 case 11:
  308.                         output="November";
  309.                         break;
  310.                 case 12:
  311.                         output="December";
  312.                         break;
  313.         }     
  314.         if (format==FORMAT_SHORT)
  315.                 output[3]=0;
  316.         return output;
  317. }

  318. void DS1302::halt(bool enable)
  319. {
  320.   uint8_t _reg = _readRegister(REG_SEC);
  321.   _reg &= ~(1 << 7);
  322.   _reg |= (enable << 7);
  323.   _writeRegister(REG_SEC, _reg);
  324. }

  325. void DS1302::writeProtect(bool enable)
  326. {
  327.   uint8_t _reg = (enable << 7);
  328.   _writeRegister(REG_WP, _reg);
  329. }

  330. void DS1302::setTCR(uint8_t value)
  331. {
  332.         _writeRegister(REG_TCR, value);
  333. }

  334. /* Private */

  335. uint8_t DS1302::_readByte()
  336. {
  337.         pinMode(_data_pin, INPUT);

  338.         uint8_t value = 0;
  339.         uint8_t currentBit = 0;

  340.         for (int i = 0; i < 8; ++i)
  341.         {
  342.                 currentBit = digitalRead(_data_pin);
  343.                 value |= (currentBit << i);
  344.                 digitalWrite(_sclk_pin, HIGH);
  345.                 delayMicroseconds(1);
  346.                 digitalWrite(_sclk_pin, LOW);
  347.         }
  348.         

  349. …………
  350. …………
  351. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
DS1302.zip (72.21 KB, 下載次數(shù): 259)


回復(fù)

使用道具 舉報(bào)

ID:315888 發(fā)表于 2018-4-25 07:35 | 顯示全部樓層
完整版的代碼在哪啊
回復(fù)

使用道具 舉報(bào)

ID:321159 發(fā)表于 2018-6-29 19:33 | 顯示全部樓層
想下,可惜新手,黑幣不夠。!
回復(fù)

使用道具 舉報(bào)

ID:380225 發(fā)表于 2018-7-28 16:16 | 顯示全部樓層
為啥顯示 includeWProgram.h: No such file or directory;已經(jīng)將“ DS1302.cpp和DS1302.h文件中WProgram.h已經(jīng)改名為 Arduino.h”
回復(fù)

使用道具 舉報(bào)

ID:380225 發(fā)表于 2018-7-28 16:17 | 顯示全部樓層
已經(jīng)將庫(kù)文件中的DS1302.cpp和DS1302.h中 WProgram.h已經(jīng)改名為 Arduino.h,為啥還是顯示錯(cuò)誤。
回復(fù)

使用道具 舉報(bào)

ID:398299 發(fā)表于 2018-11-6 12:13 | 顯示全部樓層
在arduino IDE 1.0 及后續(xù)版本,WProgram.h已經(jīng)改名為 Arduino.h,你把相應(yīng)的.h和.cpp改名就行。這是在1.0.X版里編譯更老的程序時(shí)的通病,一個(gè)更完美的解決方案是,在老的程序前加這幾行就新老IDE通吃:

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
回復(fù)

使用道具 舉報(bào)

ID:272625 發(fā)表于 2019-3-6 13:10 | 顯示全部樓層
誰位大神有Time庫(kù),上傳上來用下!
回復(fù)

使用道具 舉報(bào)

ID:1083556 發(fā)表于 2023-7-23 00:31 | 顯示全部樓層
誰位大神有Time庫(kù),上傳上來用下!
回復(fù)

使用道具 舉報(bào)

ID:1083556 發(fā)表于 2023-7-23 01:05 | 顯示全部樓層
誰位大神有Time庫(kù),上傳上來用下!
回復(fù)

使用道具 舉報(bào)

ID:138707 發(fā)表于 2023-7-29 17:33 | 顯示全部樓層
誰位大神有Time庫(kù),上傳上來用下!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表