本帖最后由 zsq198926 于 2020-3-13 16:14 编辑
在网上看到有人用MAX7219显示屏结合arduino做的显示时间的屏幕,网上查了一下用51做MAX7219显示的好像很少,都是用1602显示的比较多,就自己动手用51单片及做了一个类似的。自己网上查了显示的驱动程序,参考了郭天祥的书,组合修改了部分代码,买了块DS12C887(这块时钟模块比较精准,有掉电走时功能,避免反复设置时间,一年时间误差1分钟左右)。主要功能显示时间(前两块显示小时,三四块显示分钟),显示星期(前三块显示英文缩写,第四块显示数字周几),显示月份(第一块月份,如果超过9,第一块上显示两个数字,第二块汉子“月”,第三四块显示日期)。另外在显示星期时调整了一下大小写,用取模软件美化了一下数字的显示(网上找的大同小异,8*8的美化效果毕竟有限)
月份显示
星期显示
时间显示
单片机代码如下:比较杂乱,高手请多多指教
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //sfr WDT_CONTR=0xe1;
- //定义Max7219端口
- sbit Max7219_pinCLK = P3^7;
- sbit Max7219_pinCS = P3^6;
- sbit Max7219_pinDIN = P3^5;
- //DS12C887 端口定义
- sbit cs=P2^4; //片选信号,低电平有效
- sbit as=P2^3; //地址选通输入端
- sbit rw=P2^2; //读写输入端
- sbit ds=P2^1; //读输入脚
- //sbit IRQ=P3^3; //外部中断
- uchar code disp1[41][8]={
- {0x1C,0x22,0x63,0x63,0x63,0x63,0x22,0x1C},//0
- {0xC,0x1C,0xC,0xC,0xC,0xC,0xC,0x1E},//1
- {0x1C,0x22,0x22,0x4,0x8,0x10,0x3E,0x3E},//2
- {0x1C,0x22,0x2,0x1C,0x2,0x2,0x22,0x1C},//3
- {0x6,0xE,0x16,0x26,0x46,0x7F,0x6,0xF},//4
- {0x3E,0x40,0x40,0x7E,0x2,0x2,0x62,0x3C},//5
- {0xC,0x10,0x20,0x3C,0x42,0x42,0x42,0x3C},//6
- {0x7E,0x7E,0x2,0x4,0x8,0x10,0x30,0x30},//7
- {0x1C,0x22,0x22,0x1C,0x22,0x22,0x22,0x1C},//8
- {0x3C,0x42,0x42,0x42,0x3E,0x6,0xC,0x38},//9
- {0x0,0x0,0x38,0x44,0x44,0x44,0x44,0x3A},//a 11
- {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
- {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
- {0x0,0x8,0x8,0x38,0x48,0x48,0x78,0x38},//D
- {0x0,0x0,0x0,0x38,0x44,0x7C,0x40,0x38},//E 15
- {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F 16
- {0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
- {0x0,0x20,0x20,0x38,0x24,0x24,0x24,0x24},//H 18
- {0x0,0x10,0x0,0x10,0x10,0x10,0x10,0x10},//I 19
- {0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J 20
- {0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
- {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
- {0x0,0x6C,0x6C,0x6C,0x7C,0x74,0x54,0x54},//M 24
- {0x0,0x0,0x40,0x7C,0x44,0x44,0x44,0x44},//N 25
- {0x0,0x0,0x0,0x38,0x44,0x44,0x44,0x38},//O 26
- {0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
- {0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
- {0x0,0x0,0x14,0x18,0x10,0x10,0x10,0x10},//R 28
- {0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S 29
- {0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T 31
- {0x0,0x0,0x44,0x44,0x44,0x44,0x44,0x3A},//U 32
- {0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
- {0x0,0x82,0x82,0x82,0x92,0xAA,0xC6,0x82},//W 34
- {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
- {0x1E,0x22,0x3E,0x22,0x3E,0x22,0x22,0x42},//月
- {0x5F,0xD1,0x51,0x51,0x51,0x51,0x51,0x5F},//10
- {0x22,0x66,0x22,0x22,0x22,0x22,0x22,0x22},//11
- {0x5F,0xC1,0x41,0x5F,0x50,0x50,0x50,0x5F},//12
- };
- /*n毫秒的延时程序*/
- void Delay_xms(uint x)
- {
- uint a,b;
- for(a=0;a<x;a++)
- for(b=0;b<112;b++);
- }
- void write_ds(uchar add,uchar date)
- {
- cs=0;
- as=1;
- ds=1;
- rw=1;
- P0=add;
- as=0;
- rw=0;
- P0=date;
- rw=1;
- as=1;
- cs=1;
- }
- // ---------------读887函数-------
- uchar read_ds(uchar rs_add)
- {
- uchar ds_date;
- as=1;
- ds=1;
- rw=1;
- cs=0;
- P0=rs_add; //写地址
- as=0;
- ds=0;
- P0=0xff;
- ds_date=P0; //读数据
- ds=1;
- as=1;
- cs=1;
- return ds_date;
- }
- //-------首次上电对时 --------
- /*
- void set_time()
- {
- write_ds(0,30); //秒
- write_ds(1,0); //闹钟秒
- write_ds(2,5); //分钟
- write_ds(3,0); //闹钟分钟
- write_ds(4,21); //小时
- write_ds(5,0); // 闹钟小时
- write_ds(6,5); // 星期几
- write_ds(7,6); // 日期
- write_ds(8,3); // 月份
- write_ds(9,20); // 年
- } */
- void INit_887()
- {
- //------------------887首次使用,----初始化-------------
- write_ds(0x0A,0x20); // 打开振荡器
- write_ds(0x0B,0x06); // 24小时模式,二级制格式,闹铃中断开
- }
- //--------------------------------------------
- //向MAX7219(U3)写入字节
- void Write_Max7219_byte(uchar DATA)
- {
- uchar i;
- Max7219_pinCS=0; //CS=0有效,CS=1锁存
- for(i=8;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;
- //Max7219_pinCLK=1;
- 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;
- //Max7219_pinCLK=1;
- 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;
- }
- /*第四片MAX7219的写入数据*/
- void Write_Max7219_4(uchar add4,uchar dat4)
- {
- Max7219_pinCS=0;
- //Max7219_pinCLK=1;
- Write_Max7219_byte(add4);
- Write_Max7219_byte(dat4);
- Max7219_pinCLK=1;
- Write_Max7219_byte(0x00); //片1写入空
- Write_Max7219_byte(0x00);
- Write_Max7219_byte(0x00); //片2写入空
- Write_Max7219_byte(0x00);
- Write_Max7219_byte(0x00); //片3空
- Write_Max7219_byte(0x00);
- Max7219_pinCS=1;
- }
- /*初始化芯片*/
- void Init_MAX7219()
- {
- Write_Max7219_1(0x09, 0x00); //译码方式:BCD码
- Write_Max7219_1(0x0a, 0x00); //亮度
- 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, 0x00); //亮度
- 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, 0x00); //亮度
- Write_Max7219_3(0x0b, 0x07); //扫描界限;8个数码管显示
- Write_Max7219_3(0x0c, 0x01); //掉电模式:0,普通模式:1
- Write_Max7219_3(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
- Write_Max7219_4(0x09, 0x00); //译码方式:BCD码
- Write_Max7219_4(0x0a, 0x00); //亮度
- Write_Max7219_4(0x0b, 0x07); //扫描界限;8个数码管显示
- Write_Max7219_4(0x0c, 0x01); //掉电模式:0,普通模式:1
- Write_Max7219_4(0x0f, 0x00);
- }
- void weekshow(void)
- {
- uchar i,week;
-
- week=read_ds(6);
- switch(week)
- {
- case 1:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[24][i-1]);
- Write_Max7219_3(i,disp1[25][i-1]);
- Write_Max7219_4(i,disp1[23][i-1]);
- }
- break;
- case 2:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[13][i-1]);
- Write_Max7219_3(i,disp1[30][i-1]);
- Write_Max7219_4(i,disp1[29][i-1]);
- }
- break;
- case 3:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[13][i-1]);
- Write_Max7219_3(i,disp1[14][i-1]);
- Write_Max7219_4(i,disp1[32][i-1]);
- }
- break;
- case 4:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[31][i-1]);
- Write_Max7219_3(i,disp1[17][i-1]);
- Write_Max7219_4(i,disp1[29][i-1]);
- }
- break;
- case 5:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[18][i-1]);
- Write_Max7219_3(i,disp1[27][i-1]);
- Write_Max7219_4(i,disp1[15][i-1]);
- }
- break;
- case 6:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[29][i-1]);
- Write_Max7219_3(i,disp1[10][i-1]);
- Write_Max7219_4(i,disp1[28][i-1]);
- }
- break;
- case 7:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[week][i-1]);
- Write_Max7219_2(i,disp1[24][i-1]);
- Write_Max7219_3(i,disp1[30][i-1]);
- Write_Max7219_4(i,disp1[28][i-1]);
- }
- break;
- }
- }
- void timeshow (void)
- {
- uchar i,shi,shiwei,shige,fen,fenshi,fenge ;
-
- shi=read_ds(4);
- fen=read_ds(2);
- Delay_xms(5);
- shiwei=shi/10;
- shige=shi%10;
- fenshi=fen/10;
- fenge=fen%10;
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[fenge][i-1]);
- Write_Max7219_2(i,disp1[fenshi][i-1]);
- Write_Max7219_3(i,disp1[shige][i-1]);
- Write_Max7219_4(i,disp1[shiwei][i-1]);
- }
- }
- void dayshow()
- {
- uchar i,yue,day,dayshi,dayge;
- yue=read_ds(8);
- day=read_ds(7);
- Delay_xms(5);
- dayshi=day/10;
- dayge=day%10;
- if(yue<10)
- {
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[dayge][i-1]);
- Write_Max7219_2(i,disp1[dayshi][i-1]);
- Write_Max7219_3(i,disp1[36][i-1]);
- Write_Max7219_4(i,disp1[yue][i-1]);
- }
- }
- switch (yue)
- {
- case 10:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[dayge][i-1]);
- Write_Max7219_2(i,disp1[dayshi][i-1]);
- Write_Max7219_3(i,disp1[36][i-1]);
- Write_Max7219_4(i,disp1[38][i-1]);
- }
- break;
- case 11:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[dayge][i-1]);
- Write_Max7219_2(i,disp1[dayshi][i-1]);
- Write_Max7219_3(i,disp1[36][i-1]);
- Write_Max7219_4(i,disp1[39][i-1]);
- }
- break;
- case 12:
- for(i=1;i<9;i++)
- {
- Write_Max7219_1(i,disp1[dayge][i-1]);
- Write_Max7219_2(i,disp1[dayshi][i-1]);
- Write_Max7219_3(i,disp1[36][i-1]);
- Write_Max7219_4(i,disp1[40][i-1]);
- }
- break;
- }
- }
- /**********************主程序**************************/
- void main()
- {
- // uchar i;//j;
- Init_MAX7219();
- INit_887();
- Delay_xms(5);
- // set_time(); 44
- while(1)
- {
- // WDT_CONTR=0x35;
- weekshow();
- // WDT_CONTR=0x35;
- Delay_xms(5000);
- timeshow();
- Delay_xms(5000);
- // dayshow();
- // Delay_xms(5000);
- }
- }
复制代码
|