求一个arduino用四位共阴数码管显示数字从0到9999的代码
1条回答 默认 最新
- 编程乐学 2023-12-15 13:32关注
蹲个点赞
// 定义数码管引脚 int digitPins[] = {2, 3, 4, 5}; int segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; // 数码管显示的数字 int num = 0; void setup() { // 初始化引脚 for (int i = 0; i < 4; i++) { pinMode(digitPins[i], OUTPUT); } for (int i = 0; i < 8; i++) { pinMode(segmentPins[i], OUTPUT); } } void loop() { // 数码管显示数字 displayNumber(num); // 数字递增 num++; if (num > 9999) { num = 0; } // 延迟1秒 delay(1000); } // 数码管显示函数 void displayNumber(int number) { // 将数字拆分成个位、十位、百位和千位 int digit[4]; digit[0] = number % 10; digit[1] = (number / 10) % 10; digit[2] = (number / 100) % 10; digit[3] = (number / 1000) % 10; // 显示数字 for (int i = 0; i < 4; i++) { // 选中对应的数码管 digitalWrite(digitPins[i], LOW); // 设置段选引脚状态 for (int j = 0; j < 8; j++) { digitalWrite(segmentPins[j], HIGH); } // 显示对应的数字 switch (digit[i]) { case 0: digitalWrite(segmentPins[6], LOW); break; case 1: digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[2], LOW); break; case 2: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[6], LOW); digitalWrite(segmentPins[4], LOW); digitalWrite(segmentPins[3], LOW); break; case 3: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[6], LOW); digitalWrite(segmentPins[2], LOW); digitalWrite(segmentPins[3], LOW); break; case 4: digitalWrite(segmentPins[5], LOW); digitalWrite(segmentPins[2], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[6], LOW); break; case 5: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[5], LOW); digitalWrite(segmentPins[6], LOW); digitalWrite(segmentPins[2], LOW); digitalWrite(segmentPins[3], LOW); break; case 6: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[5], LOW); digitalWrite(segmentPins[6], LOW); digitalWrite(segmentPins[4], LOW); digitalWrite(segmentPins[3], LOW); digitalWrite(segmentPins[2], LOW); break; case 7: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[2], LOW); break; case 8: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[2], LOW); digitalWrite(segmentPins[3], LOW); digitalWrite(segmentPins[4], LOW); digitalWrite(segmentPins[5], LOW); digitalWrite(segmentPins[6], LOW); break; case 9: digitalWrite(segmentPins[0], LOW); digitalWrite(segmentPins[1], LOW); digitalWrite(segmentPins[2], LOW); digitalWrite(segmentPins[5], LOW); digitalWrite(segmentPins[6], LOW); digitalWrite(segmentPins[3], LOW); break; } // 延迟一段时间 delay(1); // 关闭数码管 for (int j = 0; j < 8; j++) { digitalWrite(segmentPins[j], HIGH); } digitalWrite(digitPins[i], HIGH); } }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
- ¥15 led数码显示控制(标签-流程图)
- ¥20 为什么在复位后出现错误帧
- ¥15 结果有了,想问一下这个具体怎么输入
- ¥15 怎么修改鸿蒙app的UI及功能设计
- ¥15 帮我利用jupyter 运行一个正确的代码
- ¥15 如何使用Gephi软件和Python包中的GephiStreamer交互
- ¥15 sqlite加密问题咨询
- ¥15 appdesigner接收不到udp组播的数据
- ¥15 verilog 非阻塞赋值下的移位拼接错误