标题: 基于51单片机的电子万年历代码 [打印本页]

作者: 10086MVP    时间: 2018-12-7 14:58
标题: 基于51单片机的电子万年历代码
#include < reg52.h >

#include < character.h >

#include < lcd.h >

#include < clock.h >

#include < sensor.h>

#include < calendar.h >

#include < key.h >

/*****************************预定义**************************************/

#define uchar unsigned char

#define uint unsigned int

/****************************************************************************/ sbit bell = P2 ^ 0; //定义蜂鸣器端口

sbit in = P2 ^ 7; //定义红外检测端口

/***************************************************************************** * 名称: Timer0_Service() inturrupt 1

* 功能: 中断服务程序整点报时3声嘟嘟的声音

*****************************************************************************/ void Timer0_Service() interrupt 1

{

static uchar count = 0;

static uchar flag = 0; //记录鸣叫的次数

count = 0;

TR0 = 0; //关闭Timer0

TH0 = 0x3c;

TL0 = 0XB0; //延时50 ms

TR0 = 1 ; //启动Timer0

count ++;

if( count == 20 ) //鸣叫1 秒

{

bell = ~ bell;

count = 0;

flag ++;

}

if( flag == 6 )

{

flag = 0;

TR0 = 0; //关闭Timer0

}

}

/*****************************************************************************

* 名称: Timer2_Servie() interrupt 5

* 功能: 中断服务程序整点报时一分钟

*****************************************************************************/

void Timer3_Service() interrupt 5

{

static uchar count;

TF2 = 0; //软件清除中断标志

count ++;

if( in == 1 )

{

count = 0; //计算清0

TR2 = 0; //关闭Timer2

bell = 1; //关闭蜂鸣器

}

if( count == 120 ) // 一分钟后关闭报警

{

count = 0; //计算清0

TR2 = 0; //关闭Timer2

bell = 1; //关闭蜂鸣器

}

}

/******************************************************************************

* 函数名称:main()

********************************************************************************/

void main( void )

{

uchar clock_time[7] = { 0x00, 0x00, 0x12, 0x29, 0x06, 0x08 }; //定义时间变量秒分时日月年uchar alarm_time[2] = { 0, 0}; //闹钟设置alarm_time[0]: 分钟alarm_time[1] :小时

uchar temperature[2]; //定义温度变量temperature[0] 低8位temperature[1] 高8位

Lcd_Initial(); //LCD初始化

clock_Initial( clock_time ); //时钟初试化

/***********************中断初始化***************************/

EA = 1; //开总中断

ET0 = 1; //Timer0 开中断

ET2 = 1; //Timer2 开中断

TMOD = 0x01 ; //Timer0 工作方式1

RCAP2H = 0x3c;

RCAP2L = 0xb0; //Timer2 延时50 ms

while( 1 )

{

switch( Key_Scan() )

{

case up_array:

{

Key_Idle();

}

break;

case down_array:

{

Key_Idle();

}

break;

case clear_array:

{

Key_Idle();

}

break;

case function_array:{

Key_Function( clock_time, alarm_time );

}

case null:

{

Clock_Fresh( clock_time ); //时间刷新

Lcd_Clock( clock_time ); //时间显示

Sensor_Fresh( temperature ); //温度更新

Lcd_Temperture( temperature ); //温度显示

Calendar_Convert( 0 , clock_time );

Week_Convert( 0, clock_time );

//整点报时

if( ( * clock_time == 0x59 ) && ( * ( clock_time + 1 ) == 0x59 ) )

{

bell = 0;

TR2 = 1; //启动Timer2

}

//闹钟报警

if( * alarm_time == * ( clock_time + 1 ) ) //分钟相吻合

if( * ( alarm_time + 1 ) == *( clock_time + 2 ) ) //小时相吻合

{

bell = 0;

TR2 = 1; //启动Timer2

}

}

break;

}

}

}





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1