#include <reg52.h>
#include "intrins.h"
#include "Max7219.h"
uchar code laji[][8]={
{0x18,0x24,0x42,0x42,0x7E,0x42,0x42,0x42},//A
{0x7C,0x42,0x42,0x7C,0x42,0x42,0x42,0x7C},//B
{0x3C,0x42,0x40,0x40,0x40,0x40,0x42,0x7C},//C
{0x7C,0x42,0x42,0x42,0x42,0x42,0x42,0x7C},//D
};
/*n毫秒的延时程序*/
/*void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}*/
void main()
{
uchar i,j;
//Delay_xms(50);
MAX7219Init();
while(1)
{
for(j=0;j<8;j++)
{
for(i=0;i<number;i++)
{
Max7219WR(j+1,laji[i][j]);
}
CS=1; _nop_(); CS=0;
//Delay_xms(100);
}
}
}
Max7219.h
#ifndef _Max7219_H_
#define _Max7219_H_
#define uchar unsigned char
#define uint unsigned int
#define number 4 //点阵级联个数
#define lum 5 //亮度级别,0-F,共16级
//定义Max7219端口
sbit CLK = P2^2; //时钟 Max7219_pin
sbit CS = P2^1; //锁存
sbit DIN = P2^0; //数据
//向MAX7219 写入地址和数据
void Max7219WR(uchar addr,uchar dat)
{
uchar n=8;
CLK=0;
CS=0; //CS=0有效,CS=1锁存
/*发送数据 高位先发*/
while(n--)
{
DIN = addr & 0x80;
CLK=1;
addr <<= 1;
CLK=0;
}
n=8;
/*发送数据 高位先发*/
while(n--)
{
DIN = dat & 0x80;
CLK=1;
dat <<= 1;
CLK=0;
}
}
void MAX7219Init()
{
uchar i,j;
for(i=0;i<5;i++) //
{
for(j=number;j>0;j--)
switch(i)
{
case 0:Max7219WR(0x09, 0x00); break; //译码方式:BCD码 不译码
case 1:Max7219WR(0x0A, lum); break; //亮度 0-15 共16级
case 2:Max7219WR(0x0B, 0x07); break; //扫描界限;8位显示
case 3:Max7219WR(0x0C, 0x01); break; //掉电模式:0,普通模式:1
case 4:Max7219WR(0x0F, 0x00); break; //显示测试:1;测试结束,正常显示:0
}
CS=1; _nop_(); CS=0; //每写入四次给一个锁存脉冲
}
}
#endif