码迷,mamicode.com
首页 > 其他好文 > 详细

51单片机入门(四)

时间:2017-04-01 23:01:24      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:src   tab   set   int   lsb   time   tco   http   declare   

用数码管实现一个二十四小时的时钟:定义三个按键K1(选择时分秒),K2(加一),K3(进入设置,再按一次则退出)。

代码:

//功能:用数码管二十四小时制的时钟
//作者:刘建东
//日期:2017.4.1
#include<reg52.h>
#define uchar unsigned char
sbit K1=P3^1; //select hour /minute /second
sbit K2=P3^0; //add one
sbit K3=P3^2; //into clock configuration
uchar setplace; //set which place:0,1,2
uchar timer;
uchar hour=0,minute=0;second=0;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
uchar code table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//*****************declare functions
void delayms(uchar);
void TimerConfig();
void IntConfig();
void digiDisplay();
//********************main()
void main()
{
TimerConfig();
IntConfig();
while(1)
{
if(TR0==0)
{
if(K1==0)
{
delayms(10);
if(K1==0)
{
setplace++;
if(setplace>=3) setplace=0;
}
while(~K1);
}
if(K2==0)
{
delayms(10);
if(K2==0)
{
if(setplace==0)
{
second++;
if(second>=60) second=0;
}else if(setplace==1)
{
minute++;
if(minute>=60) minute=0;
}else hour++;
if(hour>=24) hour=0;
}
while(~K2);
}
}
//timer add one
if(timer==20)
{
timer=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
hour=0;
}
}
}
digiDisplay();
}
}
//**********************digiDisplay()
void digiDisplay()
{
LSA=1;LSB=1;LSC=1;
P0=table[hour/10];
delayms(1);
P0=0x00;
LSA=0;LSB=1;LSC=1;
P0=table[hour%10];
delayms(1);
P0=0x00;
LSA=1;LSB=0;LSC=1;
P0=0xa0;
delayms(1);
P0=0xa0;
LSA=0;LSB=0;LSC=1;
P0=table[minute/10];
delayms(1);
P0=0xa0;
LSA=1;LSB=1;LSC=0;
P0=table[minute%10];
delayms(1);
P0=0xa0;
LSA=0;LSB=1;LSC=0;
P0=0xa0;
delayms(1);
P0=0xa0;
LSA=1;LSB=0;LSC=0;
P0=table[second/10];
delayms(1);
P0=0xa0;
LSA=0;LSB=0;LSC=0;
P0=table[second%10];
delayms(1);
P0=0xa0;
}
//********************delayms(uchar)
void delayms(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
{
for(b=110;b>0;b--);
}
}
//****************TimerConfig()
void TimerConfig()
{
TMOD=0X01;
TH0=(65535-43539)/256;
TL0=(65535-43539)%256;
EA=1;
ET0=1;
TR0=1;
}
//*************Interrupt service
void TimerService() interrupt 1
{
TMOD=0X01;
TH0=(65535-43539)/256;
TL0=(65535-43539)%256;
timer++;
}
//*********************IntConfig()
void IntConfig()
{
EA=1;
EX0=1;
IT0=1;
}
//****************Intrupt service
void IntService() interrupt 0
{
delayms(10);
if(K3==0)
{
TR0=~TR0;
setplace=0; //FIRST IS SECOND BIT
}
}

效果图:

技术分享

51单片机入门(四)

标签:src   tab   set   int   lsb   time   tco   http   declare   

原文地址:http://www.cnblogs.com/ljd4you/p/6657686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!