#include <DS1302.h>
//define common anode font code 0123456789-
unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char time[]={0,0,0,0,0,0,0,0};//used to store time
unsigned char date[]={0,0,0,0,0,0,0,0};//used to store date
SYSTEMTIME time1; //A structure named SYSTEMTIME has been defined in the ds1302.h file. Here we define a variable named
//time in the SYSTEMTIME structure
void delay(unsigned char i) //delay subroutine
{
unsigned char j;
while((i--)!=0)
{
for(j=123;j>0;j--);
}
}
unsigned char button_time(n,x,y) //Clock adjustment subroutine
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=0;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==0)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
unsigned char button_date(n,x,y) //Date adjustment subroutine
unsigned char n,x,y;
{
if(P1_7==0)
{
delay(50);
if(P1_7==0)
{
n++;
if(n==x)
n=1;
while(P1_7==0);
}
}
if(P1_5==0)
{
delay(50);
if(P1_5==0)
{
if(n==1)
n=y;
else
n--;
while(P1_5==0);
}
}
return n;
}
void display(second10,second1,minute10,minute1,hour10,hour1) //display subroutine
unsigned char second10,second1,minute10,minute1,hour10,hour1;
{
P2=0xfe;
P0=dispcode[second10]; //display the tens digit of seconds
delay(1);
P2=0xfd;
P0=dispcode[second1]; //Display the units digit of seconds
delay(1);
P2=0xfb;
P0=dispcode[10]; //display "-"
delay(1);
P2=0xf7;
P0=dispcode[minute10]; //display the tens digit of minute
delay(1);
P2=0xef;
P0=dispcode[minute1]; //display the unit digit of minute
delay(1);
P2=0xdf;
P0=dispcode[10]; //display "-"
delay(1);
P2=0xbf; P0=dispcode[hour10]; //
delay(1)
for the displayed tens digit ;
P2=0x7f;
P0=dispcode[hour1]; //the unit digit of the display
delay(1);
}
void main()
{
unsigned char flag;
Initial_DS1302(); //Initialize the DS1302 clock chip,
while(1)
{
DS1302_GetTime(&time1); //Read time parameters
time[5]=(time1.Second)%10; //Store the units digit of the second into time[5]
time[4]=(time1.Second)/10; //Store the tens digit of the second into time[4]
time[3]=(time1.Minute)%10; //Store the units digit of the minute into time[3]
time[2]=(time1.Minute)/10; //Store the tens digit of the minute into time[2]
time[1]=(time1.Hour)%10; //Store the units digit of the hour into time[1]
time[0]=(time1.Hour)/10; //Store the tens digit of the hour into time[0]
date[5]=(time1.Day)%10;
date[4]=(time1.Day)/10;
date[3]=(time1.Month)%10;
date[2]=(time1.Month)/10;
date[1]=(time1.Year)%10;
date[0]=(time1.Year)/10;
if(P1_4==0) //If you press the Time Start key once, the clock starts to display the time normally, and press it again to display the date
{
delay(50);
if(P1_4==0)
{
flag++;
if(flag>1)
{
flag=0;
}
}
while(P1_4==0);
}
if(P1_6==0) //If you press the Time Set key once, the date starts to be displayed, and press it again to enter the date and clock adjustment mode
{
delay(50);
if(P1_6==0)
{
flag++;
if(flag>7)
{
flag=0;
}
}
while(P1_6==0);
}
switch(flag)
{
case 0:display(time[0],time[1],time[2],time[3],time[4],time[5]); //Call the sub-function display to display the data stored in the array time
break;
case 1:display(date[0],date[1],date[2],date[3],date[4],date[5]); //Call the sub-function display to display the data stored in the array date
break;
case 2:time1.Year=button_date(time1.Year,100,99); //调整年
DS1302_SetTime(0x8c,time1.Year);
display(date[0],date[1],10,10,10,10);
break;
case 3:time1.Month=button_date(time1.Month,13,12); //调整月
DS1302_SetTime(0x88,time1.Month);
display(10,10,date[2],date[3],10,10);
break;
case 4:time1.Day=button_date(time1.Day,32,31); //调整日
DS1302_SetTime(0x86,time1.Day);
display(10,10,10,10,date[4],date[5]);
break;
case 5:time1.Hour=button_time(time1.Hour,24,23); //调整时
DS1302_SetTime(0x84,time1.Hour);
display(time[0],time[1],10,10,10,10);
break;
case 6:time1.Minute=button_time(time1.Minute,60,59); //调整分
DS1302_SetTime(0x82,time1.Minute);
display(10,10,time[2],time[3],10,10);
break;
case 7:time1.Second=button_time(time1.Second,60,59); //调整秒
DS1302_SetTime(0x80,time1.Second);
display(10,10,10,10,time[4],time[5]);
break;
}
}
}
Previous article:12864 LCD display serial driver demonstration C language programming
Next article:Calendar clock based on DS1302 (1602 LCD display DS1302 clock) C language programming
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Do you know how to save power in RTOS applications?
- The problem of lower yield strength
- What you should know about the switching characteristics of Mosfet
- Understanding wireless communication technology
- Solving the Challenges of High-Speed Serial Connectivity
- Smart MM32 eMiniBoard Review Summary
- Infineon Tmall flagship store offers huge discounts Part 1 - Limited cashback on a first-come, first-served basis, Double 11 special promotion!
- Good evening, experts.
- The startup method of high performance DSP in large capacity wireless transmission technology
- You may need oscilloscope information urgently, especially analog ones. You can get the information here.