MAX7219驱动8X8点阵级联,翻页
2020-09-18 来源:51hei
买了几块MAX7219点阵模块,弄了个点阵级联,时间上翻显示,有秒点上下闪动,里面有两段,一段是点阵屏水平放置时数字上翻,另一段是点阵屏垂直放置时数字上翻,程序纯属业余爱好,高手勿喷,下面直接代码,3个IO口不能有其他器件,下面是压缩过的图像,有点变化,其实是没有拖影的,
单片机源程序如下:
#include #include #include #include #define uchar unsigned char #define uint unsigned int uchar timecount,point,sec; void timedeal(); void Delay_xms(uint x) { uint i,j; for(i=0;i } void main(void) { uchar i,j,a,b,c,d; IE=0x82; //允许定时器0工作 TR0=1; //定时器0可以工作 TMOD=0x01; //timer0为模式1 TH0 = 0x4c; TL0 = 0x00; Delay_xms(50); InitMAX7219(); while (1) { a=sec%10; c=sec/10; /*******************************以下屏幕旋转90度**************/ if(b==a) { for(i=0;i<8;i++) { for(j=0;j<8;j++) { Write_1(8-j,logo_1[a][j]); Write_2(8-j,logo_2[c][j]|logo_1[11][j]); } } } else { for(i=0;i<8;i++) { for(j=0;j<8;j++) { Write_1(8-j,(logo_1[b][j]<>8-i)); if(d==c) Write_2(8-j,logo_2[d][j]|logo_1[12][j]); else Write_2(8-j,(logo_2[d][j]<>8-i)|logo_1[12][j]); } Delay_xms(50); } } b=a; d=c; /*******************************以下屏幕旋转90度************** if(b==a) { for(j=1;j<9;j++) { Write_1(j,logo_3[b][j-1]); //1 Write_2(j,logo_4[d][j-1]|logo_3[11][j-1]); //2 } } else for(i=0;i<8;i++) { for(j=1;j<9;j++) { Write_1(j,logo_3[b][(j-1+i)]); //1 if(d==c)Write_2(j,logo_4[d][(j-1)]|logo_3[12][j-1]); //2 else Write_2(j,logo_4[d][(j-1+i)]|logo_3[12][j-1]);//2 } Delay_xms(50); } b=a; d=c; /**************************************************************************/ } } /****************************************************/ void time50ms(void) interrupt 1 //计时模式 { TH0 = 0x4c; TL0 = 0x00; timecount++; if(timecount==10) { timecount=0; point=!point; if(point) { sec++; if(sec>=60) sec=0; } } } /*************************************************/ #ifndef _MAX7219_H_ #define _MAX7219_H_ #include /*********************** 定义MAX7219 内部寄存器 **********************/ #define REG_DECODE 0x09 // 译码方式寄存器 #define REG_INTENSITY 0x0a // 亮度寄存器 #define REG_SCAN_LIMIT 0x0b // 扫描界限寄存器 #define REG_SHUTDOWN 0x0c // 停机寄存器 #define REG_DISPLAY_TEST 0x0f // 显示测试寄存器 #define INTENSITY_MIN 0x00 // 最小显示亮度 #define INTENSITY_MAX 0x0f // 最大显示亮度 /*********************** 定义MAX7219 端口信号 ************************/ sbit DATA=P3^5; sbit LOAD=P3^4; sbit CLK=P3^3; #define DATA_high DATA=1 #define DATA_low DATA=0 #define LOAD_high LOAD=1 #define LOAD_low LOAD=0 #define CLK_high CLK=1 #define CLK_low CLK=0 /************************** 函数原型说明 *****************************/ void Write_1 (unsigned char, unsigned char); void Write_2 (unsigned char, unsigned char); void SendByte (unsigned char); void Clear (void); /************************************************************************ * 函数原型:void Init() * 功 能: 对MAX7219进行初始化,调用任何其他函数之前必需先调用此函数 *************************************************************************/ void InitMAX7219(void) { DATA_high; LOAD_high; CLK_high; Write_1(REG_SCAN_LIMIT, 7); // 扫描8位数字 Write_1(REG_DECODE, 0); // 设置不译码方式 Write_1(REG_SHUTDOWN,1); // 结束停机方式,正常工作 Write_1(REG_DISPLAY_TEST, 0); // 结束显示测试,正常工作 Write_1(REG_INTENSITY, 0); // 设置亮度 Write_2(REG_SCAN_LIMIT, 7); // 扫描8位数字 Write_2(REG_DECODE, 0); // 设置不译码方式 Write_2(REG_SHUTDOWN,1); // 结束停机方式,正常工作 Write_2(REG_DISPLAY_TEST, 0); // 结束显示测试,正常工作 Write_2(REG_INTENSITY, 0); // 设置亮度 Clear(); // 清零全部显示器 } /************************************************************************* * 函数原型:void Clear() * 功 能:关闭全部显示器 **************************************************************************/ void Clear (void) { unsigned char i; for (i=1;i<=8;i++) Write_1(i, 0); // 关闭全部显示器 Write_2(i, 0); } /************************************************************************* * 函数原型:static void Write (unsigned char reg_add, unsigned char dat) * 功 能:向指定的MAX7219内部寄存器写入数据 * 参 数: reg_add = MAX7219内部寄存器地址 dat = 待写入的数据 **************************************************************************/ void Write_1 (unsigned char reg_add, unsigned char dat) { LOAD_low; // 使LOAD变低,启动串行数据发送 SendByte(reg_add); // 发送MAX7219内部寄存器地址 if(reg_add>8) SendByte(dat); // 发送待写入的数据 else SendByte((dat<<7)|(dat>>1)); //如发送的是显示数据,则根据硬件转换一下数据位 LOAD_high; // 使LOAD变高,串行数据结束 } /************************************************************************* * 函数原型:static void Write (unsigned char reg_add, unsigned char dat) * 功 能:向指定的MAX7219内部寄存器写入数据 * 参 数: reg_add = MAX7219内部寄存器地址 dat = 待写入的数据 **************************************************************************/ void Write_2 (unsigned char reg_add, unsigned char dat) { LOAD_low; // 使LOAD变低,启动串行数据发送 SendByte(reg_add); // 发送MAX7219内部寄存器地址//写入地址,即数码管编号 if(reg_add>8) SendByte(dat); // 发送待写入的数据 //写入数据,即数码管显示数字 else SendByte((dat<<7)|(dat>>1)); //如发送的是显示数据,则根据硬件转换一下数据位 CLK_high; SendByte(0x00); // 第一片空 SendByte(0x00); // 第一片空 LOAD_high; // 使LOAD变高,串行数据结束 } /************************************************************************* * 函数原型:static void SendByte (unsigned char dat) * 功 能:向MAX7219发送一个字节串行数据 * 参 数:dat = 待发送的数据 **************************************************************************/ void SendByte (unsigned char dat) { unsigned char i; for (i=0; i<8; i++) { CLK_low; // 使CLK变低 if(dat&0x80) // 发送一位数据'1'或'0' DATA_high; else DATA_low; dat<<=1; CLK_high; // 使CLK变高 } } #endif /************************************************************************/ unsigned char code logo_1[][8]= { 0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x3E,//0 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,//1 0x00,0x00,0x46,0x49,0x49,0x49,0x49,0x31,//2 0x00,0x00,0x41,0x49,0x49,0x49,0x49,0x36,//3 0x00,0x00,0x70,0x08,0x08,0x08,0x08,0x7F,//4 0x00,0x00,0x31,0x49,0x49,0x49,0x49,0x46,//5 0x00,0x00,0x3E,0x49,0x49,0x49,0x49,0x46,//6 0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x3F,//7 0x00,0x00,0x36,0x49,0x49,0x49,0x49,0x36,//8 0x00,0x00,0x31,0x49,0x49,0x49,0x49,0x3E,//9 0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x3E,//0 0x22,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x41,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; unsigned char code logo_2[][8]= { 0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x3E,//0 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,//1 0x00,0x00,0x46,0x49,0x49,0x49,0x49,0x31,//2 0x00,0x00,0x41,0x49,0x49,0x49,0x49,0x36,//3 0x00,0x00,0x70,0x08,0x08,0x08,0x08,0x7F,//4 0x00,0x00,0x31,0x49,0x49,0x49,0x49,0x46,//5 0x00,0x00,0x3E,0x41,0x41,0x41,0x41,0x3E,//0 }; unsigned char code logo_3[][8]= { 0x00,0x1E,0x21,0x21,0x21,0x21,0x21,0x1E,//0 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//1 0x00,0x3E,0x01,0x01,0x1E,0x20,0x20,0x1F,//2 0x00,0x3E,0x01,0x01,0x1E,0x01,0x01,0x3E,//3 0x00,0x21,0x21,0x21,0x1F,0x01,0x01,0x01,//4 0x00,0x1F,0x20,0x20,0x1E,0x01,0x01,0x3E,//5 0x00,0x1F,0x20,0x20,0x3E,0x21,0x21,0x1E,//6 0x00,0x3E,0x01,0x01,0x01,0x01,0x01,0x01,//7 0x00,0x1E,0x21,0x21,0x1E,0x21,0x21,0x1E,//8 0x00,0x1E,0x21,0x21,0x1F,0x01,0x01,0x3E,//9 0x00,0x1E,0x21,0x21,0x21,0x21,0x21,0x1E,//0 0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00, 0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x80, }; unsigned char code logo_4[][8]= { 0x00,0x1E,0x21,0x21,0x21,0x21,0x21,0x1E,//0 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//1 0x00,0x3E,0x01,0x01,0x1E,0x20,0x20,0x1F,//2 0x00,0x3E,0x01,0x01,0x1E,0x01,0x01,0x3E,//3 0x00,0x21,0x21,0x21,0x1F,0x01,0x01,0x01,//4 0x00,0x1F,0x20,0x20,0x1E,0x01,0x01,0x3E,//5 0x00,0x1E,0x21,0x21,0x21,0x21,0x21,0x1E,//0 }; 图片:链接:https://pan.baidu.com/s/1ae95GQXkaVLrqg8nV1oE8w
提取码:5q6j