/***************max7219.h****************/
#ifndef _max7219_h_
#define _max7219_h_
#include
#include "link.h"
/***************控制引脚定义****************/
sbit _7219_DIN = P2^0;
sbit _7219_CS = P2^1;
sbit _7219_CLK = P2^2;
/***************控制字定义****************/
#define _7219_NO_OP 0x00 //Digit0~Digit7位于RAM的地址
#define _7219_DIG0 0x01
#define _7219_DIG1 0x02
#define _7219_DIG2 0x03
#define _7219_DIG3 0x04
#define _7219_DIG4 0x05
#define _7219_DIG5 0x06
#define _7219_DIG6 0x07
#define _7219_DIG7 0x08
#define _7219_DECODE 0x09 //译码方式地址
#define _7219_INTENSITY 0x0A //亮度寄存器地址
#define _7219_SCANLIMIT 0x0B //扫描界限寄存器地址,用于设置外接数码管个数
#define _7219_SHUTDOWN 0x0C //停机寄存器地址,使其处于低功耗
#define _7219_DISPTEST 0x0F //显示测试寄存器地址,用于测试数码管和MAX2719是否工作
//Decode Mode
#define _7219_DECODE_ALL 0xff
#define _7219_DECODE_NONE 0x00
// Intensity
#define _7219_INTENSITY_MIN 0x01
#define _7219_INTENSITY_MAX 0x0f
// Scanlimit
#define _7219_SCANLIMIT_7 0x07
#define _7219_SCANLIMIT_6 0x06
#define _7219_SCANLIMIT_5 0x05
#define _7219_SCANLIMIT_4 0x04
#define _7219_SCANLIMIT_3 0x03
#define _7219_SCANLIMIT_2 0x02
#define _7219_SCANLIMIT_1 0x01
#define _7219_SCANLIMIT_0 0x00
//Shutdown mode
#define _7219_SHUTDOWN_ON 0x01
#define _7219_SHUTDOWN_OFF 0x00
//Display test
#define _7219_DISPTEST_ON 0x01
#define _7219_DISPTEST_OFF 0x00
//7219 CODE B
//BCD译码对应的字符段位
#define _7219_0 0x00
#define _7219_1 0x01
#define _7219_2 0x02
#define _7219_3 0x03
#define _7219_4 0x04
#define _7219_5 0x05
#define _7219_6 0x06
#define _7219_7 0x07
#define _7219_8 0x08
#define _7219_9 0x09
#define _7219_E 0x0B
#define _7219_H 0x0C
#define _7219_L 0x0D
#define _7219_P 0x0E
#define _7219_BLANK 0x0F
//7219 NO Code
#define _7219_SEG_DP 7
#define _7219_SEG_A 6
#define _7219_SEG_B 5
#define _7219_SEG_C 4
#define _7219_SEG_D 3
#define _7219_SEG_E 2
#define _7219_SEG_F 1
#define _7219_SEG_G 0
/***************函数声明****************/
extern void Write_Max7219(uchar Addr, uchar Content);
extern void Init_Max7219();
#endif
/**************************max7219.c*******************************/
#include "max7219.h"
#include "link.h"
#include
/****************************************************************
*函数功能:写入一个字节到max7219
*入口参数:数据Content
*出口参数:无
*****************************************************************/
void Write_Max7219_byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
_7219_CLK=0;
_7219_DIN=(bit)(temp&0x80);
temp<<=1;
_7219_CLK=1;
}
}
/****************************************************************
*函数功能:在max7219指定的地址写入一个字节的内容
*入口参数:要写入数据的地址:Address
写入数据的具体值:Content
*出口参数:无
****************************************************************/
void Write_Max7219(unsigned char address,unsigned char dat)
{
_7219_CS=0;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
_7219_CS=1;
}
/****************************************************************
*函数功能:配置max7219的基本参数
*入口参数:无
*出口参数:无
****************************************************************/
void Init_Max7219()
{
Write_Max7219(_7219_SHUTDOWN , _7219_SHUTDOWN_OFF);
Write_Max7219(_7219_INTENSITY, 0x05);
Write_Max7219(_7219_DECODE , _7219_DECODE_ALL);
Write_Max7219(_7219_DISPTEST , _7219_DISPTEST_OFF);
Write_Max7219(_7219_SHUTDOWN , _7219_SHUTDOWN_ON);
Write_Max7219(_7219_SCANLIMIT , _7219_SCANLIMIT_7);
}