|
|
本帖最后由 kpj001 于 2025-10-27 23:32 编辑
视频先登:https://www.bilibili.com/video/BV1s8sDzwEWg/
实物介绍(非广告):https://item.taobao.com/item.htm?id=800826018081
数码管通常分为共阳共阴,共同特点引脚比较多,一般我都不用的。
最近流行“查理复用(Charlieplexing)数码管”,就是单排引脚这种。
到手发现居然没有现成驱动,于是试着从 01 10 这样的组合开始测试,几个回合就找出来段码。
这样需要显示0-199 整数的场合,直插面包板style,就多了一种及其便宜的选择(1毛钱一颗)。
代码为 arduino uno nano D6-D2 直插【看视频】,作为点亮测试还是可以的,实际使用可以考虑用中断代替延时。
当然这款数码管搭配arduino略显浪费,STC DIP 封装的才是良配,代码略微修改即可。
- const int seg188_pins[] = {6, 5, 4, 3, 2};
- int segDuration = 1; // 每个段点亮时间,实测1ms合适
- bool digits[10][7] = { // 7段数码管显示数字所需点亮之段码,格式: {a, b, c, d, e, f, g}
- {1, 1, 1, 1, 1, 1, 0}, // 0
- {0, 1, 1, 0, 0, 0, 0}, // 1
- {1, 1, 0, 1, 1, 0, 1}, // 2
- {1, 1, 1, 1, 0, 0, 1}, // 3
- {0, 1, 1, 0, 0, 1, 1}, // 4
- {1, 0, 1, 1, 0, 1, 1}, // 5
- {1, 0, 1, 1, 1, 1, 1}, // 6
- {1, 1, 1, 0, 0, 0, 0}, // 7
- {1, 1, 1, 1, 1, 1, 1}, // 8
- {1, 1, 1, 1, 0, 1, 1} // 9
- };
- int currentNumber = 0;
- unsigned long lastChangeTime = 0;
- int displayCounter = 0;
- void setup() {
- Serial.begin(115200);
- }
- void loop() {
- showNumber(currentNumber);
-
- // 每隔一段时间改变显示的数字(测试用)
- if (millis() - lastChangeTime > 100) {
- lastChangeTime = millis();
- currentNumber = (currentNumber + 1) % 200; // 循环显示0-199
- Serial.println("Displaying: " + String(currentNumber));
- }
- }
- void showNumber(int a) { // 数字范围 0-199
- int tens = (a % 100) / 10;
- int ones = a % 10;
- showDigit(3, ones); // 个位
- if(a>9)showDigit(2, tens); // 十位
- if(a>99)showDigit(1, 1); // 百位
- }
- void showDigit(int digitPos, int number) {
- bool* segments = digits[number];
- // 个位
- if (digitPos == 3) {
- if (segments[0]) lightSegment(0, 1); // a3
- if (segments[1]) lightSegment(1, 0); // b3
- if (segments[2]) lightSegment(0, 2); // c3
- if (segments[3]) lightSegment(2, 0); // d3
- if (segments[4]) lightSegment(0, 3); // e3
- if (segments[5]) lightSegment(3, 0); // f3
- if (segments[6]) lightSegment(4, 0); // g3
- }
- // 十位
- else if (digitPos == 2) {
- if (segments[0]) lightSegment(1, 2); // a2
- if (segments[1]) lightSegment(2, 1); // b2
- if (segments[2]) lightSegment(3, 2); // c2
- if (segments[3]) lightSegment(3, 1); // d2
- if (segments[4]) lightSegment(4, 1); // e2
- if (segments[5]) lightSegment(4, 2); // f2
- if (segments[6]) lightSegment(4, 3); // g2
- }
- // 百位,比较特殊,只有显示1 和不显示两种状态
- else if (digitPos == 1) {
- lightSegment(2, 3); // b1 (对应a1)
- lightSegment(1, 3); // c1 (对应b1)
- }
- }
- void lightSegment(int anodePin, int cathodePin) {
- for (int i = 0; i < 5; i++) pinMode(seg188_pins[i], INPUT);
- pinMode(seg188_pins[anodePin], OUTPUT);digitalWrite(seg188_pins[anodePin], HIGH);
- pinMode(seg188_pins[cathodePin], OUTPUT);digitalWrite(seg188_pins[cathodePin], LOW);
- delay(segDuration);
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|