8只数码管滚动显示数字
时间: 2024-02-17 12:34:42 浏览: 54
8只数码管滚动显示数字串.zip
以下是使用Arduino编写的滚动显示数字的代码:
```
const int digitPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // 数码管8个引脚的控制管脚
const int digitCount = 8; // 数码管数量
const int numCount = 10; // 数字数量
const int numPins[][7] = { // 数字的引脚状态
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}, // 0
{LOW, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 1
{HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH}, // 2
{HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH}, // 3
{LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH}, // 4
{HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH}, // 5
{HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH}, // 6
{HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW}, // 7
{HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}, // 8
{HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH} // 9
};
const int digitDelay = 5; // 数码管之间的延迟
const int scrollDelay = 100; // 滚动延迟
void setup() {
for (int i = 0; i < digitCount; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
void loop() {
for (int num = 0; num < numCount; num++) { // 循环显示数字 0-9
for (int i = 0; i < digitCount; i++) { // 循环显示每个数码管
for (int j = 0; j < 7; j++) { // 设置数码管引脚状态
digitalWrite(j + 2, numPins[num][j]);
}
digitalWrite(digitPins[i], HIGH); // 打开当前数码管
delay(digitDelay);
digitalWrite(digitPins[i], LOW); // 关闭当前数码管
}
delay(scrollDelay);
}
}
```
这段代码可以控制8个数码管显示数字0-9,并且数字会从右向左滚动显示。其中`digitPins`数组定义了8个数码管的控制管脚,`numPins`二维数组定义了数字0-9的引脚状态,`digitDelay`定义了数码管之间的延迟,`scrollDelay`定义了滚动延迟。在`loop()`函数中,首先循环显示数字0-9,然后循环显示每个数码管,并根据数字的引脚状态设置数码管的引脚状态,最后打开当前数码管并延迟一定时间后关闭。通过这种方式,就可以实现数码管的滚动显示了。
阅读全文