MAX7219芯片在这篇blog里
1.目的
使用MAX7219计时0~99s
2.原理图
3.软件
#include<reg51.h>
#include<stdio.h>
sbit DIN=P3^0;
sbit LOAD=P3^1;
sbit CLK=P3^2;
sbit key0=P1^0;
sbit key1=P1^1;
//寄存器宏定义
#define DECODE_MODE 0x09 //译码控制寄存器
#define INTENSITY 0x0A //亮度控制寄存器
#define SCAN_LIMIT 0x0B //扫描界限寄存器
#define SHUT_DOWN 0x0C //掉电模式寄存器
#define DISPLAY_TEST 0x0F //显示控制寄存器
unsigned char minute=0;
unsigned char counter=0;
unsigned char num_add[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
unsigned char num_dat[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89};
void max7219_send(unsigned char address,unsigned char dat)
{
unsigned char i;
LOAD=0; //拉低片选线,选中器件
/*发送地址*/
for(i=0;i<8;i++)
{
CLK=0; //清零时钟总线
DIN=(bit)(address&0x80); //每次取高字节
address<<=1; //左移一位
CLK=1; //时钟上升沿,发送地址
}
/*发送数据*/
for(i=0;i<8;i++)
{
CLK=0;
DIN=(bit)(dat&0x80);
dat<<=1;
CLK=1;
}
LOAD=1;
}
void max7219_init()
{
max7219_send(SHUT_DOWN,0x01); //开启正常工作模式(0xX1)
max7219_send(DISPLAY_TEST,0x00); //选择正常工作模式(0xX0)
max7219_send(DECODE_MODE,0xff); //选用全译码模式
max7219_send(SCAN_LIMIT,0x07); //8只LED全用
max7219_send(INTENSITY,0x04); //设置初始亮度9/32
}
void display(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x%10;
max7219_send(num_add[1],num_dat[j]);
max7219_send(num_add[0],num_dat[i]);
}
void timer_init()
{
EA=1;
ET0=1;
TMOD=0x01;
TH0=0xd8;//设定10ms中断一次
TL0=0xef;
}
void main()
{
while(1)
{
if(!(key0==0))
{
TR0=0;
display(minute);//没有按下开始键时等待
}
if((key0==0))
{
max7219_init();
timer_init();
TR0=1;
while(!(key1==0));
if((key0==1)&&(key1==0))
{
TR0=0;
minute=0;
display(minute);
}
}
}
}
void timer_10ms() interrupt 1
{
if(counter<100)
{
counter++;
}
else
{
if(minute<99)
{
minute++;
counter=0;
}
else
{
minute=0;
counter=0;
}
}
TH0=0xd8;//设定10ms中断一次
TL0=0xef;
display(minute);
}