顯示具有 MAX7219 標籤的文章。 顯示所有文章
顯示具有 MAX7219 標籤的文章。 顯示所有文章

2021年6月18日

使用 MAX7219 驅動 8x8 矩陣式顯示器

本次的主角就是 8x8 矩陣顯示器:



不過如果只有 8x8 矩陣顯示器 是很難單獨運作的,必須要靠 MAX7219 這塊 IC 來處理字元的輸入與顯示。

組合在一起於是有了模組。



接線方式如下圖:


顯示結果如下影片:

參考網頁:
https://docs.labs.mediatek.com/resource/linkit7697-arduino/zh_tw/tutorial/driving-8x8-dot-matrices-with-max7219
https://forum.arduino.cc/t/max7219-led-matrix-rotate-text-8x8/438719
https://lastminuteengineers.com/max7219-dot-matrix-arduino-tutorial/
https://code.google.com/archive/p/arudino-maxmatrix-library/wikis/Example_Display_Characters.wiki
https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf

程式如下:

//
// Use one MAX7219 to control a 8x8 dot matrix
// http://www.icshop.com.tw/product_info.php/products_id/13181
//
// MAX7219 datasheet: https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf
//

// the MAX7219 address map (datasheet table 2)
#define MAX7219_DECODE_REG      (0x09)
#define MAX7219_INTENSITY_REG   (0x0A)
#define MAX7219_SCANLIMIT_REG   (0x0B)
#define MAX7219_SHUTDOWN_REG    (0X0C)
#define MAX7219_DISPLAYTEST_REG (0x0F)
#define MAX7219_DIGIT_REG(pos)  ((pos) + 1)
#define MAX7219_COLUMN_REG(pos) MAX7219_DIGIT_REG(pos)

// shutdown mode (datasheet table 3)
#define MAX7219_OFF             (0x0)
#define MAX7219_ON              (0x1)

// pin 13 of MAX7219 (CLK)
const int clock_pin = 8;
// pin 12 of MAX7219 (LOAD)
const int data_latch_pin = 9;
// pin 1 of MAX7219 (DIN)
const int data_input_pin = 10;

// number of columns of the display matrx
#define NUM_OF_COLUMNS  (8)
// for each character bitmap, it consumes 4 bytes
#define BYTE_PER_MAP    (5)

// matrix pattern for "Samba"
const byte char_pattern[] =
{
  B1000110, B1001001, B1001001, B0110010, B0000000, // S
  B0100000, B1010100, B1010100, B1111000, B0000000, // a
  B1111100, B0000100, B1111100, B0000100, B1111000, // m
  B1111111, B1000100, B1000100, B0111000, B0000000, // b
  B0100000, B1010100, B1010100, B1111000, B0000000, // a
  B0000000, B0000000, B0000000, B0000000, B0000000, // space
};

#define DISPLAY_STR_LENGTH  (sizeof(char_pattern) / BYTE_PER_MAP)

// update the register value of MAX7219
void set_register(byte address, byte value)  
{
  digitalWrite(data_latch_pin, LOW);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, address);
  shiftOut(data_input_pin, clock_pin, MSBFIRST, value);
  digitalWrite(data_latch_pin, HIGH);
}

void clear_matrix()
{
  // clear the dot matrix
  for (int i = 0; i < NUM_OF_COLUMNS; i++)
  {
    set_register(MAX7219_COLUMN_REG(i), B00000000);
  }
}

void init_max7219()
{
  // disable test mode. datasheet table 10
  set_register(MAX7219_DISPLAYTEST_REG, MAX7219_OFF);
  // set medium intensity. datasheet table 7
  set_register(MAX7219_INTENSITY_REG, 0xf);
  // turn off display. datasheet table 3
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_OFF);
  // drive 8 digits. datasheet table 8
  set_register(MAX7219_SCANLIMIT_REG, 7);
  // no decode mode for all positions. datasheet table 4
  set_register(MAX7219_DECODE_REG, B00000000);

  // clear matrix display
  clear_matrix();
}

void setup()  
{
  // init pin states
  pinMode(clock_pin, OUTPUT);
  pinMode(data_latch_pin, OUTPUT);    
  pinMode(data_input_pin, OUTPUT);

  // init MAX2719 states
  init_max7219();
}

unsigned int char_index = 0;

void loop()  
{
  int i;
 
  // turn off display first
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_OFF);

  // display one bitmap
  for (i = 0; i < BYTE_PER_MAP; i++)
  {
    // starting from column 2
    set_register(MAX7219_COLUMN_REG(2 + i), char_pattern[char_index * BYTE_PER_MAP + i]);
  }

  // turn on display
  set_register(MAX7219_SHUTDOWN_REG, MAX7219_ON);

  // step to the next character
  char_index++;
  // wrap around the character if needed
  if (char_index >= DISPLAY_STR_LENGTH)
  {
    char_index = 0;
  }
 
  delay(666);
}




2021年6月15日

7段顯示器

先講心得:
單純的 7 段顯示器,其實就是把 8 顆 LED 燈做在一塊板子上,透過排列的方式表示數字。這種板子,訓練大腦思惟,入門的玩一次就好了,真的要用這塊板子顯示數字、溫度、各種文字訊息,太可怕了、太不方便了;所以才會有 MAX7219 這種 IC 來控制 8x8 矩陣顯示器、 8 位數顯示器( 如下圖,中間那塊 IC 就是 MAX7219 )。而如果要把這種 8 顆 LED、8x8 矩陣顯示器、MAX7219 給國小學生使用,難度較高。


 



 

參考網頁: https://blog.jmaker.com.tw/4digit7seg/

參考語法:https://create.arduino.cc/editor/jasonshow/a9703023-e24f-4e3f-b0a2-5e666ff9bed4/preview
( 上面網頁的輸出與圖不同,必須修改程式的輸出腳位為 6~12 。另外,圖片的店阻色環也標示錯誤,請參考我的影片才是 220 。 )


 

/*
//本範例會在七段顯示LED上從9倒數到0
//傑森創工製作
//https://www.facebook.com/jasonshow

//本例預設是用共陰的顯示器
//若用的是共陽,除了要把接地改成接到5V
//另外在每個數字的表現方法,也要把0和1反過來
//1表示LED on,0則是LED off

// 七段用到的Arduino pin: 6,7,8,9,10,11,12
// 右下角的點用pin 5

//Arduino完整學習套件賣場:https://bre.is/E221Yubs7


傑森創工賣場:https://goo.gl/EWoPQ4

傑森創工粉絲團:
https://www.facebook.com/jasonshow
 */


//以下的陣列包含了0-9各數字的表現方法
byte seven_seg_digits[10][7] = { { 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,0,0,1,1 }   // = 9
                               };

void setup() {                
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  pinMode(10, OUTPUT);   
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  writeDot(0);  // 一開始右下角的點就是不顯示的
}

void writeDot(byte dot) {  //顯示右下角小數點
  digitalWrite(5, dot);
}
    
void sevenSegWrite(byte digit) {  //顯示特定數字的副程式
  byte pin = 6;  //我們的七段顯示是從pin6開始的
  for (byte segCount = 0; segCount < 7; ++segCount) {
    digitalWrite(pin, seven_seg_digits[digit][segCount]);
    ++pin;
  }
}

void loop() {
  //數字從9開始倒數,每個數字間隔1秒
  for (byte count = 10; count > 0; --count) {
   delay(1000);
   sevenSegWrite(count - 1);
  }
  delay(4000);
}