max7219是一個(gè)用于驅(qū)動(dòng)8位7段數(shù)字LED或者8x8點(diǎn)陣LED的驅(qū)動(dòng)芯片, 以列掃描的方式, 用16個(gè)pin管理64個(gè)發(fā)光點(diǎn), 顯示8個(gè)數(shù)字時(shí)刷新率為500-1300Hz, 典型值為800Hz.
pin腳功能
* DIG0 - DIG7: 這個(gè)8個(gè)pin, 分別代表一個(gè)7段數(shù)字+點(diǎn)號(hào)
* SEGA - SEGG, DP: A-G代表7段數(shù)字里的每一段, DP代表Decimal Point, 小數(shù)點(diǎn)
* 每一段的驅(qū)動(dòng)電流是40mA
* V+, GND: 電壓和接地
* DIN: 串行數(shù)據(jù)輸入
* CS: 片選, 當(dāng)電平下拉后從串行口按時(shí)脈移入移位寄存器, 當(dāng)電平上拉后鎖存
* DOUT: 串行數(shù)據(jù)輸出, 這個(gè)口用于級(jí)聯(lián)
* 級(jí)聯(lián)的N個(gè)MAX7219, 可以看成有N個(gè)串聯(lián)的16位移位寄存器
* 如果要對(duì)第N位操作, 要經(jīng)過16*N個(gè)時(shí)脈后數(shù)據(jù)才能到達(dá), 這時(shí)候CS上拉, 將命令鎖存.
通信方式
* 通信協(xié)議為SPI, 接受SPI Master的輸入, 不返回?cái)?shù)據(jù)
* MAX7219, 不管CS是什么電平, DIN的數(shù)據(jù)都會(huì)隨著時(shí)脈寫入移位寄存器
* MAX7221, 只有當(dāng)CS下拉時(shí), 才會(huì)將數(shù)據(jù)從DIN寫入, 或者往DOUT寫出
* CS必須在第16個(gè)數(shù)寫入后, 下一個(gè)時(shí)脈上升沿到來前拉高, 否則數(shù)據(jù)會(huì)丟失
* 數(shù)據(jù)是MSB, 大值在前
* 對(duì)于16個(gè)bit, 前8個(gè)(D15-D08)為地址, 實(shí)際上使用的只有D11-D8這4個(gè), 后8個(gè)D7-D0為數(shù)據(jù)
通信的前8位地址
共有14個(gè)
* 0x?0: No-Op, 用于將數(shù)據(jù)輸出到DOUT
* 0x?1: 第一個(gè)數(shù)字
* 0x?2: 第二個(gè)
* 0x?3: 第三個(gè)
* 0x?4: 第四個(gè)
* 0x?5: 第五個(gè)
* 0x?6: 第六個(gè)
* 0x?7: 第七個(gè)
* 0x?8: 第八個(gè)
* 0x?9: 數(shù)字解碼模式
* 0x?A: 亮度, 0x00到0xFF
* 0x?B: 掃描限制(數(shù)字個(gè)數(shù)), 這個(gè)對(duì)亮度有影響, 如果兩個(gè)級(jí)聯(lián)的這個(gè)參數(shù)不一樣, 亮度會(huì)不一樣
* 0x?C: 狀態(tài)(關(guān)閉, 普通)
* 0x?F: 測(cè)試狀態(tài)(測(cè)試, 普通)
級(jí)聯(lián)傳輸
* 使用No-Op地址操作實(shí)現(xiàn)級(jí)聯(lián)傳輸
* 例如, 對(duì)第四塊MAX7219寫入
* 首先按預(yù)設(shè)地址寫入地址, 寫入值
* 寫入三次NoOp操作(0x?0??), ?號(hào)代表隨意值
* 當(dāng)CS拉高后, 這4個(gè)MAX7219都會(huì)收到操作地址和操作值, 但是前三塊看到的是NoOp, 所以前三塊無動(dòng)作
使用STC12C5A60S2的硬件SPI驅(qū)動(dòng)MAX7219點(diǎn)陣
STC12C5A60S2系列是帶內(nèi)建SPI支持的, 基于HML_FwLib_STC12封裝庫(kù)可以方便的實(shí)現(xiàn)MAX7219的點(diǎn)陣驅(qū)動(dòng), 項(xiàng)目地址 https://github.com/IOsetting/HML_FwLib_STC12 , 驅(qū)動(dòng)代碼:
- /*****************************************************************************/
- /**
- * \file spi_max7219.c
- * \author IOsetting | iosetting@outlook.com
- * \date
- * \brief Example code of SPI driving dot matrix module
- * \note The module chip is MAX7219, pin connection:
- * P1_3 => CS,
- * P1_5(MOSI) => DIN,
- * P1_7(SPCLK) => CLK
- *
- * \version v0.1
- * \ingroup example
- * \remarks test-board: Minimum System; test-MCU: STC12C5AF56S2
- ******************************************************************************/
- /*****************************************************************************
- * header file *
- *****************************************************************************/
- #include "hml/hml.h"
- #define CS P1_3
- #define DECODE_MODE 0x09
- #define INTENSITY 0x0A
- #define SCAN_LIMIT 0x0B
- #define SHUT_DOWN 0x0C
- #define DISPLAY_TEST 0x0F
- const byte numbers[]={
- 0x00,0x00,0x7C,0xC6,0xC6,0xCE,0xD6,0xD6, // -0-.
- 0xE6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18, // -1-
- 0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30, // -2-
- 0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06, // -3-
- 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE, // -4-
- 0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00,
- 0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x0E, // -5-
- 0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6, // -6-
- 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
- 0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18, // -7-
- 0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6, // -8-
- 0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06, // -9-
- 0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00};
- void Write7219(byte addr, byte dat)
- {
- CS = 0;
- SPI_RW(addr);
- SPI_RW(dat);
- CS = 1;
- }
- /*****************************************************************************/
- /**
- * \author IOsetting
- * \date
- * \brief initialize MAX7219
- * \param[in]
- * \return none
- * \ingroup example
- * \remarks
- ******************************************************************************/
- void Init7219(void)
- {
- Write7219(SHUT_DOWN,0x01); // 0x00:shutdown, 0x01:normal
- Write7219(DECODE_MODE,0x00); // No decode
- Write7219(SCAN_LIMIT,0x07); // Display 8 digits
- Write7219(INTENSITY,0x00); // 0x00:min, 0xFF:max
- Write7219(DISPLAY_TEST,0x00); // 0x00:normal, 0x01:test mode
- }
- /*****************************************************************************/
- /**
- * \author IOsetting
- * \date
- * \brief initialize SPI
- * \param[in]
- * \return none
- * \ingroup example
- * \remarks
- ******************************************************************************/
- void initSys(void)
- {
- SPI_configTypeDef sc;
- sc.baudRatePrescaler = SPI_BaudRatePrescaler_64;
- sc.cpha = SPI_CPHA_1Edge;
- sc.cpol = SPI_CPOL_low;
- sc.firstBit = SPI_FirstBit_MSB;
- sc.pinmap = SPI_pinmap_P1;
- sc.nss = SPI_NSS_Soft;
- sc.mode = SPI_Mode_Master;
- SPI_config(&sc);
- SPI_cmd(ENABLE);
- }
- void main()
- {
- initSys();
- Init7219();
- P1_3 = 1;
- byte pos = 0, size = sizeof(numbers), i, j;
- while(1)
- {
- for (i = 0; i < 8; i++)
- {
- j = (pos + i) % size;
- Write7219(i + 1, numbers[j]);
- }
- pos = (pos + 1) % size;
- sleep(100);
- }
- }
復(fù)制代碼
|