Mạch thời gian thực MKE-M09 RTC DS1307 real time clock module: Difference between revisions

From MakerLab Wiki
Jump to navigation Jump to search
No edit summary
Line 81: Line 81:


==Chương trình mẫu==
==Chương trình mẫu==
Xem thêm '''''[[Cách_cài_đặt_các_thư_viện_phần_cứng_Arduino_Library]]'''''
<br>
Các bạn tải [https://github.com/makerlabvn/TinyWireM '''''Thư viện TinyWireM'''''].
<br>
Các bạn tải [https://github.com/makerlabvn/RTClib '''''Thư viện RTClib'''''] sẽ có sẵn chương trình mẫu như bên dưới.


{{kxncode
|name=ds3231.ino
|code=
<syntaxhighlight>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
  Serial.begin(57600);
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
void loop () {
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    // calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    Serial.print(" now + 7d + 12h + 30m + 6s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    Serial.print("Temperature: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C");
    Serial.println();
    delay(3000);
}
</syntaxhighlight>
}}
[[Category:Modules]]
[[Category:Modules]]

Revision as of 08:54, 28 September 2021

MKL-M09 RTC DS3231 real time clock module
MKL-M09 RTC DS3231 real time clock module

Giới thiệu

Mạch thời gian thực MKL-M09 RTC DS3231 real time clock module được sử dụng để lấy dữ liệu về thời gian thực: giờ, phút, giây, thứ, ngày, tháng, năm trong các ứng dụng cần điều khiển và đồng bộ với thời gian thực tế, mạch sử dụng giao tiếp I2C nên dễ dàng kết nối và sử dụng với chỉ 2 chân giao tiếp I2C là SDA (data) và SCL (clock).

Mạch thời gian thực MKL-M09 RTC DS3231 real time clock module được thiết kế để có thể sử dụng trực tiếp an toàn với các board mạch giao tiếp ở mức điện áp 3.3/5VDC: Arduino, Raspberry Pi, Jetson Nano, Micro:bit,....


thumb
Cảnh báo:

Xin nạp (upload) chương trình trước khi kết nối mạch vào Arduino/Vietduino để chắc chắn rằng các chân giao tiếp đã được cấu hình đúng!

Thông số kỹ thuật

  • Điện áp hoạt động: 5VDC
  • Chuẩn giao tiếp: Digital I2C
  • Các chân giao tiếp: SDA (Serial Data) / SCL (Serial Clock)
  • Điện áp giao tiếp: TTL 3.3/5VDC
  • IC thời gian thực: DS3231, datasheet.
  • Tích hợp pin CR1220 lưu giữ thời gian khi không cấp nguồn.
  • Thiết kế tối ưu để có thể sử dụng trực tiếp với các board mạch giao tiếp ở cả hai mức điện áp 3.3VDC hoặc 5VDC như: Arduino, Raspberry Pi, Jetson Nano, Micro:bit,....
  • Chuẩn kết nối: Conector XH2.54 4Pins
  • Tương thích tốt nhất khi sử dụng với mạch MakerEDU Shield.

Kích thước

MKL-M09 RTC DS3231 real time clock module dimension
MKL-M09 RTC DS3231 real time clock module dimension

Các chân tín hiệu

MKL-M09 RTC DS3231 real time clock module back
MKL-M09 RTC DS3231 real time clock module back
MKL-M09 Ghi chú
GND Chân cấp nguồn âm 0VDC
5V Chân cấp nguồn dương 5VDC
SDA Chân tín hiệu Digital Data
SCL Chân tín hiệu Digital Clock

Kết nối phần cứng

Bước 1: Chuẩn bị phần cứng:

Bước 2: Cắm MakerEdu Shield vào mạch Vietduino Uno.

Bước 3: Kết nối cổng tín hiệu của mạch vào cổng (I2C) trên MakerEDU Shield.

Bước 4: Kết nối Vietduino Uno với máy tính thông qua cáp USB.

File:MKL-M09 RTC DS3231 real time clock module with MakerEDU Shield.jpg
MKL-M09 RTC DS3231 real time clock module with MakerEDU Shield


thumb
Lưu ý:

Nếu không có sẵn MakerEDU Shield, bạn có thể kết nối trực tiếp mạch với Arduino/Vietduino như bảng dưới đây.

Arduino/Vietduino Mạch thời gian thực MKL-M09 RTC DS3231 real time clock module
GND GND
5V 5V
A4 SDA
A5 SCL

Chương trình mẫu

Xem thêm Cách_cài_đặt_các_thư_viện_phần_cứng_Arduino_Library
Các bạn tải Thư viện TinyWireM.
Các bạn tải Thư viện RTClib sẽ có sẵn chương trình mẫu như bên dưới.

Template:Kxncode