写一段动态数码管显示程序,按下k1k2数 码管数字加一,按下k3k4数码管数字减一,K1k3 控制数码管左四位,k2k4控制数码管右四位。 (加需要能加到100,减需要能减到-10)。
时间: 2023-04-07 15:00:40 浏览: 53
以下是动态数码管显示程序的示例代码:
```
#include <TM1637.h>
#define CLK 2
#define DIO 3
TM1637 tm1637(CLK, DIO);
int leftNum = 0;
int rightNum = 0;
void setup() {
tm1637.init();
tm1637.setBrightness(7);
}
void loop() {
if (digitalRead(4) == LOW) { // K4 pressed
if (rightNum > -10) {
rightNum--;
tm1637.displayNum(leftNum, rightNum);
}
delay(200);
}
if (digitalRead(3) == LOW) { // K3 pressed
if (leftNum > 0) {
leftNum--;
tm1637.displayNum(leftNum, rightNum);
}
delay(200);
}
if (digitalRead(2) == LOW) { // K2 pressed
if (rightNum < 100) {
rightNum++;
tm1637.displayNum(leftNum, rightNum);
}
delay(200);
}
if (digitalRead(1) == LOW) { // K1 pressed
if (leftNum < 9) {
leftNum++;
tm1637.displayNum(leftNum, rightNum);
}
delay(200);
}
}
```
这段程序使用了TM1637库来控制动态数码管的显示。在setup()函数中,初始化了TM1637对象并设置了亮度。在loop()函数中,通过检测按键状态来实现数码管数字的加减和位置的控制。每次数字变化后,调用displayNum()函数来更新数码管的显示。