Implementation of perpetual calendar (including alarm clock and stopwatch) based on 51 single chip microcomputer

Publisher:qinghongLatest update time:2020-06-23 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

{

showtime.Day++;

if(showtime.Month==1||showtime.Month==3||showtime.Month==5||showtime.Month==7||showtime.Month==8||showtime.Month==10||showtime.Month==12)

if(showtime.Day==32)

showtime.Day=0;

else if(showtime.Month==2)

if(showtime.Day=30)

showtime.Day=0;

else

if(showtime.Day==31)

showtime.Day=0;

}

DateToStr(&showtime);

zifu_dis(1,0,&showtime.DateString[0]); 

break;

case K2: //K2 is pressed, the number decreases by one

//(troublesome thoughts) DArray3[DateWch]--;

//(troublesome thoughts)zifu_dis(0,0,array32show(DArray3)); 

if(Wch==0)

showtime.Year--;

else if(Wch==1)

showtime.Month--;

else if(Wch==2)

showtime.Day--;

DateToStr(&showtime);

zifu_dis(1,0,&showtime.DateString[0]); 

break;

case K4: 

   // Press K4 to exit this loop and return to the mode detection loop

flag=1;

break;

}

}

if(flag==1) //When flag is 1, confirm the modification, reset the date in 1302, and return to the initial mode detection

{

DS1302_SetTime(DS1302_YEAR,showtime.Year);

DS1302_SetTime(DS1302_MONTH,showtime.Month);

DS1302_SetTime(DS1302_DAY,showtime.Day);

flag=0;

Wch=0;

break;

}

}

break;

case MODE2: //Set the alarm

while(1)

{

DS1302_GetTime(&CurrentTime);

DateToStr(&CurrentTime);

TimeToStr(&CurrentTime);

zifu_dis(0,0,&CurrentTime.TimeString[0]); //Do not stop the time display when setting the alarm

if(key_scan()==K1||key_scan()==K2||key_scan()==K3||key_scan()==K4)//Check if any key is pressed, and only perform the operation if a key is pressed

{

switch(key_scan()) //Detect the key again

{

case K3: //K1 is pressed to select which alarm is changed

AlarmWch++;

if(AlarmWch==4)

AlarmWch=0;

break;

case K2: // Press K2 to select whether the hour or second of the alarm is changed

    HourSecWch++;

if(HourSecWch==2)

HourSecWch=0;

break;    

case K1: //K3 is pressed, the number increases by 1

Alarmandshow.Alarm[AlarmWch][HourSecWch]++;

if (Alarmandshow.Alarm[AlarmWch][HourSecWch]==60)

Alarmandshow.Alarm[AlarmWch][HourSecWch]=0;

array2show(&Alarmandshow,AlarmWch);

zifu_dis(2+AlarmWch%2,2+AlarmWch/2*3,&Alarmandshow.showstring[0]);

break;

case K4: //Press K4 to exit this loop and return to the mode detection loop

flag=1;break;

}

}

if(flag==1)

{

AlarmWch=0;

HourSecWch=0; //It is best to clear AlarmWch and HourSecWch to 0, as they will be used later.

flag=0;

break;

}

}

break;

case MODE3: //Stopwatch

while(1) //This loop is used to display the initial interface of the stopwatch  

{

temp=0;

showtime.Second=0;

lcd_init();

zifu_dis(0,3,"00.0");

if(key_scan()==K1) //K1 is pressed, the stopwatch starts timing

{

while(1) //This loop is the loop after the stopwatch starts

{  

delay_ms(73); //Including the program execution time, the total is 100ms  

sec2show(&showtime);

zifu_dis(0,3,&showtime.TimeString[0]);

if(flag==0)

showtime.Second++; //Every 100ms, Second++,

switch(key_scan())

{

case K1:

zifu_dis(temp/3+1,temp*3%9,&showtime.TimeString[0]);

temp++; //Read the stopwatch and record

if(temp==9)

temp=0;

break;

case K2:

flag=~flag;

break;

Case K3:

flag=2;

break;

case K4:

flag=1;

break;

}

if(flag==2||flag==1)

{

if(flag==2)

flag=0;

break;

}

}

}

if(flag==1)

{

flag=0;

break;

}

}

lcd_init();

for(;AlarmWch<4;AlarmWch++)

{

//arrayshow.array2[AlarmWch][HourSecWch]=0;

array2show(&Alarmandshow,AlarmWch);

zifu_dis(AlarmWch/2+2,AlarmWch%2*3+2,&Alarmandshow.showstring[0]);

}

zifu_dis(2,0,"alarm clock");

AlarmWch=0;

HourSecWch=0;

break;

}

}

 

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

Function name: array32show(uchar *array3)

Function: Convert the hours, minutes, and seconds in array[3] into a form that can be directly displayed

Input parameter: *array3 The first address of array[3]

Return value: show show[9]'s first address, which can be used to display directly

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

/*uchar *array32show(uchar *array3)

{

uchar show[5];

show[0] = *array3/10+0x30;

show[1] = *array3++%10+0x30;

show[2] = ':';

show[3] = *array3/10+0x30;

show[4] = *array3%10+0x30 ; //When using LCD to display, it needs to be converted into ASCII code, so 0x30 is added. If it is displayed with a digital tube, it does not need to be added

show[5] = '';

return show;

} *///Not used

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

Function name: show2array3(uchar *show)

Function: Convert the characters in show[] that can be displayed directly into array[3] that can be directly added by one

Input parameter: *show show the first address of the array

Return value: array3 The first address of the array, which can be used directly to perform an increment operation

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

/*uchar *show2array3(uchar *show)

{

uchar array3[3];

array3[0]=(show[0]-0x30)*10+(show[1]-0x30);

array3[1]=(show[3]-0x30)*10+(show[4]-0x30);

array3[2]=(show[6]-0x30)*10+(show[7]-0x30);

return array3;

}*/ 

void array2show(ARRAY2SHOW *arrayshow0,uchar wch)

{

arrayshow0->showstring[0] = arrayshow0->Alarm[wch][0]/10+0x30;

arrayshow0->showstring[1] = arrayshow0->Alarm[wch][0]%10+0x30;

arrayshow0->showstring[2] = ':';

arrayshow0->showstring[3] = arrayshow0->Alarm[wch][1]/10+0x30;

arrayshow0->showstring[4] = arrayshow0->Alarm[wch][1]%10+0x30;

//When using LCD display, it needs to be converted into ASCII code, so 0x30 is added. If it is displayed with a digital tube, it does not need to be added

arrayshow0->showstring[5] = '';

}

 

void sec2show(SYSTEMTIME *secshow)

{

secshow->TimeString[0]=secshow->Second/100+0x30;

secshow->TimeString[1]=secshow->Second%100/10+0x30;

secshow->TimeString[2]='.';

secshow->TimeString[3]=secshow->Second%10+0x30;

secshow->TimeString[4]='';

}

 

#ifndef __KEYPROCESS_H

#define _KEYPROCESS_H

 

#include

#include

#include "delay.h"

#include "key.h"

#include "ds1302.h"

#include "LCD12864.h"

 

 

#ifndef uchar

#define uchar unsigned char

#endif

 

typedef struct _ARRAYSHOW_

{

unsigned char showstring[6];

unsigned char Alarm[4][2];

}ARRAY2SHOW;

 

typedef struct _SHOW_

{

unsigned char showstring[6];

unsigned char array2[4][2];

}show;

 

#define MODE0 0X00

#define MODE1 0X01

#define MODE2 0X02

#define MODE3 0X03

#define TIMESET MODE0

#define DATESET MODE1

#define ALARMSET MODE2

#define SECCON MODE3

 

void key_process(uchar mode);

 

#endif


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

Program description: Use 12864 LCD and ds1302 with buttons to realize perpetual calendar, four-way adjustable alarm clock, stopwatch (based on 51 single chip microcomputer)

Author: Huang Zixuan, Harbin Engineering University

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

#include

#include

#include "delay.h"

#include "ds1302.h"

#include "LCD12864.h"

#include "key.h"

#include "buzzer.h"

#include "keyProcess.h"

SYSTEMTIME CurrentTime; //Store the current time and date read from ds1302

ARRAY2SHOW Alarmandshow; //Store the alarm time and the string used to display the alarm

char code table[7][20]={{"Monday"},{"Tuesday"},{"Wednesday"},{"Thursday"},{"Friday"},{"Saturday"},{"Sunday"}};

sbit led=P1^7;

void main()

{

uchar mode;

Initial_DS1302(); //ds1302 initialization

// DS1302_SetTime(DS1302_HOUR,10);

// DS1302_SetTime(DS1302_MINUTE,0);

// DS1302_SetTime(DS1302_SECOND,0); //Write the initial time to ds1302

// DS1302_SetTime(DS1302_YEAR,17);

// DS1302_SetTime(DS1302_MONTH,1);

// DS1302_SetTime(DS1302_DAY,16); //Write the initial date to ds1302

DS1302_SetTime(DS1302_WEEK,3);

lcd_init(); //lcd12864 initialization

zifu_dis(2,0,"alarm clock");

zifu_dis(2,2,"00:00");

zifu_dis(2,5,"00:00");

zifu_dis(3,2,"00:00");

zifu_dis(3,5,"00:00"); //Set the initial display of the alarm

 

while(1)

{

if(key_scan()==K1||key_scan()==K2||key_scan()==K3||key_scan()==K4)

{

switch (key_scan())

{

case K1: mode=MODE0;break; //MODE0 sets time

case K2: mode=MODE1;break; //MODE0 sets the date

case K3: mode=MODE2;break; //MODE0 sets the alarm

case K4: mode=MODE3;break; //MODE0 sets the stopwatch

}

key_process(mode); //Key processing function

}

DS1302_GetTime(&CurrentTime);

DateToStr(&CurrentTime);

TimeToStr(&CurrentTime);

zifu_dis(0,0,&CurrentTime.TimeString[0]); 

zifu_dis(1,0,&CurrentTime.DateString[0]); //Read the time in ds1302 and display it on the LCD

zifu_dis(1,4,table[CurrentTime.Week]);

if((CurrentTime.Hour==Alarmandshow.Alarm[0][0]&&CurrentTime.Minute==Alarmandshow.Alarm[0][1])||

   (CurrentTime.Hour==Alarmandshow.Alarm[1][0]&&CurrentTime.Minute==Alarmandshow.Alarm[1][1])||

   (CurrentTime.Hour==Alarmandshow.Alarm[2][0]&&CurrentTime.Minute==Alarmandshow.Alarm[2][1])||

   (CurrentTime.Hour==Alarmandshow.Alarm[3][0]&&CurrentTime.Minute==Alarmandshow.Alarm[3][1]))

   // Check if the set alarm time is consistent with the current time, if so, the buzzer will sound.

   

buzzer_delay();

}

}

This is used for the buzzer, which is an IO port that can be pulled high or low.

[1] [2] [3] [4] [5]
Reference address:Implementation of perpetual calendar (including alarm clock and stopwatch) based on 51 single chip microcomputer

Previous article:51 MCU (Part 9). 51 MCU simple project - perpetual calendar and temperature acquisition
Next article:51 single chip perpetual calendar

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号