#include <reg52.h>
/*****************************
宏定义声明
*****************************/
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
//max7219 part
#define DECODE_MODE 0x09
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0F
/*************************************************************************
管脚定义:
不同的电路板上,改变这里的定义,
*************************************************************************/
sbit LOAD = P0^1; //MAX7219 Load-Data Input: rising edge pin 12
sbit DIN = P0^0; //MAX7219 Serial-Data Input: rising edge pin 1
sbit CLK = P0^2; //MAX7219 Serial-Clock Input: maximum 10MHz pin 13
/***********************************************************
函数声明
***********************************************************/
void Write_Max7219_byte(unsigned char temp);
void Write_Max7219(unsigned char address,unsigned char dat);
void Init_Max7219(void);
/***************
测试程序,
显示1~3
***************/
void main(void)
{
unsigned char i;
Init_Max7219();
while(TRUE)
{
for(i=1; i <4; i++)
{
Write_Max7219(i,i);
}
}
}
/******************************************
发送一个字节的子程序:
上升沿发送数据,
MSB first
******************************************/
void Write_Max7219_byte(unsigned char temp)
{
unsigned char i;
for (i=0; i <8; i++)
{
CLK = LOW;
DIN = (bit)(temp&MSB);
temp < <=1;
CLK = HIGH;
}
}
/**********************************************************
向寄存器中写入一个数据
先写地址,后写数据
load 上升沿锁存数据
**********************************************************/
void Write_Max7219(unsigned char address,unsigned char dat)
{
LOAD = LOW;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
LOAD = HIGH;
}
/**********************
初始化max7219 子函数
设置工作寄存器
需要查看芯片手册
**********************/
void Init_Max7219(void)
{
Write_Max7219(SHUT_DOWN, 0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
Write_Max7219(DISPLAY_TEST, 0x00); //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1
Write_Max7219(DECODE_MODE, 0xff); //Decode Mode Select D7~D0 1 B decode 0 No decode
Write_Max7219(SCAN_LIMIT, 0x02); //SCAN LIMIT 0~7 0xX0~0xX7
Write_Max7219(INTENSITY, 0x04); //Set Intensity 0xX0~0xXf
}