|
在各种测试台中,会看到一些显示电流、电压的模块,这些模块多数采用多位数码管。在本文中,我们将主要介绍如何基于Arduino开发板使用TM1637显示模块。
TM1637显示模块简介 该显示模块基于TM1637驱动器,是一种4位七段显示模块,可显示各种输出。它仅使用两个引脚(SCL和SDA)与微控制器通信。TM1637显示模块的芯片允许我们扫描8*2键盘。该模块还提供八种不同的模式来调整显示背景光。此外,它在3.5至5V的电压范围内工作。
TM1637显示模块的引脚分布 该显示模块有四个引脚,如下图所示:
● VCC:显示电源(3.3 至 5.5V) ● GND:接地 ● SLC:I2C数据同步 ● SDA:I2C数据信息
TM1637显示模块与Arduino的硬件连接 将模块连接到Arduino开发板,如下图所示。
代码 首先将TM1637库安装到您的Arduino IDE上,该库的下载地址:https://github.com/AKJ7/TM1637。
将以下代码上传到您的Arduino开发板。 - /**
- * @file basic.ino
- * @ingroup examples
- * @brief Basic library usage example
- *
- * This example shows how to display different types of values on the display.
- */
- /**
- *
- * API
- * class TM1637 {
- public:
- static constexpr uint8_t TOTAL_DIGITS = 4;
- TM1637(uint8_t clkPin, uint8_t dataPin) noexcept;
- TM1637(const TM1637&) = delete;
- TM1637& operator=(const TM1637&) = delete;
- ~TM1637() = default;
- void begin();
- inline void init();
- inline Animator* refresh();
- template <typename T>
- typename type_traits::enable_if<
- type_traits::is_string<T>::value ||
- type_traits::is_floating_point<T>::value ||
- type_traits::is_integral<T>::value,
- Animator*>::type
- display(const T value, bool overflow = true, bool pad = false, uint8_t offset = 0);
- Animator* displayRawBytes(const uint8_t* buffer, size_t size);
- void offMode() const noexcept;
- void onMode() const noexcept;
- inline void colonOff() noexcept;
- inline void colonOn() noexcept;
- inline Animator* switchColon() noexcept;
- void clearScreen() noexcept;
- inline void setDp(uint8_t value) noexcept;
- inline uint8_t getBrightness() const noexcept;
- void changeBrightness(uint8_t value) noexcept;
- void setBrightness(uint8_t value) noexcept;
- };
- class Animator
- {
- Animator(uint8_t clkPin, uint8_t dataPin, uint8_t totalDigits);
- void blink(Tasker::duration_type delay);
- void fadeOut(Tasker::duration_type delay);
- void fadeIn(Tasker::duration_type delay);
- void scrollLeft(Tasker::duration_type delay);
- void off() const;
- void on(DisplayControl_e brightness) const;
- void reset(const String& value);
- void clear();
- void refresh();
- }
- struct DisplayDigit
- {
- DisplayDigit& setA();
- DisplayDigit& setB();
- DisplayDigit& setC();
- DisplayDigit& setD();
- DisplayDigit& setE();
- DisplayDigit& setF();
- DisplayDigit& setG();
- DisplayDigit& setDot();
- operator uint8_t();
- }
- */
- #include <TM1637.h>
- // Instantiation and pins configuration
- // Pin 3 - > DIO
- // Pin 2 - > CLK
- TM1637 tm(2, 3);
- void setup()
- {
- tm.begin();
- tm.setBrightness(4);
- }
- void loop()
- {
- // Display Integers:
- tm.display(1234);
- // delay(1000);
- //
- // // Display float:
- // tm.display(29.65);
- // delay(1000);
- //
- // // Display String:
- // tm.display("PLAY");
- // delay(1000);
- // tm.display("STOP");
- // delay(1000);
- }
复制代码
此代码在模块显示两个数字和两个单词。 |