19-DS1302 real-time clock

อยู่ระหว่างการทำยังไม่เรียบร้อย

เพื่อการทดลองดัดแปลงใช้ดัดแปลงใช้แหล่งจ่าย 5 โวลท์ แทน 3.3 โวลท์

การทดลองนี้ ได้แสดงวิธีการใช้งานไอซี Real-time clock เบอร์ DS1302 ซึ่งมีการอินเตอร์เฟสแบบ3-Wire โดยผ่านการควบคุมของไมโครคอนโทรลเลอร์ Arduino

รูปที่ 1 เป็นโลจิกไดอะแกรมของวงจรที่ใช้ทดลอง โดยมี DS1302 ทำหน้าที่เป็นตัวสร้างค่าเวลาและวันเดือนปี LCD ใช้สำหรับการแสดงผล ส่วนสวิชทั้ง 3 ตัวใช้สำหรับการตั้งเวลาและวันที่ รายละเอียดของ DS1302 และ ไลบรารี่ สำหรับ DS1302 ds1302.h ดาวน์โหล์ได้จากhttp://blog.trendsmix.com/?attachment_id=148 หรือ http://narong.ece.engr.tu.ac.th/microlab/document/index.php แล้วทำการ unzip ไฟล์ ไปเก็บที่ โฟลดเดอร์ libraries ซึ่งอยู่ในโฟลดเดอร์ Sketch ของ Arduino

และการใช้ API ให้ศึกษาจากไฟล์ ตัวอย่างโปรแกรม ในหน้าเว็บดังกล่าว

ตัวอย่างที่ 1 แสดงผลด้วย Serial Monitor

ตัวอย่างนี้นำมาจาก http://blog.trendsmix.com/?attachment_id=148

/*

Example sketch for interfacing with the DS1302 timekeeping chip.

Copyright (c) 2009, Matt Sparks

All rights reserved.

http://quadpoint.org/projects/arduino-ds1302

*/

#include <stdio.h>

#include <string.h>

#include <LiquidCrystal.h>

#include <DS1302.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

/* Set the appropriate digital I/O pin connections */

uint8_t CE_PIN = 8;

uint8_t IO_PIN = 7;

uint8_t SCLK_PIN = 6;

/* Create buffers */

char buf[50];

char day[10];

/* Create a DS1302 object */

DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);

void print_time()

{

/* Get the current time and date from the chip */

Time t = rtc.time();

/* Name the day of the week */

memset(day, 0, sizeof(day)); /* clear day buffer */

switch (t.day) {

case 1:

strcpy(day, "Sunday");

break;

case 2:

strcpy(day, "Monday");

break;

case 3:

strcpy(day, "Tuesday");

break;

case 4:

strcpy(day, "Wednesday");

break;

case 5:

strcpy(day, "Thursday");

break;

case 6:

strcpy(day, "Friday");

break;

case 7:

strcpy(day, "Saturday");

break;

}

/* Format the time and date and insert into the temporary buffer */

snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",

day,

t.yr, t.mon, t.date,

t.hr, t.min, t.sec);

/* Print the formatted string to serial so we can see the time */

Serial.println(buf);

}

void setup()

{

Serial.begin(9600);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

lcd.print("Real time clock"); // Print a message to the LCD.

/* Initialize a new chip by turning off write protection and clearing the

clock halt flag. These methods needn't always be called. See the DS1302

datasheet for details. */

rtc.write_protect(false);

rtc.halt(false);

/* Make a new time object to set the date and time */

/* Monday, Jan 13, 2014 at 13:30:37. */

Time t(2014, 1, 13, 13, 30, 37, 2);

/* Set the time and date on the chip */

rtc.time(t);

delay(1000);

}

/* Loop and print the time every second */

void loop()

{

print_time();

delay(1000);

}

ตัวอย่างที่ 2 แสดงผลบน LCD

#include <stdio.h>

#include <string.h>

#include <LiquidCrystal.h>

#include <DS1302.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins

/* Set the appropriate digital I/O pin connections */

uint8_t CE_PIN = 6;

uint8_t IO_PIN = 7;

uint8_t SCLK_PIN = 8;

/* Create buffers */

char buf[50];

char day[10];

/* Create a DS1302 object */

DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);

void print_time()

{

/* Get the current time and date from the chip */

Time t = rtc.time();

/* Name the day of the week */

memset(day, 0, sizeof(day)); /* clear day buffer */

switch (t.day) {

case 1:

strcpy(day, "Sunday");

break;

case 2:

strcpy(day, "Monday");

break;

case 3:

strcpy(day, "Tuesday");

break;

case 4:

strcpy(day, "Wednesday");

break;

case 5:

strcpy(day, "Thursday");

break;

case 6:

strcpy(day, "Friday");

break;

case 7:

strcpy(day, "Saturday");

break;

}

/* Format the time and date and insert into the temporary buffer */

snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",

day,

t.yr, t.mon, t.date,

t.hr, t.min, t.sec);

/* Print the formatted string to serial so we can see the time */

Serial.println(buf);

}

void printtime2LCD()

{

Time t = rtc.time();

lcd.setCursor(0, 0);

lcd.print(day);

lcd.print(" ");

lcd.print(t.yr);

lcd.print(" ");

lcd.print(t.mon);

lcd.print(" ");

lcd.print(t.date);

lcd.setCursor(0, 1);

lcd.print(t.hr);

lcd.print(" ");

lcd.print(t.min);

lcd.print(" ");

lcd.print(t.sec);

}

void setup()

{

Serial.begin(9600);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:

lcd.print("Real time clock"); // Print a message to the LCD.

/* Initialize a new chip by turning off write protection and clearing the

clock halt flag. These methods needn't always be called. See the DS1302

datasheet for details. */

rtc.write_protect(false);

rtc.halt(false);

/* Make a new time object to set the date and time */

/* Monday, Jan 13, 2014 at 13:30:37. */

Time t(2014, 1, 13, 13, 30, 37, 2);

/* Set the time and date on the chip */

rtc.time(t);

delay(1000);

}

/* Loop and print the time every second */

void loop()

{

print_time();

printtime2LCD();

delay(1000);

}