基于tinkercad网站的arduino电子时钟

519 阅读1分钟

1.七段数码管的电路连接如图:

3f5a71dc6a62a0786434d46e3a859d9.png

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 table

const 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使得七段数码管的引脚编号只能被检索,不能被改变

{
    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

{
    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的值