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);
max7219_send(DISPLAY_TEST,0x00);
max7219_send(DECODE_MODE,0xff);
max7219_send(SCAN_LIMIT,0x07);
max7219_send(INTENSITY,0x04);
}
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;
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;
TL0=0xef;
display(minute);
}
|