#include <pic.h>
__CONFIG(HS&WDTDIS&LVPDIS);
#define INT8U unsigned char
#define _XTAL_FREQ 4000000UL
//定义Max7219端口
#define DIN RB0
#define CS RB1
#define CLK RB2
const INT8U disp1[10][8]={
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
{0x18,0x28,0x08,0x08,0x08,0x08,0x08,0x08},//1
{0x3c,0x42,0x42,0x02,0x1c,0x20,0x40,0x7E},//2
{0x3c,0x42,0x42,0x0c,0x2,0x42,0x42,0x3c},//3
{0x28,0x28,0x48,0x48,0x7e,0x8,0x8,0x8},//4
{0x3e,0x40,0x40,0x7c,0x2,0x2,0x42,0x3c},//5
{0x3e,0x42,0x40,0x7C,0x42,0x42,0x42,0x3c},//6
{0x7E,0x02,0x4,0x8,0x8,0x8,0x8,0x8},//7
{0x3c,0x42,0x42,0x3c,0x42,0x42,0x42,0x3c},//8
{0x3c,0x42,0x42,0x42,0x3e,0x2,0x42,0x3c}//9
};
//-----------------------------------------------------------------------------
//向MAX7219写入字节
//-----------------------------------------------------------------------------
void Write_Max7219_byte(INT8U DATA)
{
INT8U i;
CS=0; //CS=0有效,CS=1锁存
for(i=8;i>=1;i--)
{
CLK=0;
if(DATA & 0x80) DIN=1; else DIN=0;
//DIN=(DATA&0x80); //&10000000, 编译器对位操作的理解:非0即为1
DATA=DATA<<1;
CLK=1; //上升沿把数据送出去
}
}
//------------------------------------------------------------------
//向M第一片MAX7219写入数据
//------------------------------------------------------------------
void Write_Max7219_1(INT8U add1,INT8U dat1)
{
CS=0;
Write_Max7219_byte(add1); //写入地址,即数码管编号
Write_Max7219_byte(dat1); //写入数据,即数码管显示数字
CS=1;
}
//---------------------------------------------------------------------
//第二片MAX7219的写入数据
//---------------------------------------------------------------------
void Write_Max7219_2(INT8U add2,INT8U dat2)
{
CS=0;
Write_Max7219_byte(add2);
Write_Max7219_byte(dat2);
CLK=1;
Write_Max7219_byte(0x00); //片1写入空
Write_Max7219_byte(0x00);
CS=1;
}
//---------------------------------------------------------------------
//第三片MAX7219的写入数据
//---------------------------------------------------------------------
void Write_Max7219_3(INT8U add3,INT8U dat3)
{
CS=0;
Write_Max7219_byte(add3);
Write_Max7219_byte(dat3);
CLK=1;
Write_Max7219_byte(0x00); //片1写入空
Write_Max7219_byte(0x00);
Write_Max7219_byte(0x00); //片2写入空
Write_Max7219_byte(0x00);
CS=1;
}
//---------------------------------------------------------------------
//第四片MAX7219的写入数据
//---------------------------------------------------------------------
void Write_Max7219_4(INT8U add4,INT8U dat4)
{
CS=0;
Write_Max7219_byte(add4);
Write_Max7219_byte(dat4);
CLK=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);
CS=1;
}
//---------------------------------------------------------------------
//初始化芯片
//---------------------------------------------------------------------
void Init_MAX7219(void)
{
Write_Max7219_1(0x09, 0x00); //译码方式:不译码
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); //译码方式:不译码
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); //译码方式:不译码
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); //译码方式:不译码
Write_Max7219_4(0x0a, 0x00); //亮度最小
Write_Max7219_4(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_4(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_4(0x0f, 0x00); //显示测试:1;测试结束,正常显示:
}
//---------------------------------------------------------------------
//*********************主程序**************************
//---------------------------------------------------------------------
void main(void)
{
INT8U i,j=0,k=0;;
TRISB=0;
PORTB=0x00;
__delay_ms(50);
Init_MAX7219();
while(1)
{
for(i=1;i<9;i++)
{
Write_Max7219_1(i,disp1[j%10][i-1]);
Write_Max7219_2(i,disp1[j/10%10][i-1]);
Write_Max7219_3(i,disp1[j/100%10][i-1]);
Write_Max7219_4(i,disp1[k][i-1]);
}
j+=1;
if(j>254)
{
j=0;k+=1;
if(k>9) k=0;
}
__delay_ms(300);
}
}