?? main.c
字號:
#include<reg52.h>
#include<LCD12864.H>
/****定義按鍵接口**************/
sbit Mode=P2^6;
sbit Add=P2^7;
sbit Dec=P1^5;
sbit Ent=P1^6;
sbit led1=P1^1;
sbit led2=P1^0;
sbit led3=P1^4;
sbit SPK=P1^3;
unsigned char code para_month[13]={0,0,3,3,6,1,4,6,2,5,0,3,5}; //星期月參變數(shù)
unsigned char cnt,counter,counter1;
unsigned char id,id1,word;
void Add_key1();
void Set_time(unsigned char sel,bit sel_1);
void Add_key2();
void Set_bell(unsigned char sel,bit sel_1);
bit leap_year();
unsigned char week_proc();
extern void Write_24c04( unsigned char add,unsigned char dat);
extern unsigned char Read_24C04(unsigned char add);
/*******************按鍵加一減一程序*******************/
void Add_key1()
{
if(Add==0)
{
Delay12864(20);
if(Add==0)
{
Set_time(id,0);
while(Add==0);
}
}
if(Dec==0)
{
Delay12864(20);
if(Dec==0)
{
Set_time(id,1);
while(Dec==0);
}
}
}
/***************設(shè)定時間**********/
void Set_time(unsigned char sel, bit sel_1) //根據(jù)選擇調(diào)整相應(yīng)項目并寫入DS1302
{
if(sel==1){
if(sel_1==0){year++;if(year>99)year=0;}
else { year--;if(year==0)year=99;}}
if(sel==2){
if(sel_1==0){month++; if(month>12)month=1;}
else {month--;if(month==0)month=12;}}
if(sel==3){
if(sel_1==0){date++;if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date>31) date=1; //大月31天
if (month==4||month==6||month==9||month==11)
if (date>30) date=1; //小月30天
if (month==2)
{if(leap_year()) //閏年的條件
{if (date>29) date=1;} //閏年2月為29天
else
{if (date>28) date=1;}}
} //平年2月為28天
else {date--;if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date==0) date=31; //大月31天
if (month==4||month==6||month==9||month==11)
if (date==1) date=30; //小月30天
if (month==2)
{if(leap_year()) //閏年的條件
{if (date==0) date=29;} //閏年2月為29天
else
{if (date==0) date=28;}} //平年2月為28天
}
}
if(sel==4){
if(sel_1==0){week++;if(week>7)week=1;}
else{week--;if(week==0)week=7;}}
if(sel==5){
if(sel_1==0){hour++;if(hour>23)hour=0;}
else{hour--;if(hour==0)hour=23;}}
if(sel==6){
if(sel_1==0){min++;if(min>59)hour=0;}
else{min--;if(min==0)min=59;}}
if(sel==7){
if(sel_1==0){sec++;if(hour>59)sec=0;}
else{sec--;if(sec==0)sec=59;}}
// write_clock(0x8e,0x00);//允許寫操作
// write_clock(address,(item/10)*16+item%10);//轉(zhuǎn)換成16進(jìn)制寫入1302
// write_clock(0x8e,0x80);//寫保護,禁止寫操作
}
/**************設(shè)定腦鈴*******************/
/*******************按鍵加一減一程序*******************/
void Add_key2()
{
if(Add==0)
{
Delay12864(20);
if(Add==0)
{
Set_bell(id1,0);
while(Add==0);
}
}
if(Dec==0)
{
Delay12864(20);
if(Dec==0)
{
Set_bell(id1,1);
while(Dec==0);
}
}
}
/***************設(shè)定時間**********/
void Set_Bell(unsigned char sel, bit sel_1)//根據(jù)選擇調(diào)整相應(yīng)項目并寫入24
{
signed char address,item;
signed char max,mini;
if(sel==7) {address=0x0e; max=59;mini=0;} //秒
if(sel==6) {address=0x0d; max=59;mini=0;} //分
if(sel==5) {address=0x0c; max=23; mini=0;} //時
if(sel==4) {address=0x0b; max=59;mini=0;} //秒
if(sel==3) {address=0x0a; max=59;mini=0;} //分
if(sel==2) {address=0x09; max=23; mini=0;} //時
if(sel==1)
{
// address=0x08;
if(Add ==0 || Dec ==0)
Bell_Swh=~Bell_Swh;
Write_24c04(0x08,Bell_Swh);
} //開關(guān)
item = Read_24C04(address);
if(sel_1==0)
item++;
else
item--;
if(item>max) item=mini;
if(item<mini) item=max;
Write_24c04(address,item);//寫入24
}
//閏年的計算
bit leap_year()
{
bit leap;
if((year%4==0&&year%100!=0)||year%400==0)//閏年的條件
leap=1;
else
leap=0;
return leap;
}
//星期的自動運算和處理
unsigned char week_proc()
{ unsigned char num_leap;
unsigned char c;
num_leap=year/4-year/100+year/400;//自00年起到y(tǒng)ear所經(jīng)歷的閏年數(shù)
if( leap_year()&& month<=2 ) //既是閏年且是1月和2月
c=5;
else
c=6;
week=(year+para_month[month]+date+num_leap+c)%7;//計算對應(yīng)的星期
return week;
}
/******************做日期運算*****************/
//時間和日期處理程序
void pro_timedate()
{
sec++;
if(sec > 59)
{sec = 0;
min++;
if(min>59)
{min=0;
hour++;
if(hour>23)
{hour=0;
date++;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date>31) {date=1;month++;} //大月31天
if (month==4||month==6||month==9||month==11)
if (date>30) {date=1;month++;} //小月30天
if (month==2)
{if( leap_year()) //閏年的條件
{if (date>29) {date=1;month++;}} //閏年2月為29天
else
{if (date>28) {date=1;month++;}} //平年2月為28天
}
if (month>12) {month=1;year++;}
if (year>99) year=0;
}
}
}
week_proc();
Write_24c04( 0x01,0);
Write_24c04( 0x02,year);
Write_24c04( 0x03,month);
Write_24c04( 0x04,date);
Write_24c04( 0x05,hour);
Write_24c04( 0x06,min);
Write_24c04( 0x07,sec);
//鬧鐘啟用時,報警時間到,啟動Timer1
}
/********定時器0。做時鐘用*****************/
void timer0() interrupt 1 using 0
{
TH0=0x3c;
TL0=0xb0;
cnt++;
// if((word==1)&&(Bell_Swh == 1))
// {
// TR1=0;
// }
if(cnt==20)
{
led1^=1;
cnt=0;
pro_timedate();
}
}
/********定時器0。做時鐘用*****************/
void timer1() interrupt 3 using 0
{
TH1=0x3c;
TL1=0xb0;
counter1++;
counter++;
// if(Bell_Swh == 1)counter1++;//鬧鐘為
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -