- UID
- 373967
- 帖子
- 8786
- 主題
- 2609
- 精華
- 0
- 積分
- 993
- 楓幣
- 2669
- 威望
- 970
- 存款
- 31556
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 50
- 在線時間
- 451 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-11-6
|
製作六位數字時鐘所需材料:
Padauk PMS150C 微控制器
6 位共陰極 7 段 LED 數字顯示器
6 顆 220Ω 電阻
2 個按鈕作為時間校正用
電路板、麵包板或其他適當的電路搭建工具
杜邦線等連接線材
一個 5V 直流電源
程式碼範例,使用 Padauk PMS150C 控制 LED 顯示器顯示時間:
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
delay(100); // debounce
}
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, 0);
displayDigit(3, 0);
displayDigit(4, 0);
displayDigit(5, 0);
} else if (mode == 2) {
// Set minutes
if (digitalRead(BUTTON2_PIN) == LOW) {
minutes++;
if (minutes >= 60) {
minutes = 0;
}
delay(100); // debounce
}
displayDigit(0, 0);
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
delay(100); // debounce
}
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, 0);
displayDigit(3, 0);
displayDigit(4, 0);
displayDigit(5, 0);
} else if (mode == 2) {
// Set minutes
if (digitalRead(BUTTON2_PIN) == LOW) {
minutes++;
if (minutes >= 60) {
minutes = 0;
}
delay(100); // debounce
}
displayDigit(0, 0);
displayDigit(1, 0);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
display
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
delay(100); // debounce
}
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, 0);
displayDigit(3, 0);
displayDigit(4, 0);
displayDigit(5, 0);
} else if (mode == 2) {
// Set minutes
if (digitalRead(BUTTON2_PIN) == LOW) {
minutes++;
if (minutes >= 60) {
minutes = 0;
}
delay(100); // debounce
}
displayDigit(0, 0);
displayDigit(1, 0);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
delay(100); // debounce
}
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, 0);
displayDigit(3, 0);
displayDigit(4, 0);
displayDigit(5, 0);
} else if (mode == 2) {
// Set minutes
if (digitalRead(BUTTON2_PIN) == LOW) {
minutes++;
if (minutes >= 60) {
minutes = 0;
}
delay(100); // debounce
}
displayDigit(0, 0);
displayDigit(1, 0);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
#include <PMS150C.h>
// Pin definitions
#define CLOCK_PIN 0
#define DATA_PIN 1
#define LATCH_PIN 2
#define BUTTON1_PIN 3
#define BUTTON2_PIN 4
// Global variables
unsigned int timer = 0; // Timer counter
unsigned int seconds = 0; // Current time in seconds
unsigned int minutes = 0; // Current time in minutes
unsigned int hours = 0; // Current time in hours
unsigned int mode = 0; // Current mode (normal or setting)
void setup() {
// Set pin modes
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Initialize timer
TMR0 = 6; // Prescale = 1:64, TMR0 = 6 (256-6)*64/1000000 = 10.24us
OPTION = 0b00000111; // Prescale = 1:64, timer is on
// Set initial time
seconds = 0;
minutes = 0;
hours = 0;
}
void loop() {
// Read buttons
if (digitalRead(BUTTON1_PIN) == LOW) {
mode = 1;
} else if (digitalRead(BUTTON2_PIN) == LOW) {
mode = 2;
} else {
mode = 0;
}
// Update time
if (timer >= 976) { // 976 * 10.24us = 10ms
timer = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
}
// Display time
if (mode == 0) {
// Display time normally
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
displayDigit(4, seconds / 10);
displayDigit(5, seconds % 10);
} else if (mode == 1) {
// Set hours
if (digitalRead(BUTTON2_PIN) == LOW) {
hours++;
if (hours >= 24) {
hours = 0;
}
delay(100); // debounce
}
displayDigit(0, hours / 10);
displayDigit(1, hours % 10);
displayDigit(2, 0);
displayDigit(3, 0);
displayDigit(4, 0);
displayDigit(5, 0);
} else if (mode == 2) {
// Set minutes
if (digitalRead(BUTTON2_PIN) == LOW) {
minutes++;
if (minutes >= 60) {
minutes = 0;
}
delay(100); // debounce
}
displayDigit(0, 0);
displayDigit(1, 0);
displayDigit(2, minutes / 10);
displayDigit(3, minutes % 10);
display |
|