Arduino使用TM1637四位数码管

本文详细介绍了TM1637 LED驱动控制芯片及其在4位数码管上的应用,包括Grove-4-DigitDisplay库的使用,通过实例展示如何利用TM1637和DS1302实现实时时钟功能。同时,深入探讨了SevenSegmentTM1637 Arduino库的基本和高级方法,为开发者提供了丰富的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 简介

TM1637是一种带键盘扫描接口的LED驱动控制专用电路,内部集成有MCU数字接口,数据锁存器,LED高压驱动,键盘扫描等电路。
4位数码管:这里用的是TM1637驱动的数码管

引脚

数码管有四个引脚,意义如下:

  • GND:电源负极
  • VCC:电源正极,+5V
  • DIO:数据IO模块,可以接任意的数字引脚
  • CLK:时钟引脚,可以接任意的数字引脚。

2. Grove - 4-Digit Display Library

该库提供了一个名为TM1637的类,类中包含一个构造函数,形式为
TM1637(uint8_t Clk, uint8_t Data),第一个参数指定时钟引脚,第二个参数指定数据引脚,这两个引脚都是数字引脚。

2.1类中比较实用的函数有:

  • init函数:用于数码管初始化,就是清除数码管中的显示内容
  • point函数:指定数码管中间的冒号是否显示,输入参数为布尔值,为0时不显示,不为0时显示
  • display(uint8_t BitAddr,int8_t DispData)函数:指定数码管中某一位的显示内容,第一个参数指定数码管的某一位,四位数码管从左到右顺序为0,1,2,3,第二个参数是显示的内容,其实是个序号,库中自带有一个长度为16的数组,默认显示0-9,A-F,第二个参数指定的是这个数组的序号。
  • display(int8_t DispData[])函数:输入参数为一个序号数组,指定四位数码管中每一位要显示的内容。

2.1.1 库中提供了三个示例

分别为:ClockDisplay、NumberFlow、Stopwatch,其中:

  • ClockDisplay:时钟显示示例,显示小时和分钟。
  • NumberFlow:循环显示数组内容示例
  • Stopwatch:这个是个秒表的示例

2.2 库文件及帮助

数码管(4位数码管(芯片TM1637))使用库文件:Grove4-Digit Display:https://github.com/Seeed-Studio/Grove_4Digital_Display
库帮助文档:http://wiki.seeedstudio.com/Grove-4-Digit_Display/

在这里插入图片描述

2.3 利用tm1637和DS1302制作时钟代码

#include <TimerOne.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include "TM1637.h"
#define TimeCLK 7
#define TimeDIO 8
TM1637 tm1637(TimeCLK, TimeDIO);

ThreeWire myWire(4, 5, 2); //DAT, CLK, RST
RtcDS1302<ThreeWire> Rtc(myWire);

int8_t time_disp[] = {0x00, 0x00, 0x00, 0x00};
unsigned char update;
unsigned char clock_point = 0;
unsigned char minute = 0;
unsigned char hour = 12;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Rtc.Begin();
  /* RtcDateTime dt=RtcDateTime(__DATE__, __TIME__);
    Rtc.SetDateTime(dt);
  */
  RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  if (Rtc.GetIsWriteProtected())
  {
    Serial.println("RTC was write protected, enabling writing now");
    Rtc.SetIsWriteProtected(false);
  }

  if (!Rtc.GetIsRunning())
  {
    Serial.println("RTC was not actively running, starting now");
    Rtc.SetIsRunning(true);
  }

  RtcDateTime now = Rtc.GetDateTime();
  if (now < compiled)
  {
    Serial.println("RTC is older than compile time!  (Updating DateTime)");
    Rtc.SetDateTime(compiled);
  }
  tm1637.set();
  tm1637.init();
  Timer1.initialize(1000000);//timing for 500ms
  Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR

}

void loop() {
  if (update) {
    time_update();
    tm1637.display(time_disp);
  }

}

void TimingISR() {
  update = 1;
  clock_point = (~clock_point) & 0x01;
}
void time_update() {
  if (clock_point) tm1637.point(POINT_ON);
  else tm1637.point(POINT_OFF);
  RtcDateTime now = Rtc.GetDateTime();
  minute = now.Minute();
  hour = now.Hour();
  time_disp[0] = hour / 10;
  time_disp[1] = hour % 10;
  time_disp[2] = minute / 10;
  time_disp[3] = minute % 10;
  update = 0;
}

3. SevenSegmentTM1637 Arduino Library

注意:此库中的示例代码值得研究,能更深刻认识数码管。

3.1 Basic methods

  • SevenSegmentTM1637(clkPin, dioPin):Creates a display object
  • init(): Initializes the display
  • print(value) Prints anything to the display (e.g. a string, number…)
  • clear() :Clears the display (and resets cursor)
  • home() :Resets cursor
  • setCursor(row, col) :Set the cursor to specified position
  • setBacklight(value): Sets the brightness of the display
  • on(): Turn the display on (set brightness to default)
  • off(): Turns the display off (set brightness to zero)
  • blink() :Blinks what ever is on the display
  • setColonOn(bool) :Sets the colon in the middle of the display
  • setPrintDelay(time) :Sets the delay when printing more than 4 characters

3.2 Advanced methods

  • encode(): Encodes a single digit, single char or char array (C string)
  • printRaw() :Prints raw byte(s) to the display
  • command() :Sends one or more raw byte commands to the display

3.3 Low level methods

  • comStart(): Serial communication start command
  • comWriteByte(bytes): Serial communication send byte command
  • comAck() :Serial communication get acknowledged command
  • comStop() :Serial communication stop command

If you still want or need more functionality, I’ve included two super classes:

  • SevenSegmentExtended() :Extends the base class with more usefull functions.
  • SevenSegmentFun(): Even more extensions for fun.

If you use any of these super classes, you will also get all the basic, advanced and low level methods as well. If you use the fun class extension you will get the extended class methods as well.

3.4 Extended class extra methods

  • SevenSegmentExtended(clkPin, dioPin):Creates a display object
  • printTime(hour, min) :Prints the time to the display
  • printDualCounter(leftValue, rightValue) :Prints two digits to the display

3.5 Fun class extra methods

  • SevenSegmentFun(clkPin, dioPin):Creates a display object
  • printLevelVertical(level): Prints 1-3 vertical levels e.g. volume, battery
  • printLevelHorizontal(levels): Prints 4 horizontal levels e.g. equalizer
  • scrollingText() Prints text and (keeps) scrolling
  • snake() :Classic snake demo
  • nightrider() :Nightrider Kit demo
  • bombTimer() :Count down a (bomb) timer
  • bouchingBall() :Bounching ball demo

For more extended information on what arguments all above functions accept and return see the header files of the classes (SevenSegmentTM1637.h, SevenSegmentExtended.h and SevenSegmentFun.h).

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蔚蓝慕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值