Arduino UNO R4 WiFi Real-Time Clock

Started by tha, October 27, 2023, 02:11:51 PM

Previous topic - Next topic

tha

https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc

Arduino UNO R4 WiFi Real-Time Clock

เรียนวิธีการเข้าถึง the real-time clock (RTC) บน the UNO R4 WiFi.

AUTHOR: Karl Söderby

ในบทช่วยสอนนี้คุณจะเรียนวิธีการเข้าถึง the real-time clock (RTC) บน an Arduino UNO R4 WiFi board. The RTC ถูกฝังอยู่ใน microcontroller (RA4M1) ของ the UNO R4 WiFi.

Goals

เป้าหมายของโครงการนี้คือ:

  •  กำหนดวันเริ่มต้นของ RTC
  •  เข้าถึงวันที่/เวลาจาก RTC ในรูปแบบปฏิทิน
  •  เข้าถึงเวลาในรูปแบบ Unix

Hardware & Software Needed

  •  Arduino IDE (online or offline)
  •  Arduino UNO R4 WiFi
  •  Arduino Renesas Core

tha

Real-Time Clock (RTC)

The RTC บน the UNO R4 WiFi สามารถเข้าถึงโดยใช้ the RTC library ที่ถูกรวมใน the Renesas core. ไลบารีนี้ช่วยให้คุณตั้งค่า/ได้ค่า the time ตลอดจนใช้ alarms เพื่อทริก interrupts.

The UNO R4 WiFi มี a VRTC pin, ที่ถูกใช้เพื่อทำให้ the onboard RTC รันอยู่, แม้เมื่อ the boards power supply ถูกตัด. เพื่อใช้การนี้, จ่าย a voltage ในช่วง 1.6 - 3.6 V ให้กับ the VRTC pin.

มีตัวอย่างเชิงปฏิบัติมากมายที่ใช้ RTC และตัวอย่างที่ให้ไว้ในหน้านี้จะช่วยคุณในการเริ่มต้นใช้งานมัน

tha

Set Time

  •  RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE)
  •  RTC.setTime(startTime)

เพื่อตั้งค่า the starting time สำหรับ the RTC, คุณสามารถสร้าง an RTCTime object. ที่นี่คุณสามารถระบุ the day, month, year, hour, minute, second, และระบุ day of week ตลอดจน daylight saving mode.

จากนั้นเพื่อตั้งเวลา ให้ใช้ the setTime() method.

Example:


tha

Get Time

RTC.getTime(currentTime)

เพื่อดึง the time, เราจำเป็นต้องสร้าง a RTCTime object, และใช้ the getTime() method เพื่อดึง the current time.

ตัวอย่างนี้ตั้งค่า & ได้ค่า the time และเก็บมันใน an RTCTime object ที่เรียกว่า currentTime.

tha

Print Date & Time

ตัวอย่างข้างต้นแสดงวิธีการตั้งค่าและรับค่า the time และเก็บมันใน an object. ข้อมูลนี้สามารถถูกดึงออกโดยชุดของ methods:

  •  getDayOfMonth()
  •  getMonth()
  •  getYear()
  •  getHour()
  •  getMinutes()
  •  getSeconds()

ตัวอย่างข้างล่างพิมพ์ออก the date และ time จาก the currentTime object.


tha

Unix

  •  currentTime.getUnixTime()

เพื่อดึงเอา the Unix timestamp, ให้ใช้ the getUnixTime() method.

tha

Periodic Interrupt

A periodic interrupt ช่วยให้คุณตั้งค่า a recurring callback.

เพื่อใช้สิ่งนี้ คุณจะต้องเริ่มต้น the periodic callback, โดย the setPeriodicCallback() method:

  •  RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)

คุณจะต้องสร้าง a function ที่จะถูกเรียกอีกด้วย:

  •  void periodicCallback() { โค้ดที่จะถูกปฏิบัติ }

โปรดทราบว่า IRQ มีเวลาดำเนินการที่รวดเร็วมาก การวางโค้ดจำนวนมากไม่ใช่แนวทางปฏิบัติที่ดี ดังนั้นในตัวอย่างด้านล่าง เราจะเปลี่ยนเพียงแฟล็กเดียว นั่นคือ irqFlag

ตัวอย่างข้างล่างกระพริบหลอดไฟทุกๆ 2 seconds:



The period สามารถถูกระบุโดยใช้ the following enumerations:

  •  ONCE_EVERY_2_SEC
  •  ONCE_EVERY_1_SEC
  •  N2_TIMES_EVERY_SEC
  •  N4_TIMES_EVERY_SEC
  •  N8_TIMES_EVERY_SEC
  •  N16_TIMES_EVERY_SEC
  •  N32_TIMES_EVERY_SEC
  •  N64_TIMES_EVERY_SEC
  •  N128_TIMES_EVERY_SEC
  •  N256_TIMES_EVERY_SEC

tha