7219芯片驱动LED的电子时钟
扫描二维码
随时随地手机看文章
/***************时钟采用定时中断方式,50MS一次******完整可用,硬件实验通过**********************/
#include
#include
#include
/***********************************************************************/
#defineucharunsignedchar
#defineuintunsignedint
//commonpart
#defineHIGH1
#defineLOW0
#defineTRUE1
#defineFALSE0
#defineZERO0
#defineMSB0x80
#defineLSB0x01
//max7219part
#defineREG_NO_OP0x00//定义空操作
#defineDIG_10x01//定义数码管1
#defineDIG_20x02//定义数码管2
#defineDIG_30x03//定义数码管3
#defineDIG_40x04//定义数码管4
#defineDIG_50x05//定义数码管5
#defineDIG_60x06//定义数码管6
#defineDIG_70x07//定义数码管7
#defineDIG_80x08//定义数码管8
#defineDECODE_MODE0x09
#defineINTENSITY0x0A
#defineSCAN_LIMIT0x0B
#defineSHUT_DOWN0x0C
#defineDISPLAY_TEST0x0F
uchardeda,sec,min,hour,week=1;
/***********************************************************************/
//changethispartatdifferentboard
sbitLOAD=P2^7;//MAX7219Load-DataInput:risingedgepin12
sbitDIN=P2^5;//MAX7219Serial-DataInput:risingedgepin1
sbitCLK=P2^6;//MAX7219
sbitTT=P1^4;
sbitQQ=P3^6;
sbitRR=P3^7;
//functiondefine
/***********************************************************************/
voidWrite_Max7219_byte(unsignedchartemp);//writemax7219abyte
voidWrite_Max7219(unsignedcharaddress,unsignedchardat);//writemax7219commandanddata
voidInit_Max7219(void);//Initizemax7219
//testprogramdisplayfrom1~8
/***********************************************************************/
/***********************************************************************/
voidWrite_Max7219_byte(unsignedchartemp)
{
unsignedchari;
for(i=0;i<8;i++)
{
CLK=LOW;
DIN=(bit)(temp&MSB);
temp<<=1;
CLK=HIGH;
}
}
/***********************************************************************/
voidWrite_Max7219(unsignedcharaddress,unsignedchardat)
{
LOAD=LOW;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
LOAD=HIGH;
}
/***********************************************************************/
voidInit_Max7219(void)
{
Write_Max7219(SHUT_DOWN,0x01);//NormalOperationXXXXXXX1ShutdownModeXXXXXXXX0
Write_Max7219(DISPLAY_TEST,0x00);//NormalOperationXXXXXXX0DisplayTestMode
Write_Max7219(DECODE_MODE,0xff);//DecodeModeSelectD7~D01Bdecode0Nodecode
Write_Max7219(SCAN_LIMIT,0x07);//SCANLIMIT0~70xX0~0xX7
Write_Max7219(INTENSITY,0x04);
Write_Max7219(DIG_4,0x02);Write_Max7219(DIG_3,0x01);
Write_Max7219(DIG_6,0x07);Write_Max7219(DIG_5,0x05);
Write_Max7219(DIG_7,0x01);
}
/*****************************************************/
voidinit(void)
{
TMOD=0x01;
TH0=-(50000/256);
TL0=-(50000%256);
ET0=1;
TR0=1;
EA=1;
TT=0;
QQ=0;
RR=0;
Init_Max7219();
}
/******************************************/
voidtime0(void)interrupt1
{
TH0=-(50000/256);
TL0=-(50000%256);
deda++;
}
/***************************************/
voidconv(void)
{
if(deda>=20){deda=0;sec++;Write_Max7219(DIG_8,sec%10);Write_Max7219(DIG_7,sec/10);}
if(sec>=60){sec=0;min++;Write_Max7219(DIG_6,min%10);Write_Max7219(DIG_5,min/10);}
if(min>=60){min=0;hour++;Write_Max7219(DIG_4,hour%10);Write_Max7219(DIG_3,hour/10);}
if(hour>=24){hour=0;week++;Write_Max7219(DIG_2,week%10);}
if(week>7){week=1;}
if(hour==3)TT=1;
}
/*****************************************/
voiddelay(uintk)
{
uintdatai,j;
for(i=0;i
{;}}
}
/**********************************************/
ucharscan_key(void)
{
uchartemp;
P3=0x3f;
temp=P3;
if(temp!=0x3f)
{delay(20);
temp=P3;
if(temp!=0x3f)returntemp;
}
return0x3f;
}
/********************************************/
voidmain(void)
{ucharkey_flag;
init();
while(1)
{
conv();
key_flag=scan_key();
switch(key_flag)
{
case0x3b:if(++min>59)min=0;delay(300);Write_Max7219(DIG_6,min%10);Write_Max7219(DIG_5,min/10);break;
case0x37:if(++hour>23)hour=0;delay(300);Write_Max7219(DIG_4,hour%10);Write_Max7219(DIG_3,hour/10);break;
case0x2f:if(++week>7)week=1;delay(300);Write_Max7219(DIG_2,week%10);break;
default:break;
}
}
}