单片机:数码管---走时和闹钟
本帖最后由 划句顾 于 2023-4-18 16:02 编辑原理图
走时
代码如下:
/****
*Time: 20220504
*Author:LaoGu
*purpose:数码管动态显示数字:走时功能
****/
#include "reg51.h"
sbit SH = P1^0;
sbit DS = P1^1;
sbit ST = P1^2;
sbit a = P1^3;
sbit b = P1^4;
sbit c = P1^5;
unsigned char buf,p=0,cnt=0,zxm[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
bit sec = 0;
char hour,minute,second;
void Send(char n)
{
char i;
for(i=0;i<8;i++)
{
if(n&0x80)
{
DS = 1;
}
else
DS = 0;
SH = 0;
SH = 1;
n<<=1;
}
ST = 0;
ST = 1;
}
void CS(char n)
{
c = n/4;
b = n%4/2;
a = n%2;
}
void InitT0()
{
TMOD = 0x01;
IE = 0x82;
TH0 = -5000>>8;
TL0 = -5000;
TR0 = 1;
}
void T0ser() interrupt 1
{
TH0 = -5000>>8;
TL0 = -5000;
if(++p>5)
{
p=0;
}
CS(6); //CS(7)也可以
Send(zxm]);
CS(p);
if(++cnt==200)
{
cnt = 0;
sec = 1;
}
}
void main()
{
InitT0();
hour = 12;
minute = 34;
second = 56;
while(1)
{
if(sec)
{
sec = 0;
if(++second>59)
{
second = 0;
if(++minute>59)
{
minute = 0;
if(++hour>23)
hour = 0;
}
}
buf=hour/10;
buf=hour%10;
buf=minute/10;
buf=minute%10;
buf=second/10;
buf=second%10;
}
}
}
闹铃功能:每小时响一次【可通过修改代码来改变闹铃的时间点】
前面的基本和走时的差不多,只是增加了soundtime函数
/****
*Time: 20220504
*Author:LaoGu
*purpose:数码管动态显示数字:按键功能+闹铃
****/
#include "reg51.h"
sbit SH = P1^0;
sbit DS = P1^1;
sbit ST = P1^2;
sbit a = P1^3;
sbit b = P1^4;
sbit c = P1^5;
sbit SET = P3^0;
sbit ADD = P3^3;
sbit SUB = P3^7;
sbit beep = P1^6;
bit sec = 0,key_mark;
unsigned char buf,
p=0,
cnt=0,
state,
zxm[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
char hour,minute,second;
void soundtime();
//传送数据函数
void Send(char n)
{
char i;
for(i=0;i<8;i++)
{
if(n&0x80)
{
DS = 1;
}
else
DS = 0;
SH = 0;
SH = 1;
n<<=1;
}
ST = 0;
ST = 1;
}
//片选函数
void CS(char n)
{
c = n/4;
b = n%4/2;
a = n%2;
}
void InitT0()
{
TMOD = 0x01;
IE = 0x82;
TH0 = -5000>>8;
TL0 = -5000;
TR0 = 1;
}
void T0ser() interrupt 1
{
TH0 = -5000>>8;
TL0 = -5000;
if(++p>5)
{
p=0;
}
CS(6); //CS(7)也可以
Send(zxm]);
CS(p);
if(++cnt==200)
{
cnt = 0;
sec = 1;
}
soundtime();
}
//延迟函数
void Delay()
{
int i,j;
for(i=0;i<1;i++)
for(j = 0;j<1000;j++);
}
/*
*键函数
*/
void key()
{
if(!SET)
{
if(++state>3)
state = 0;
}
else if(!ADD)
{
if(state==1)
{
if(++hour>23)
hour=0;
}
else if(state ==2)
{
if(++minute>59)
minute = 0;
}
else if(state ==3)
{
if(++second>59)
second =0;
}
}
else if(!SUB)
{
if(state==1)
{
if(--hour<0)
hour=23;
}
else if(state ==2)
{
if(--minute<0)
minute = 59;
}
else if(state ==3)
{
if(--second<0)
second =59;
}
}
}
void soundtime()
{
if(minute ==0 && second < 3)//可修改这个来改变闹铃点
{ beep = 1;
Delay();
beep = 0;
}
}
/*
*主函数
*/
void main()
{
InitT0();
hour = 12;
minute = 58;
second = 50;
while(1)
{
//扫描按键
if((!SET || !ADD || !SUB) && !key_mark)
{
Delay();
if(!SET || !ADD || !SUB)
{
key();
key_mark = 1;
}
}
else if(SET && ADD && SUB)
{
key_mark = 0;
}
//实现走时
if(sec)
{
sec = 0;
if(state == 0)
{
if(++second>59)
{
second = 0;
if(++minute>59)
{
minute = 0;
if(++hour>23)
hour = 0;
}
}
}
buf=hour/10;
buf=hour%10;
buf=minute/10;
buf=minute%10;
buf=second/10;
buf=second%10;
}
}
}
static/image/hrline/5.gif
1.可修改条件if(minute ==0 && second < 3)来改变闹铃点 ,
2.second < 3,来控制你的闹钟响多久,second=3时,闹钟就停了。
例如,可修改为if(hour ==14 && minute==0 &&second < 3)则表示为 下午14点,准点闹铃
void soundtime()
{
if(minute ==0 && second < 3)
{ beep = 1;
Delay(); //响1s
beep = 0;
}
} {:10_275:}{:10_275:}{:10_275:}很棒的笔记~ {:5_106:} {:10_256:}后续我会继续完善这个电子时钟
不二如是 发表于 2022-5-5 22:20
很棒的笔记~
{:5_109:}
页:
[1]