Nessuna descrizione

DS1307Sample.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
  3. * Copyright (c) 2015 Intel Corporation.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. //NOT TESTED!!!
  25. public class DS1307Sample {
  26. static private void printTime(upm_ds1307.DS1307 rtc) {
  27. System.out.print("The time is: " + rtc.getMonth() + "/" + rtc.getDayOfMonth() + "/"
  28. + rtc.getYear() + " " + rtc.getHours() + ":" + rtc.getMinutes() + ":"
  29. + rtc.getSeconds());
  30. if (rtc.getAmPmMode()) {
  31. if (rtc.getAmPmMode())
  32. System.out.print(" PM");
  33. else
  34. System.out.print(" AM");
  35. }
  36. System.out.println();
  37. if (rtc.getAmPmMode())
  38. System.out.println("Clock is in AM/PM mode");
  39. else
  40. System.out.println("Clock is in 24h mode");
  41. }
  42. public static void main(String[] args) throws InterruptedException {
  43. // ! [Interesting]
  44. // Instantiate a DS1037 on I2C bus 0
  45. upm_ds1307.DS1307 rtc = new upm_ds1307.DS1307(0);
  46. // always do this first
  47. System.out.println("Loading the current time...");
  48. if (!rtc.loadTime()) {
  49. System.err.println("rtc->loadTime() failed.");
  50. System.exit(-1);
  51. }
  52. printTime(rtc);
  53. // set the year as an example
  54. System.out.println("setting the year to 50");
  55. rtc.setYear(50);
  56. rtc.setTime();
  57. // reload the time and print it
  58. rtc.loadTime();
  59. printTime(rtc);
  60. // ! [Interesting]
  61. }
  62. }