MAX7219LEDマトリクスモジュール

投稿者: | 2022-01-22

各局皆様、こんにちは。アマチュア無線局、JS2IIUです。

今回は写真のHiLetgo MAX7219 LEDマトリクスモジュールを手に入れたので、動かしてみました。4-in-1タイプの製品で、8×8 LEDマトリクスとMAX7219がセットになった基板が4つくっついたものになっています。バラせばそれぞれ独立して使うこともできます。こういうのはまた次にやる時にどうやるんだっけとなるので、手順を残しておきます。

目次

MAX7219

マキシムの8セグLEDのコントローラIC、MAX7219の詳しい情報は、本家のデータシート、アプリケーションノートにあります。今回は8セグではなく、8×8のLEDマトリクスです。配線のイメージはこちらのページの図が分かりやすいです。

LEDマトリクスとの間にMAX7219CWG+が見えています。

HiLetgoの商品

きちんと動作しました。DIYで使う分には問題ない品質です。ハンダを手作業でやっているのでしょうか、浮いていたり、傾いていたり・・・でもちゃんと動いています。Amazonのレビューではハンダ不良があって手直ししたとのコメントがありましたが、そこまでの問題はなかったので、今回はあたりを引いたのでしょう。

Arduinoとの接続

下の表に従って接続します。後から出てくるMD_MAX72xxのライブラリを使うのでこのように接続しています。今回使ったのはArduino nanoです。他のArduinoボードを使ったときは適宜読替えが必要です。

ArduinoLED Matrix Module
5VVCC
GNDGND
pin 11DIN
pin 10CS
pin 13CLK

サンプルプログラムの修正

Arduinoからモジュールを動かすには、MAX7219に対応するライブラリを使います。テスト用に使ったのは「MD_MAX72xx」です。他にもライブラリがあるので、用途に応じて選択するよ良いでしょう。このライブラリに同梱されている「MD_MAX72xx_Test」を使います。以下のソースは冒頭の29行目までを示しています。このままでも動作しますが、表示の向きがおかしいため、24行目を書き換えます。

// Program to exercise the MD_MAX72XX library
//
// Uses most of the functions in the library
#include <MD_MAX72xx.h>

// Turn on debug statements to the serial output
#define  DEBUG  1

#if  DEBUG
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x) Serial.print(F(x))
#define PRINTD(x) Serial.println(x, DEC)

#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTD(x)

#endif

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES	11

#define CLK_PIN   13  // or SCK
#define DATA_PIN  11  // or MOSI
#define CS_PIN    10  // or SS

書き換えが必要な部分。

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW

以下のように変更します。FC16_HWにします。

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW

この変更に関する詳細はこちら(MD_MAX72xx LED Matrix Arduino Library)を参照してください。Module Type enumerated type.のところに詳しく書かれています。

動作の様子はこちらです。

好きな文字列を表示させる

自由に文字列を表示させたいので、サンプルプログラムをベースにしていじってみます。サンプルプログラムは「MD_MAX72xx_DaftPunk」を使います。変更箇所は以下の通りです。

44行目をPAROLA_HWからFC16_HWに変更します。

// --------------------
// MD_MAX72xx hardware definitions and object
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
//
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 11
#define CLK_PIN   13  // or SCK
#define DATA_PIN  11  // or MOSI
#define CS_PIN    10  // or SS

表示させたい文字列は98行目のconst char *msgTab[]の中身を置き換えます。

// ========== Text routines ===========
//
// Text Message Table
// To change messages simply reorder, add to, or delete from, this table
const char *msgTab[] =
{
  "DAFT PUNK",
  "GET LUCKY",
  "ONE MORE TIME",
  "HARDER  BETTER  FASTER  STRONGER",
  "HUMAN AND ROBOT",
  "TECHNOLOGIC",
};

メインループrunMatrixAnimation関数からさまざまなデモを呼び出しています。変数stateの値を見て、呼び出すデモが決まります。このstateが0の時に文字列を表示するデモが呼び出されます。デモのためにstateの値がインクリメントされるようになっていますので、インクリメントされずに、常に0になるよう書き換えます。952行目と956行目のインクリメントをコメントアウトすることで、常に文字列表示のデモが繰り返されるようになります。

void runMatrixAnimation(void)
// Schedule the animations, switching to the next one when the
// the mode switch is pressed.
{
  static  uint8_t state = 0;
  static  uint8_t mesg = 0;
  static  boolean bRestart = true;
	static boolean	bInMessages = false;
  boolean changeState = false;

#if RUN_DEMO
  // check if one second has passed and then count down the demo timer. Once this
  // gets to zero, change the state.
  if (millis()-prevTimeDemo >= 1000)
  {
    prevTimeDemo = millis();
    if (--timeDemo == 0)
    {
      timeDemo = DEMO_DELAY;
      changeState = true;
    }
  }
#else
  // check if the switch is pressed and handle that first
  changeState = (ks.read() == MD_UISwitch::KEY_PRESS);
#endif
  if (changeState)
  {
    if (bInMessages) // the message display state
    {
      mesg++;
      if (mesg >= sizeof(msgTab)/sizeof(msgTab[0]))
      {
        mesg = 0;
    	bInMessages = false;
        state++;
      }
    }
    else
      state++;

    bRestart = true;
  };

  // now do whatever we do in the current state
  switch(state)
  {
    case  0: bInMessages = true; bRestart = scrollText(bRestart, msgTab[mesg]); break;
    case  1: bRestart = graphicMidline1(bRestart);       break;
    case  2: bRestart = graphicMidline2(bRestart);       break;
    case  3: bRestart = graphicScanner(bRestart);        break;
    case  4: bRestart = graphicRandom(bRestart);         break;
    case  5: bRestart = graphicFade(bRestart);           break;
    case  6: bRestart = graphicSpectrum1(bRestart);      break;
    case  7: bRestart = graphicHeartbeat(bRestart);      break;
    case  8: bRestart = graphicHearts(bRestart);         break;
    case  9: bRestart = graphicEyes(bRestart);           break;
    case 10: bRestart = graphicBounceBall(bRestart);     break;
    case 11: bRestart = graphicArrowScroll(bRestart);    break;
    case 12: bRestart = graphicScroller(bRestart);       break;
    case 13: bRestart = graphicWiper(bRestart);          break;
    case 14: bRestart = graphicInvader(bRestart);        break;
    case 15: bRestart = graphicPacman(bRestart);         break;
    case 16: bRestart = graphicArrowRotate(bRestart);    break;
    case 17: bRestart = graphicSpectrum2(bRestart);      break;
    case 18: bRestart = graphicSinewave(bRestart);       break;

    default: state = 0;
  }
}

参考

最後まで読んでいただきありがとうございました。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です