- UID
- 373967
- 帖子
- 9013
- 主題
- 2609
- 精華
- 0
- 積分
- 1004
- 楓幣
- 0
- 威望
- 981
- 存款
- 35926
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 460 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-12-23
|
arduino nano製作4位數字時鐘
製作4位數字時鐘所需材料:
Arduino Nano (ATmega328P)
DS3231 RTC模組(含電池)
共陽極4位數字7段顯示器 x 4
220歐姆電阻 x 8
杜邦線數據線(母-母)x 20
按鈕開關 x 3
麵包板 x 1
連接腳位:
DS3231 RTC模組 - Arduino Nano
SDA - A4
SCL - A5
VCC - 5V
GND - GND
共陽極4位數字7段顯示器 x 4 - Arduino Nano
A - 2
B - 3
C - 4
D - 5
E - 6
F - 7
G - 8
DP - 9
COM1 - 10
COM2 - 11
COM3 - 12
COM4 - 13
按鈕開關
調整時鐘紐 - D10
設置模式開關 - D11
單位/十位數字選擇開關 - D12
程式碼:
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int digitPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
int comPins[] = {10, 11, 12, 13};
int hourUnitPin = 12;
int hourTenPin = 11;
int minUnitPin = 10;
int minTenPin = 9;
int setTimePin = 8;
int modePin = 7;
int setTime = 0;
int setMode = 0;
int unitMode = 1;
DateTime now;
void setup() {
for (int i = 0; i < 8; i++) {
pinMode(digitPins[i], OUTPUT);
}
for (int i = 0; i < 4; i++) {
pinMode(comPins[i], OUTPUT);
}
pinMode(hourUnitPin, INPUT_PULLUP);
pinMode(hourTenPin, INPUT_PULLUP);
pinMode(minUnitPin, INPUT_PULLUP);
pinMode(minTenPin, INPUT_PULLUP);
pinMode(setTimePin, INPUT_PULLUP);
pinMode(modePin, INPUT_PULLUP);
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
now = rtc.now();
int hour = now.hour();
int minute = now.minute();
if (setTime) {
if (unitMode) {
if (digitalRead(hourUnitPin) == LOW) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), now.hour() + 1, minute, now.second()));
delay(200);
}
if (digitalRead(minUnitPin) == LOW) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, now.minute() + 1, now.second()));
} else {
if (digitalRead(hourTenPin) == LOW) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), (now.hour() + 10) % 24, minute, now.second()));
delay(200);
}
if (digitalRead(minTenPin) == LOW) {
rtc.adjust(DateTime(now.year(), now.month(), now.day(), hour, (now.minute() + 10) % 60, now.second()));
delay(200);
}
}
}
setMode = digitalRead(modePin);
if (setMode == LOW) {
unitMode = !unitMode;
delay(200);
}
setTime = digitalRead(setTimePin);
if (setTime == LOW) {
delay(200);
setTime = digitalRead(setTimePin);
if (setTime == LOW) {
setMode = 1;
while (setTime == LOW) {
now = rtc.now();
int hour = now.hour();
int minute = now.minute();
for (int i = 0; i < 4; i++) {
digitalWrite(comPins[i], HIGH);
int digit = 0;
if (i == 0) {
digit = hour / 10;
} else if (i == 1) {
digit = hour % 10;
} else if (i == 2) {
digit = minute / 10;
} else {
digit = minute % 10;
}
for (int j = 0; j < 8; j++) {
digitalWrite(digitPins[j], (digit >> j) & 0x01);
}
digitalWrite(comPins[i], LOW);
delay(5);
}
}
setMode = 0;
}
}
for (int i = 0; i < 4; i++) {
digitalWrite(comPins[i], HIGH);
int digit = 0;
if (i == 0) {
digit = hour / 10;
} else if (i == 1) {
digit = hour % 10;
} else if (i == 2) {
digit = minute / 10;
} else {
digit = minute % 10;
}
for (int j = 0; j < 8; j++) {
digitalWrite(digitPins[j], (digit >> j) & 0x01);
}
digitalWrite(comPins[i], LOW);
delay(5);
}
} |
|