|
发表于 2016-8-14 18:57:31
单片机实时时钟软件,用数码管显示时间,按键调节时间、闹钟
- /*
- The function of this program is to set the time and judge the time is true or not
- the value "location" is marked the location what you are setting "1" represent setting day,"2" represent setting month,"3" represent setting year
- the struct "Time" is used to save the time
- when value "location" less than or equal 3,it means that you're setting time now,the digment will only show the time you're setting
- when value "location" more than 3,it means that you have done the setting of time,the digment will show the whole time
- push "left" or "right" button to move the number
- push "up" or "down" button to add or minus the number
- ###this time set program is just a primer version,it can't calculation the time such as you can set 99days a month###
- ###this time program is just a primer+ verson,it not can judge the time is true or not,but it still only can set the data not the time like 2014-03-08,not 22:37:40 ###
- this time program is a primer 1.3 verson,it can judge the time is true or not,and it not only can set the data ,it can set the time,too 2014.3.9
- this program is a realclock program,it can show the time and set the time with the up-down-left-right keys
- this program is a realclock with alarm fucntion ,it can brazzer for 1 minute 2014-03-17
- */
- #include <reg52.h>
- #include "Head.h"
- #include "DS1302_drv.h"
- #include "digitron_drv.h"
- unsigned char gSetTime[]={0x13,0x01,0x21,0x11,0x14,0x00};
- struct Time//time struct,save the time
- {
- unsigned char year;
- unsigned char month;
- unsigned char day;
- unsigned char hour;
- unsigned char minute;
- unsigned char second;
- }time,tempTime,temp;
- struct Alarm
- {
- unsigned char hour;
- unsigned char minute;
- unsigned char open;
- }alarm;
- /**********************************************
- 功能:
- 显示时间
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void DisplayTime(void)
- {
- DigShowNumber(7,time.hour>>4,0);
- DigShowNumber(6,time.hour&0x0f,1);
- DigShowNumber(5,time.minute>>4,0);
- DigShowNumber(4,time.minute&0x0f,1);
- DigShowNumber(3,time.second>>4,0);
- DigShowNumber(2,time.second&0x0f,0);
- }
- /**********************************************
- 功能:
- 显示日期
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void DisplayCalendar(void)
- {
- DigShowNumber(8,2,0);
- DigShowNumber(7,0,0);
- DigShowNumber(6,time.year>>4,0);
- DigShowNumber(5,time.year&0x0f,1);
- DigShowNumber(4,time.month>>4,0);
- DigShowNumber(3,time.month&0x0f,1);
- DigShowNumber(2,time.day>>4,0);
- DigShowNumber(1,time.day&0x0f,0);
- }
- /**********************************************
- 功能:
- 设置时间和日期
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void SetTimeCalendar(void)
- {
- WriteDS1302(ADR_W_WP,WP_OFF); //关闭写保护功能,即允许对各寄存器进行写操作
-
- WriteDS1302(ADR_W_YEAR,time.year); //年
- WriteDS1302(ADR_W_MONTH,time.month); //月
- WriteDS1302(ADR_W_DATE,time.day); //日
- WriteDS1302(ADR_W_HOUR,time.hour); //时
- WriteDS1302(ADR_W_MINUTES,time.minute); //分
- WriteDS1302(ADR_W_SECONDS,time.second); //秒
-
- WriteDS1302(ADR_W_WP,WP_ON); //启动写保护功能,即禁止对各寄存器进行写操作
- }
- /**********************************************
- 功能:
- 读取时间
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void ReadTime(void)
- {
- time.second = ReadDS1302(ADR_R_SECONDS); //读取秒
- time.minute = ReadDS1302(ADR_R_MINUTES); //读取分
- time.hour = ReadDS1302(ADR_R_HOUR); //读取时
- }
- /**********************************************
- 功能:
- 读取日期
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void ReadCalendar(void)
- {
- time.day = ReadDS1302(ADR_R_DATE); //读取日
- time.month = ReadDS1302(ADR_R_MONTH); //读取月
- time.year = ReadDS1302(ADR_R_YEAR); //读取年
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /**********************************************
- 主函数,显示时间和日期
- 设置时间
- **********************************************/
- void main()
- {
- //unsigned char location=4;
- unsigned char location;
- unsigned short cntDly;
- unsigned short tempYear;
- BUZZER=0;
- location=10;
- cntDly=0;
- tempYear=2014;
- ReadTime();
- ReadCalendar();
- if((time.hour/0x10*10+time.hour%0x10)>23)
- {
- //init the time struct
- time.year=0x14;
- time.month=0x03;
- time.day=0x08;
- time.hour=0x15;
- time.minute=0x41;
- time.second=0x20;
- SetTimeCalendar();
- }
- tempTime.year=14;
- tempTime.month=3;
- tempTime.day=8;
- tempTime.hour=15;
- tempTime.minute=41;
- tempTime.second=20;
- temp.year=14;
- temp.month=3;
- temp.day=8;
- temp.hour=15;
- temp.minute=41;
- temp.second=20;
- alarm.hour=0;
- alarm.minute=0;
- alarm.open=0;//闹钟默认为 关
- while(1)
- {
- //ReadTime();//读取时间
- //ReadCalendar();//读取日期
- if(location==10)//no setting now
- {
- if(cntDly<=2000)
- {
- ++cntDly;
- ReadTime(); //读取时间
- if(((time.hour/16*10+time.hour%16)==alarm.hour)&&((time.minute/16*10+time.minute%16)==alarm.minute)&&(alarm.open))
- {
- BUZZER=~BUZZER;
- //P1=0;
- }
- else
- BUZZER=0;
- DisplayTime();
- }
- else if(cntDly>2000&&cntDly<=2500)
- {
- ++cntDly;
- ReadCalendar(); //读取时间
- DisplayCalendar();
- }
- else
- cntDly=0;
- }
- else if(location==9)//set year
- {
- DigShowNumber(8,2,0);
- DigShowNumber(7,0,0);
- DigShowNumber(6,time.year>>4,0);
- DigShowNumber(5,time.year&0x0f,1);
- }
- else if(location==8)//set month
- {
- DigShowNumber(4,time.month>>4,0);
- DigShowNumber(3,time.month&0x0f,1);
- }
- else if(location==7)//set day
- {
- DigShowNumber(2,time.day>>4,0);
- DigShowNumber(1,time.day&0x0f,0);
- }
- else if(location==6)//set hour
- {
- DigShowNumber(7,time.hour>>4,0);
- DigShowNumber(6,time.hour&0x0f,1);
- }
- else if(location==5)//set minute
- {
- DigShowNumber(5,time.minute>>4,0);
- DigShowNumber(4,time.minute&0x0f,1);
- }
- else if(location==4)//set second
- {
- DigShowNumber(3,time.second>>4,0);
- DigShowNumber(2,time.second&0x0f,0);
- }
- else if(location==3)//set alarm open
- {
- DigShow(8,10,0);
- DigShow(7,11,1);
- DigShow(5,alarm.open,0);
- }
- else if(location==2)//set alarm hour
- {
- DigShow(8,10,0);
- DigShow(7,11,1);
- DigShow(4,alarm.hour/10,0);
- DigShow(3,alarm.hour%10,1);
- }
- else if(location==1)//set alarm second
- {
- DigShow(8,10,0);
- DigShow(7,11,1);
- DigShow(2,alarm.minute/10,0);
- DigShow(1,alarm.minute%10,0);
- }
- else
- ;
- if(!(KEY_UP&&KEY_DOWN&&KEY_LEFT&&KEY_RIGHT))
- {
- tempTime.year=time.year/0x10*10+time.year%0x10;
- tempTime.month=time.month/0x10*10+time.month%0x10;
- tempTime.day=time.day/0x10*10+time.day%0x10;
- tempTime.hour=time.hour/0x10*10+time.hour%0x10;
- tempTime.minute=time.minute/0x10*10+time.minute%0x10;
- tempTime.second=time.second/0x10*10+time.second%0x10;
- temp.year=tempTime.year;
- temp.month=tempTime.month;
- temp.day=tempTime.day;
- temp.hour=tempTime.hour;
- temp.minute=tempTime.minute;
- temp.second=tempTime.second;
- delay10ms();
- if(!KEY_UP)
- {
- if(location==9)//set year
- {
- if(tempTime.year==99)
- {
- tempTime.year=0;
- tempYear=2000;
- }
- else
- {
- ++tempTime.year;
- ++tempYear;
- }
- }
- else if(location==8)//set month
- {
- if(tempTime.month==12)
- tempTime.month=1;
- else
- ++tempTime.month;
- }
- else if(location==7)//set day
- {
- if(tempTime.month==1||tempTime.month==3||tempTime.month==5||tempTime.month==7||tempTime.month==8||tempTime.month==10||tempTime.month==12)//1,3,5,7,8,10,12 month 31 days each month
- {
- if(tempTime.day>=31)
- tempTime.day=1;
- else
- ++tempTime.day;
- }
- else if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
- {
- if(tempTime.day>=30)
- tempTime.day=1;
- else
- ++tempTime.day;
- }
- else//2 month
- {
- if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
- {
- if(tempTime.day>=29)
- tempTime.day=1;
- else
- ++tempTime.day;
- }
- else//not leap year 28 days in 2 month
- {
- if(tempTime.day>=28)
- tempTime.day=1;
- else
- ++tempTime.day;
- }
- }
- }
- else if(location==6)//set hour
- {
- if(tempTime.hour==23)
- tempTime.hour=0;
- else
- ++tempTime.hour;
- }
- else if(location==5)//set minute
- {
- if(tempTime.minute==59)
- tempTime.minute=0;
- else
- ++tempTime.minute;
- }
- else if(location==4)//set second
- {
- if(tempTime.second==59)
- tempTime.second=0;
- else
- ++tempTime.second;
- }
- else if(location==3)//set alarm open
- {
- if(alarm.open)
- alarm.open=0;
- else
- alarm.open=1;
- }
- else if(location==2)//set alarm hour
- {
- if(alarm.hour==23)
- alarm.hour=0;
- else
- ++alarm.hour;
- }
- else if(location==1)//set alarm minute
- {
- if(alarm.minute==59)
- alarm.minute=0;
- else
- ++alarm.minute;
- }
- else
- ;
- if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
- {
- if(tempTime.day>30)
- tempTime.day=30;//time judge
- }
- else if(tempTime.month==02)
- {
- if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
- {
- if(tempTime.day>29)//time judge
- tempTime.day=29;
- }
- else//not leap year 28 days in 2 month
- {
- if(tempTime.day>28)//time judge
- tempTime.day=28;
- }
- }
- else
- ;
- delay160ms();
- }
- else if(!KEY_DOWN)
- {
- if(location==9)//set year
- {
- if(tempTime.year==00)
- {
- tempTime.year=99;
- tempYear=2099;
- }
- else
- {
- --tempTime.year;
- --tempYear;
- }
- }
- else if(location==8)//set month
- {
- if(tempTime.month==01)
- tempTime.month=12;
- else
- --tempTime.month;
- }
- else if(location==7)//set day
- {
- if(tempTime.month==1||tempTime.month==3||tempTime.month==5||tempTime.month==7||tempTime.month==8||tempTime.month==10||tempTime.month==12)//1,3,5,7,8,10,12 month 31 days each month
- {
- if(tempTime.day==1)
- tempTime.day=31;
- else
- --tempTime.day;
- }
- else if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
- {
- if(tempTime.day==1)
- tempTime.day=30;
- else
- --tempTime.day;
- }
- else//2 month
- {
- if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
- {
- if(tempTime.day==1)
- tempTime.day=29;
- else
- --tempTime.day;
- }
- else//not leap year 28 days in 2 month
- {
- if(tempTime.day==1)
- tempTime.day=28;
- else
- --tempTime.day;
- }
- }
- }
- else if(location==6)//set hour
- {
- if(tempTime.hour==0)
- tempTime.hour=23;
- else
- --tempTime.hour;
- }
- else if(location==5)//set minute
- {
- if(tempTime.minute==0)
- tempTime.minute=59;
- else
- --tempTime.minute;
- }
- else if(location==4)//set second
- {
- if(tempTime.second==0)
- tempTime.second=59;
- else
- --tempTime.second;
- }
- else if(location==3)//set alarm open
- {
- if(alarm.open)
- alarm.open=0;
- else
- alarm.open=1;
- }
- else if(location==2)//set alarm hour
- {
- if(alarm.hour==0)
- alarm.hour=23;
- else
- --alarm.hour;
- }
- else if(location==1)//set alarm minute
- {
- if(alarm.minute==0)
- alarm.minute=59;
- else
- --alarm.minute;
- }
- else
- ;
- if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
- {
- if(tempTime.day>30)
- tempTime.day=30;//time judge
- }
- else if(tempTime.month==02)
- {
- if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
- {
- if(tempTime.day>29)//time judge
- tempTime.day=29;
- }
- else//not leap year 28 days in 2 month
- {
- if(tempTime.day>28)//time judge
- tempTime.day=28;
- }
- }
- else
- ;
- delay160ms();
- }
- else if(!KEY_LEFT)
- {
- if(location==10)
- location=1;
- else
- ++location;
- delay160ms();
- }
- else if(!KEY_RIGHT)
- {
- if(location==1)
- location=10;
- else
- --location;
- delay160ms();
- }
- else
- ;
- ReadTime();
- ReadCalendar();
- if(tempTime.year!=temp.year)
- time.year=tempTime.year/10*16+tempTime.year%10;
- if(tempTime.month!=temp.month)
- time.month=tempTime.month/10*16+tempTime.month%10;
- if(tempTime.day!=temp.day)
- time.day=tempTime.day/10*16+tempTime.day%10;
- if(tempTime.hour!=temp.hour)
- time.hour=tempTime.hour/10*16+tempTime.hour%10;
- if(tempTime.minute!=temp.minute)
- time.minute=tempTime.minute/10*16+tempTime.minute%10;
- if(tempTime.second!=temp.second)
- time.second=tempTime.second/10*16+tempTime.second%10;
- SetTimeCalendar();
- }
- }
- }
复制代码
- /*=======================================================
- <<<<<<<<<<<<<<<<<<< 爱普雷德技术团队 >>>>>>>>>>>>>>>>>>>>
- <<<<<<<<<<<<<<<<<<< 我们一起追求卓越 >>>>>>>>>>>>>>>>>>>>
- ---------------------------------------------------------
- 旗舰网店: http://aplid.taobao.com/
- 官方邮箱: njaplid@163.com
- 技术QQ1: 535676091
- 技术QQ2: 1915624375
- 技术QQ3: 154211813
- ---------------------------------------------------------
- All right reserved.2013.01
- ---------------------------------------------------------
- 文件名: digitron_drv.c
- 描述: 数码管模块c文件
- 作者:
- =========================================================
- -----------------------------------------------------------------------------------------------------------*/
- #include "digitron_drv.h"
- code uint8 Segment[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0xc7};
- code uint8 Select[] = {0xff,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- code uint8 SegmentWithDot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x47};
- /**********************************************
- 功能:
- 输出位选字节和段码字节
- 输入参数:
- SelectByte: 位选字节
- SegmentByte: 段码字节
- 输出参数:
- 无
- 返回值:
- **********************************************/
- static void DigOutput(uint8 SelectByte, uint8 SegmentByte)
- {
- uint8 i;
-
- DIG_SHCP = 0;
- DIG_STCP = 0;
- for(i=0; i<8; i++)
- {
- if(SegmentByte&0x80)
- {
- DIG_DATA = 1;
- }
- else
- {
- DIG_DATA = 0;
- }
- _nop_();
- DIG_SHCP = 1;
- _nop_();
- DIG_SHCP = 0;
- _nop_();
- SegmentByte <<= 1;
- }
- for(i=0; i<8; i++)
- {
- if(SelectByte&0x80)
- DIG_DATA = 1;
- else
- DIG_DATA = 0;
- _nop_();
- DIG_SHCP = 1;
- _nop_();
- DIG_SHCP = 0;
- _nop_();
- SelectByte <<= 1;
- }
- DIG_STCP = 1;
- _nop_();
- DIG_STCP = 0;
- _nop_();
- }
- /**********************************************
- 功能:
- 在某位显示自定义段码
- 输入参数:
- DigSelect: 数码管位选择(1——8,即最右到最左)
- CustomSeg: 自定义段码值
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void DigShowCustom(uint8 DigSelect, uint8 CustomSeg)
- {
- if(0<DigSelect<9)
- {
- DigOutput(Select[DigSelect],CustomSeg);
- }
- }
- /**********************************************
- 功能:
- 在某位显示数字
- 输入参数:
- DigSelect: 数码管位选择(1——8,即最右到最左)
- Number: 数字(0——9)
- Dp: 小数点(1:显示;0:不显示)
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void DigShowNumber(uint8 DigSelect, uint8 Number, bit Dp)
- {
- if((0<DigSelect<9)&&(Number<10))
- {
- if(Dp)
- {
- DigOutput(Select[DigSelect],(Segment[Number]&~0x80));
- }
- else
- {
- DigOutput(Select[DigSelect],(Segment[Number]|0x80));
- }
- }
- }
- /**********************************************
- 功能:
- 初始化数码管,使数码管全灭
- 输入参数:
- 无
- 输出参数:
- 无
- 返回值:
- **********************************************/
- void DigInit(void)
- {
- DIG_DATA = 0;
- DIG_SHCP = 0;
- DIG_STCP = 0;
- DigOutput(0x00,0xff);
- }
- void DigShow(unsigned char location,unsigned char number,unsigned char dot)
- {
- unsigned char seg,sel,i;
- sel=Select[location];
- if(dot)
- seg=SegmentWithDot[number];
- else
- seg=Segment[number];
- for(i=0; i<8; i++)
- {
- if(seg&0x80)
- {
- DIG_DATA = 1;
- }
- else
- {
- DIG_DATA = 0;
- }
- DIG_SHCP = 1;
- DIG_SHCP = 0;
- seg <<= 1;
- }
- for(i=0; i<8; i++)
- {
- if(sel&0x80)
- DIG_DATA = 1;
- else
- DIG_DATA = 0;
- DIG_SHCP = 1;
- DIG_SHCP = 0;
- sel <<= 1;
- }
- DIG_STCP = 1;
- DIG_STCP = 0;
- }
复制代码
- #include"Head.h"
- void delay10ms(void)
- {
- unsigned char a,b;
- for(b=249;b>0;b--)
- for(a=17;a>0;a--);
- }
- void delay160ms(void)
- {
- unsigned char a,b,c;
- for(c=69;c>0;c--)
- for(b=194;b>0;b--)
- for(a=4;a>0;a--);
- }
复制代码 |
|