帮我用AT89C51用c语言写数字钟程序,加上闹钟,调节时间功能
时间: 2023-07-16 14:16:22 浏览: 98
好的,以下是AT89C51使用C语言实现数字钟程序的代码:
```c
#include <reg51.h>
//定义数码管的引脚
sbit dula = P2^6;
sbit wela = P2^7;
sbit beep = P1^5;
//定义闹钟时间
unsigned char alarm_hour = 0;
unsigned char alarm_minute = 0;
//定义时钟时间
unsigned char hour = 0;
unsigned char minute = 0;
unsigned char second = 0;
//定义闹钟状态
unsigned char alarm_on = 0;
//定义延时函数
void delay(unsigned int i)
{
unsigned int j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
//数码管显示函数
void display(unsigned char dat)
{
unsigned char i;
//共阴数码管,段码取反
P0 = ~dat;
for(i=0;i<100;i++);
}
//数码管位选函数
void display_position(unsigned char position, unsigned char value)
{
wela = 1;
//共阳数码管,位选码不取反
P0 = ~(0x01 << position);
wela = 0;
display(value);
}
//时钟中断服务函数
void timer0_isr() interrupt 1
{
//定时器初值为65536 - 4608 = 60928,每1ms中断一次,所以1s中断1000次
TH0 = 0xEA;
TL0 = 0x80;
//计算秒数
second++;
if(second >= 60)
{
second = 0;
minute++;
if(minute >= 60)
{
minute = 0;
hour++;
if(hour >= 24)
{
hour = 0;
}
}
}
}
//闹钟中断服务函数
void timer1_isr() interrupt 3
{
//定时器初值为65536 - 4608 = 60928,每1ms中断一次,所以1s中断1000次
TH1 = 0xEA;
TL1 = 0x80;
if(alarm_on && hour == alarm_hour && minute == alarm_minute)
{
//响铃
beep = 1;
delay(1000);
beep = 0;
}
}
//设置闹钟时间
void set_alarm_time()
{
unsigned char key_value = 0xFF;
while(key_value != 0x0D)
{
//设置闹钟小时
display_position(1, alarm_hour / 10);
display_position(2, alarm_hour % 10);
display_position(3, 10);
//设置闹钟分钟
display_position(4, alarm_minute / 10);
display_position(5, alarm_minute % 10);
display_position(6, 10);
//等待按键
P3 = 0xFF;
while(P3 == 0xFF);
delay(10);
key_value = P3;
//按键扫描
if(key_value == 0x0E)
{
alarm_hour++;
if(alarm_hour >= 24)
{
alarm_hour = 0;
}
}
else if(key_value == 0x0D)
{
break;
}
else if(key_value == 0x0B)
{
alarm_minute++;
if(alarm_minute >= 60)
{
alarm_minute = 0;
}
}
else if(key_value == 0x07)
{
alarm_hour--;
if(alarm_hour >= 24)
{
alarm_hour = 23;
}
}
else if(key_value == 0x03)
{
alarm_minute--;
if(alarm_minute >= 60)
{
alarm_minute = 59;
}
}
}
}
//设置时钟时间
void set_time()
{
unsigned char key_value = 0xFF;
while(key_value != 0x0D)
{
//设置小时
display_position(1, hour / 10);
display_position(2, hour % 10);
display_position(3, 10);
//设置分钟
display_position(4, minute / 10);
display_position(5, minute % 10);
display_position(6, 10);
//等待按键
P3 = 0xFF;
while(P3 == 0xFF);
delay(10);
key_value = P3;
//按键扫描
if(key_value == 0x0E)
{
hour++;
if(hour >= 24)
{
hour = 0;
}
}
else if(key_value == 0x0D)
{
break;
}
else if(key_value == 0x0B)
{
minute++;
if(minute >= 60)
{
minute = 0;
}
}
else if(key_value == 0x07)
{
hour--;
if(hour >= 24)
{
hour = 23;
}
}
else if(key_value == 0x03)
{
minute--;
if(minute >= 60)
{
minute = 59;
}
}
}
}
void main()
{
//定时器0用于时钟计时
TMOD |= 0x01;
TH0 = 0xEA;
TL0 = 0x80;
ET0 = 1;
EA = 1;
TR0 = 1;
//定时器1用于闹钟响铃
TMOD |= 0x10;
TH1 = 0xEA;
TL1 = 0x80;
ET1 = 1;
TR1 = 1;
while(1)
{
//显示时钟时间
display_position(1, hour / 10);
display_position(2, hour % 10);
display_position(3, 10);
display_position(4, minute / 10);
display_position(5, minute % 10);
display_position(6, 10);
//按键扫描
P3 = 0xFF;
if(P3 == 0x0E)
{
set_time();
}
else if(P3 == 0x0D)
{
set_alarm_time();
}
else if(P3 == 0x0B)
{
alarm_on = !alarm_on;
}
delay(10);
}
}
```
以上代码实现了数字钟程序,并且加入了闹钟和调节时间功能。其中,使用定时器0来计时,定时器1用于响铃。可以通过按键来设置时钟时间、闹钟时间和开关闹钟功能。数码管采用共阴数码管,通过位选和段选来显示数字。闹钟响铃时,蜂鸣器会响一秒钟。
阅读全文