https://static.assets-stash.eet-china.com/album/old-resources/2008/10/28/56e84065-f6a5-49d2-8f24-3032c3b05b6a.rarLED电子钟
/*--------------------------------------------------------------*/
//包含头文件
#include <AT89X52.H>
#include "74HC595.H"
/*--------------------------------------------------------------*/
//键盘接口定义
sbit k1 = P1^0;
sbit k2 = P1^1;
sbit k3 = P1^2;
sbit k4 = P1^3;
sbit BP = P1^4;
/*--------------------------------------------------------------*/
//定义相关变量
unsigned char count; //50ms计数
unsigned char second; //1s计数
unsigned char minute; //1m计数
unsigned char hour; //1h计数
unsigned char time[8]; //时分秒拆位
unsigned char alarm_minute; //闹铃分钟
unsigned char alarm_hour; //闹铃小时
bit flag; //切换闹铃时间(1)和正常走时时间(0)
/*--------------------------------------------------------------*/
//函数定义
void delay(unsigned char x, unsigned char y, unsigned char z)
{
unsigned char tempx,tempy,tempz=z;
do{tempy=y;do{tempx=x;while(--tempx);}while(--tempy);}while(--tempz);
}
/*--------------------------------------------------------------*/
//定时器初始化
void Timer_init(void)
{
TMOD = 0x11;
TH0 = 0x3c; //50ms
TL0 = 0xb7;
TH1 = 0xec; //5ms
TL1 = 0x7e;
IE = 0x8a; //EA = 1; ET0 = 1; ET1 = 1;
IP = 0x02; //PT0 = 1;
TCON = 0x50; //TR0 = 1; TR1 = 1;
}
/*--------------------------------------------------------------*/
//按键处理程序
void Key_pro(void)
{
if(k1 == 0)
{
delay(1, 3, 227); //5ms
if(k1 == 0) second = 0; while(k1 == 0);
}
if(k2 == 0)
{
delay(1, 3, 227); //5ms
if(k2 == 0)
{
if(flag) {alarm_minute++; if(alarm_minute == 60) alarm_minute = 0; while(k2 == 0);}
else {minute++; if(minute == 60) minute = 0; while(k2 == 0);}
}
}
if(k3 == 0)
{
delay(1, 3, 227); //5000+1 us
if(k3 == 0)
{
if(flag) {alarm_hour++; if(alarm_hour == 24) alarm_hour = 0; while(k3 == 0);}
else {hour++; if(hour == 24) hour = 0; while(k3 == 0);}
}
}
}
/*--------------------------------------------------------------*/
//闹铃检测以及设置
void Alarm_detect(void)
{
unsigned char i;
if(k4 == 0) {delay(1, 3, 227); if(k4 == 0) flag = ~flag; while(k4 == 0);}
while(alarm_hour == hour && alarm_minute == minute)
{
for(i = 4; i != 0; i--)
{
BP = ~BP;
P0 = ~P0;
delay(7, 225, 74); //300ms
}
if(k4 == 0) break ;
delay(14, 187, 167); //1000ms
}
}
/*--------------------------------------------------------------*/
//T0定时器中断服务
void T0_intservice(void) interrupt 1
{
TH0 = 0x3c; //50ms
TL0 = 0xb7;
count++;
if(count == 20)
{
count = 0;
second++;
if(second == 60)
{
second = 0;
minute++;
if(minute == 60)
{
minute = 0;
hour++;
if(hour == 24) hour = 0;
}
}
if(flag)
{
time[0] = 10;
time[1] = 10;
time[3] = alarm_minute%10;
time[4] = alarm_minute/10;
time[6] = alarm_hour%10;
time[7] = alarm_hour/10;
}
else
{
time[0] = second%10;
time[1] = second/10;
time[3] = minute%10;
time[4] = minute/10;
time[6] = hour%10;
time[7] = hour/10;
}
}
}
/*--------------------------------------------------------------*/
//T1定时器中断服务
void T1_intservice(void) interrupt 3
{
unsigned char i;
TH1 = 0xec; //5ms
TL1 = 0x7e;
for(i = 0; i <8 ; i++)
{
Ser_IN( pos ); //片选
Ser_IN( seg[ time ] ); //段码
Par_OUT();
if(i == 2 || i == 5) time = 10; //显示"-"
Ser_IN(0x00); //片选
Ser_IN(0xff); //段码
Par_OUT();
}
if(k4 == 0) {BP = 1; P0 = 0xff;}
}
/*--------------------------------------------------------------*/
//主函数
void main(void)
{
Timer_init();
alarm_minute = 2;
while(1)
{
Alarm_detect();
Key_pro();
}
}
/*--------------------------------------------------------------*/
//74HC595驱动头文件
//Note: 74HC595驱动
//File: 74HC595.H
//Date: 08-7-16
//Time: 8:50
// __ __
//Note: MR 主复位接电源正极, OE 使能端,输出有效接电源负极
/*--------------------------------------------*/
#ifndef __74HC595_H__
#define __74HC595_H__
/*--------------------------------------------*/
sbit SD = P2^0; //串行数据输入
sbit SH_CK = P2^1; //移位寄存器时钟输入
sbit ST_CK = P2^2; //存储寄存器时钟输入
/*--------------------------------------------*/
//数码管断码和位码的定义
unsigned char code seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
//0 1 2 3 4 5 6 7 8 9 -
unsigned char code pos[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//1 2 3 4 5 6 7 8
/*--------------------------------------------*/
//函数声明
void Ser_IN(unsigned char Data); //串行数据输入
void Par_OUT(void); //串行数据输出
//void Ser_Par(unsigned char Data); //串行输入,并行输出
/*--------------------------------------------*/
//串行数据输入
void Ser_IN(unsigned char Data)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
SH_CK = 0; //CK=100MHz
SD = Data & 0x80;
Data <<= 1;
SH_CK = 1;
}
}
/*--------------------------------------------*/
//并行数据输出
void Par_OUT(void)
{
ST_CK = 0;
ST_CK = 1;
}
/*--------------------------------------------*/
/*
//串行输入,并行输出
void Ser_Par(unsigned char Data)
{
Ser_IN(Data);
Par_OUT();
}
*/
/*--------------------------------------------*/
#endif
用户257913 2010-4-1 10:44