|
本帖最后由 tianxing 于 2014-11-3 18:10 编辑
MAX7219是一款最适合初学者练手的驱动电路,单片电路可以显示8位数码管和8*8LED点阵,电路设计简单明了且可以多级连接,初学者动手容易成功,熟练掌握MAX7219,就基本通达了数码管和点阵的学习内容。希望能给大家以帮助。
#include <reg52.h>
#define INT8 unsigned char
#define Addr_Digit0 0x01
#define Addr_Digit1 0x02
#define Addr_Digit2 0x03
#define Addr_Digit3 0x04
#define Addr_Digit4 0x05
#define Addr_Digit5 0x06
#define Addr_Digit6 0x07
#define Addr_Digit7 0x08
#define Addr_Decode_Mode 0x09 //译码模式寄存器地址
#define Addr_Intensity 0x0A //亮度控制寄存器地址
#define Addr_Scan_Limit 0x0B //扫描控制寄存器地址
#define Addr_Shutdowm 0x0C //掉电模式寄存器地址
#define Addr_Display_Test 0x0F //显示检测寄存器地址
sbit Max7219_Din = P3 ^ 0;
sbit Max7219_Load = P3 ^1;
sbit Max7219_Clk = P3 ^ 2;
INT8 T_count = 0;
INT8 Hour_1 = 0, Hour_2 = 8; //任意设定初始小时值。 8
INT8 Minute_1 = 3, Minute_2 = 0; //任意设定初始分钟值。 30
INT8 Second_1 = 0, Second_2 = 0; //任意设定初始小时值。 00
INT8 code max7219_led_code[11]= //共阴码表。
{
0x7E,0x30,0x6D,0x79,0x33, //0,1,2,3,4
0x5B,0x5F,0x70,0x7F,0x7B,0x01 //5,6,7,8,9,-
};
void wr_max7219(INT8 addr,INT8 dat)
{
INT8 i;
Max7219_Load = 0;
Max7219_Clk = 0;
for( i = 0; i < 8; i ++ )
{
Max7219_Din = ( bit )( addr & 0x80 );
addr = addr << 1;
Max7219_Clk = 1;
Max7219_Clk = 0;
}
for( i = 0; i < 8; i ++ )
{
Max7219_Din = ( bit )( dat & 0x80 );
dat = dat << 1;
Max7219_Clk = 1;
Max7219_Clk = 0;
}
Max7219_Load = 1;
Max7219_Load = 0;
}
void main()
{
wr_max7219( Addr_Shutdowm, 0x01 );
wr_max7219( Addr_Scan_Limit, 0x07 );
wr_max7219( Addr_Intensity, 0x0F );
wr_max7219( Addr_Decode_Mode, 0x00 );
TMOD |= 0x10;
TMOD &= 0xDF;
TR1 = 1;
ET1 = 1;
EA = 1;
while(1)
{
wr_max7219( Addr_Digit0, max7219_led_code[ Hour_1 ]);
wr_max7219( Addr_Digit1, max7219_led_code[ Hour_2 ]);
wr_max7219( Addr_Digit2, max7219_led_code[ 10 ]);
wr_max7219( Addr_Digit3, max7219_led_code[ Minute_1 ]);
wr_max7219( Addr_Digit4, max7219_led_code[ Minute_2 ]);
wr_max7219( Addr_Digit5, max7219_led_code[ 10 ]);
wr_max7219( Addr_Digit6, max7219_led_code[ Second_1 ]);
wr_max7219( Addr_Digit7, max7219_led_code[ Second_2 ]);
}
}
void interrupt_timer1() interrupt 3
{
TH1 = ( 65536 - 5000 ) / 256;
TL1 = ( 65536 - 5000 ) % 256;
T_count ++; //定时器中断计数器加一
if( T_count == 161 ) //微调
{
T_count = 0; //清零定时器中断计数器
Second_2 ++; //秒个位计数器加一
if( Second_2 == 10 )
{
Second_1 ++;
Second_2 = 0;
if( Second_2 == 0 && Second_1 == 6 )
{
Second_1 = 0;
Second_2 = 0;
Minute_2 ++;
if( Minute_2 == 10 )
{
Minute_1 ++;
Minute_2 = 0; //分个位计数器清零
if(Minute_2 == 0 && Minute_1 == 6 )
{
Minute_1 = 0;
Minute_2 = 0;
Hour_2 ++;
if( Hour_2 == 10 && Hour_1 <= 2)
{
Hour_1 ++;
Hour_2 =0; //时个位计数器清零
}
else if( Hour_2 == 4 && Hour_1 == 2 )
{
Hour_1 = 0;
Hour_2 = 0;
}
}
}
}
}
}
}
|
评分
-
查看全部评分
|