SMG精准时钟(24小时制),三键实现时间调整,并带位闪特效:
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define DM P0
#define WM P2
uchar code SMG_DM[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code SMG_WM[]={0x00,0x20,0x40,0x60,0x80,0xA0};
sbit K1=P3^2;
sbit K2=P1^1;
sbit K3=P3^4;
sbit BEEP=P3^5;
uchar cnt=0;
bit sec_flg=0; // 0:1秒未到。
char sec,min,hour; // 时,分,秒。
uchar dis[6];
uchar status=0; // 初态:禁止调时。
bit ss_flag = 0;
//-------------------------------------------------
void delay(uint t)
{
while(t--);
}
//==================================================
void T0_init()
{
TMOD=0x01;
TH0=0x3C;
TL0=0xB0;
EA=1;
ET0=1;ET1=1;
TR0=1;TR1=1;
PT1=1;
}
//==============================================================================================
void main()
{
T0_init();
RD=0;
// D1=0,D2=1;
while(1)
{
if((sec_flg)&&(status==0))
{
sec_flg=0;
sec++;
if(sec>59) sec=0,min++;
if(min>59) min=0,hour++;
if(hour>23) hour=0;
}
dis[0]=sec%10;
dis[1]=sec/10;
dis[2]=min%10;
dis[3]=min/10;
dis[4]=hour%10;
dis[5]=hour/10;
if(status!=0)
{
if(!K1)
{
while(!K1);
BEEP=0,delay(1000),BEEP=1;
switch(status)
{
case 1: hour++;
if(hour>23)
hour=0;
break;
case 2: min++;
if(min>59)
min=0;
break;
case 3: sec++;
if(sec>59)
sec=0;
break;
default: break;
}
}
if(!K2)
{
while(!K2);
BEEP=0,delay(1000),BEEP=1;
switch(status)
{
case 1: hour--;
if(hour3000)
{
ct=0;
ss_flag=!ss_flag;
}
if(status!=0)
{
switch(counter)
{
case 5:
if(status==1)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[5]; DM=SMG_DM[dis[5]];
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[5];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[5]; DM=SMG_DM[dis[5]];
}
break;
case 4:
if(status==1)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[4]; DM=SMG_DM[dis[4]]&0x7F;
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[4];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[4]; DM=SMG_DM[dis[4]]&0x7F;
}
break;
case 3:
if(status==2)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[3]; DM=SMG_DM[dis[3]];
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[3];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[3];DM=SMG_DM[dis[3]];
}
break;
case 2:
if(status==2)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[2]; DM=SMG_DM[dis[2]]&0x7F;
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[2];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[2]; DM=SMG_DM[dis[2]]&0x7F;
}
break;
case 1:
if(status==3)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[1]; DM=SMG_DM[dis[1]];
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[1];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[1]; DM=SMG_DM[dis[1]];
}
break;
case 0:
if(status==3)
{
if(ss_flag)
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[0];DM=SMG_DM[dis[0]];
}
else
{
DM=0xFF;WM=(WM&0x1F)|SMG_WM[0];DM=0xFF;
}
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[0];DM=SMG_DM[dis[0]];
}
break;
default: break;
}
}
else
{
if((counter==2)||(counter==4))
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[counter];DM=SMG_DM[dis[counter]]&0x7F;
}
else
{
DM=0xFF; WM=(WM&0x1F)|SMG_WM[counter];DM=SMG_DM[dis[counter]];
}
}
counter++;
if(counter>5)
counter=0;
}
LED+SMG+LCD12232F(在板板上全部验证通过):
//LED测试程序:
#include "reg52.h"
#include "absacc.h"
#define uchar unsigned char
#define uint unsigned int
#define LED XBYTE[0xE000]
//------------------------------------------------------------
void delay(uint t)
{
while(t--);
}
//============================================================
void main()
{
LED=0xFF;
while(1)
{
LED=0x00;
delay(60000);
LED=0xFF;
delay(60000);
}
}
//SMG测试
#include "reg52.h"
#include "absacc.h"
#define uchar unsigned char
#define uint unsigned int
#define DM P0
#define WM P2
uchar SMG_DM[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar SMG_WM[]={0x00,0x20,0x40,0x60,0x80,0xA0};
void delay(uint t)
{
while(t--);
}
void main()
{
RD=0;
while(1)
{
WM=(WM&0x1F)|SMG_WM[0]; DM=SMG_DM[6]; delay(100);
WM=(WM&0x1F)|SMG_WM[1]; DM=SMG_DM[5]; delay(100);
WM=(WM&0x1F)|SMG_WM[2]; DM=SMG_DM[4]; delay(100);
WM=(WM&0x1F)|SMG_WM[3]; DM=SMG_DM[3]; delay(100);
WM=(WM&0x1F)|SMG_WM[4]; DM=SMG_DM[2]; delay(100);
WM=(WM&0x1F)|SMG_WM[5]; DM=SMG_DM[1]; delay(100);
}
}
LCD :
//LCD测试
#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define DB0_DB7 P0
sbit RS = P2^5;
sbit RW = P2^6;
sbit E = P2^7;
uchar dis[]="我爱单片机!";
void delay(uint k)
{
while(k--);
}
//--------------------------------------------------------------------------------------
void Wr_CMD(uchar cmd) // 12232F写指令
{
delay(1000);
E=1;
RS=0;
RW=0;
DB0_DB7=cmd;
_nop_();
E=0;
}
//--------------------------------------------------------------------------------------
void Wr_DAT(uchar dat)
{
delay(1000);
E=1;
RS=1;
RW=0;
DB0_DB7=dat;
_nop_();
E=0;
}
//-------------------------------------------------------------------------------------
void Wr_CHI(uchar str[],uchar num)
{
uchar i;
for(i=0;i