MAX7219多级联串行控制多个点阵/数码管的详解(附源码)

网上一般都是数码管例子, 这里我就用点阵吧, 其实原理是一样的~


一,先从单个MAX7219说起.

接法如下图:(DIG0~7为位选, DP~A为段选)(也即 DIG0~7为选行, DP~A为给选中的行显示怎样的"形")

(这里位选为低电平亮, 段选为高电平亮. 觉得晕的话无视, 看代码就懂了)

MAX7219多级联串行控制多个点阵/数码管的详解(附源码)_第1张图片


至于图中下面的DP,ABCDEFG为什么要那样接, 得参考MAX7219的手册, 如图:

MAX7219多级联串行控制多个点阵/数码管的详解(附源码)_第2张图片


下图是取模示例, 没接触过的同学可以参考一下:

MAX7219多级联串行控制多个点阵/数码管的详解(附源码)_第3张图片


下面是输入端与单片机的连接, (对应下面的代码,LOAD等价于CS):

MAX7219多级联串行控制多个点阵/数码管的详解(附源码)_第4张图片


二,  3级联示例(多级联可依次类推)

其实也就是DOUT接到下一级的DIN,   所有的MAX7219的LOAD与CLK都共端.

MAX7219多级联串行控制多个点阵/数码管的详解(附源码)_第5张图片


三. 代码,3级联

效果为: 三个8x8点阵屏同时循环显示从"0~9,A~Z,中,国" 等字符.

#include 
#include 
#define uchar unsigned char
#define uint  unsigned int
//定义Max7219端口
sbit Max7219_pinCLK = P2^2;
sbit Max7219_pinCS  = P2^1;
sbit Max7219_pinDIN = P2^0;
uchar code disp1[38][8]={
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
{0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
{0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
{0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
{0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
{0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
{0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
{0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
{0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
{0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
{0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
{0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
{0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
{0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
{0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
{0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
{0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
{0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
{0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
{0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
{0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
{0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
{0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
{0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
{0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
{0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
{0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
{0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
{0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
{0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
{0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
{0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
{0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8},//中
{0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE},//国
};
/*n毫秒的延时程序*/
void Delay_xms(uint x)
{
 uint i,j;
 for(i=0;i=1;i--)
          {		  
            Max7219_pinCLK=0;
            Max7219_pinDIN=DATA&0x80; //&10000000, 编译器对位操作的理解:非0即为1
            DATA=DATA<<1;
            Max7219_pinCLK=1;		//上升沿把数据送出去
           }                                 
}
//-------------------------------------------
/*向M第一片MAX7219写入数据*/
void Write_Max7219_1(uchar add1,uchar dat1)
{ 
     Max7219_pinCS=0;
	 Write_Max7219_byte(add1);           //写入地址,即数码管编号
     Write_Max7219_byte(dat1);               //写入数据,即数码管显示数字 
	 Max7219_pinCS=1;                        
}
/*第二片MAX7219的写入数据*/
void Write_Max7219_2(uchar add2,uchar dat2)
{
	Max7219_pinCS=0;
	Write_Max7219_byte(add2);
	Write_Max7219_byte(dat2);
	Max7219_pinCLK=1;
	Write_Max7219_byte(0x00);  //片1写入空
	Write_Max7219_byte(0x00);
	Max7219_pinCS=1;
}
/*第三片MAX7219的写入数据*/
void Write_Max7219_3(uchar add3,uchar dat3)
{
	Max7219_pinCS=0;
	Write_Max7219_byte(add3);
	Write_Max7219_byte(dat3);
	Max7219_pinCLK=1;
	Write_Max7219_byte(0x00); //片1写入空
	Write_Max7219_byte(0x00);
	Write_Max7219_byte(0x00); //片2写入空
	Write_Max7219_byte(0x00);
	Max7219_pinCS=1;
}


/*初始化芯片*/
void Init_MAX7219(void)
{
 Write_Max7219_1(0x09, 0x00);       //译码方式:BCD码
 Write_Max7219_1(0x0a, 0x03);       //亮度 
 Write_Max7219_1(0x0b, 0x07);       //扫描界限;8个数码管显示
 Write_Max7219_1(0x0c, 0x01);       //掉电模式:0,普通模式:1
 Write_Max7219_1(0x0f, 0x00);       //显示测试:1;测试结束,正常显示:0

 Write_Max7219_2(0x09, 0x00);       //译码方式:BCD码
 Write_Max7219_2(0x0a, 0x03);       //亮度 
 Write_Max7219_2(0x0b, 0x07);       //扫描界限;8个数码管显示
 Write_Max7219_2(0x0c, 0x01);       //掉电模式:0,普通模式:1
 Write_Max7219_2(0x0f, 0x00);       //显示测试:1;测试结束,正常显示:0

 Write_Max7219_3(0x09, 0x00);       //译码方式:BCD码
 Write_Max7219_3(0x0a, 0x03);       //亮度 
 Write_Max7219_3(0x0b, 0x07);       //扫描界限;8个数码管显示
 Write_Max7219_3(0x0c, 0x01);       //掉电模式:0,普通模式:1
 Write_Max7219_3(0x0f, 0x00);       //显示测试:1;测试结束,正常显示:0
}


/**********************主程序**************************/
void main(void)
{
 uchar i,j;
 Delay_xms(50);
 Init_MAX7219();  
 while(1)
 {
  for(j=0;j<38;j++)
  {
   for(i=1;i<9;i++)
   {
    Write_Max7219_1(i,disp1[j][i-1]);
	Write_Max7219_2(i,disp1[j][i-1]);
	Write_Max7219_3(i,disp1[j][i-1]);
	}
   Delay_xms(1000);
  }  
 } 
}


希望对大家的学习有帮助!

你可能感兴趣的:(嵌入式C和linux,单片机,max7219,点阵屏)