arduino 电子时钟
# 1.七段數碼管的電路連接如圖:
a.
七段數碼管的common極都為陰極,接至負極
b.?
Seg_Control_Pin[0][i]數組控制十位,
Seg_Control_Pin[1][i]數組控制個位
# 2.代碼部分:
const uint8_t Seg_Code[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// Nixie tube truth tableconst
uint8_t Seg_Control_Pin[2][7] = {{0, 1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12, 13}}; ?// Nixie tube pin definition
uint8_t Timers_Count = 0;?
const使得七段數碼管的引腳編號只能被檢索,不能被改變
void setup()
{
? ? for (uint8_t i = 0; i < 7; i++) // Traverse the nixie tube control pin
? ? {
? ? ? ? pinMode(Seg_Control_Pin[0][i], OUTPUT); ? // Set the nixie pin as the output
? ? ? ? pinMode(Seg_Control_Pin[1][i], OUTPUT); ? // Set the nixie pin as the output
? ? ? ? digitalWrite(Seg_Control_Pin[0][i], LOW); // Quench digital tube
? ? ? ? digitalWrite(Seg_Control_Pin[1][i], LOW); // Quench digital tube
? ? }
}
for循環依次將兩組引腳調整為output
void loop()
{
? ? delay(1000); ? ? ? ? ? ? ? ? ? ?// Wait for 1000 millisecond(s)
? ? Timers_Count++; ? ? ? ? ? ? ? ? // Count increment by 1
? ? if (Timers_Count >= 60) ? ? ? ? // If the count is greater than or equal to 60
? ? ? ? Timers_Count = 0; ? ? ? ? ? // Count returns 0
? ? for (uint8_t i = 0; i < 7; i++) // Traverse the nixie tube control pin
? ? {
? ? ? ? digitalWrite(Seg_Control_Pin[0][i], bitRead(Seg_Code[Timers_Count / 10], i));
// Get from the nixie tube truth table whether the nixie tube should belit or off based on the tens digit value of the counted value
? ? ? ?
digitalWrite(Seg_Control_Pin[1][i], bitRead(Seg_Code[Timers_Count % 10], i));
// Get whether the nixie tube should be lit up or off from the nixie tube truth table according to the bits value of the counted value
? ? }
}
在loop函數中,每次執行為每一秒鐘的顯示,當timerc大于60時,重新計零。
**一個核心函數:bitRead**
參數如下:
```
bitRead(x, n):
x: the number from which to read(用來讀取的數字)
n: which bit to read, starting at 0 for the least-significant (rightmost) bit(該數字對應的二進制數的位數)
```
也就是說,bitRead從給定的參數中讀取一個位,也就是讀取一個0或1的值
總結
以上是生活随笔為你收集整理的arduino 电子时钟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 基础教程:集成
- 下一篇: SCI 和 JCR