This is the main interface
This is the main menu interface
This is the sub-interface for setting time [page]
This is the setting date
Setting the Backlight
This is the status icon
that appears on the main interface after the key tone and alarm are set.
The alarm clock interface appears and a ring tone appears on the speaker.
Birthday countdown reminder function.
Download the complete source code: http://www.51hei.com/bbs/dpj-20391-1.html
Below is the driver for the ds1602 part:
/*------------------------------------
Name: DS1302DriverWritten
by: YuMin_DongTime
: 2013.01.20
------------------------------------*/
#ifndef _DS1302_H_
#define _DS1302_H_
/*------------------------------------
I/O bit definition
------------------------------------*/
sbit DS_RST=P2^4;
sbit DS_SCL=P2^6;
sbit DS_IO=P2^5;
/*------------------------------------
Macro definition
---------- --------------------------*/
#define DS_RST_CLR DS_RST=0
#define DS_SCL_CLR DS_SCL=0
#define DS_IO_CLR DS_IO=0
#define DS_IO_SET DS_IO =1
#define DS_SCL_SET DS_SCL=1
#define DS_RST_SET DS_RST=1
#define SECOND_ADDRESS 0x80 //Seconds register address
#define MINUTE_ADDRESS 0x82 //Minutes register address
#define HOUR_ADDRESS 0x84 //Hours register address
#define DATE_ADDRESS 0x86 //Daily register address
#define MONTH_ADDRESS 0x88 //Monthly
register address#define WEEK_ADDRESS 0x8a //Weekly register address #define
YEAR_ADDRESS 0x8c //Yearly register address
#define CONTROL_ADDRESS 0x8e //Control register address
/*------------------------------------
Global variable definition
------------------------------------*/
UINT8 Year,Month,Date,Hour,Minute,Second,Week; //temporary storage
bit of year, month, day, hour, minute, second and week Set_Hour; //1 means setting the clock , 0 means setting the minute, and the second will stop and clear when setting the time
UINT8 Set_Date; //Set the year, month, and day selection flag
bit Menu0; //Submenu No. 1 time adjustment bit
UINT8 Set_Bell;
UINT8 Bell_Hour,Bell_Minute;
code UINT8 WEEK_TAB[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //week compensation
/*------------------------------------
Write function
------------------------------------*/
void DS1302_WriteByte(UINT8 address,UINT8 byte)
{
UINT8 i;
address=address&0xfe; //R/W control bit low potential write
DS_RST_SET;
for(i=0;i<8;i++) //Send address, find register
{
if(address&0x01)
DS_IO_SET;
else DS_IO_CLR;
address>>=1;
DS_SCL_SET;
DS_SCL_CLR;
}
for(i=0;i<8;i++) //Send data to the found register
{
if(byte&0x01)
DS_IO_SET;
else DS_IO_CLR;
byte>>=1;
DS_SCL_SET;
DS_SCL_CLR;
}
DS_RST_CLR;
}
/*------------------------------------
Read function
------------------------------------*/
UINT8 DS1302_ReadByte(UINT8 address)
{
UINT8 i,byte;
address=address|0x01;
DS_RST_SET;
for(i=0;i<8;i++) //Send address, find register
{
if(address&0x01)
DS_IO_SET;
else DS_IO_CLR;
address>>=1;
DS_SCL_SET;
DS_SCL_CLR;
} for(i=0;i<8;i++) // Read data
from the found register
{
byte>>=1;
if(DS_IO)
byte|=0x80;
DS_SCL_SET;
DS_SCL_CLR;
}
DS_RST_CLR;
return byte;
}
/*------------------------------------
Reading time
--------- --------------------------*/
void DS1302_ReadTime()
{
Hour=DS1302_ReadByte(HOUR_ADDRESS);
Minute=DS1302_ReadByte(MINUTE_ADDRESS);
Second= DS1302_ReadByte(SECOND_ADDRESS)&0x7f;
}
/*------------------------------------
Write time
------------------------------------*/
void DS1302_WriteTime()
{
DS1302_WriteByte(CONTROL_ADDRESS,0X00); //Turn off write protection
DS1302_WriteByte(HOUR_ADDRESS,Hour);
DS1302_WriteByte(MINUTE_ADDRESS,Minute);
DS1302_WriteByte(SECOND_ADDRESS,Second&0x7f);//Clear seconds
DS1302_WriteByte(CONTROL_ADDRESS,0x80); //Turn on write protection
}
/*------------------------------------
Display time
------------------------------------*/
void Display_Time()
{
LCD_WriteCommand(0x34); //16x32 clock display, start expanding command mode
LCD_WriteCommand(0x36); //Start drawing mode
LCD_SendImg16x32(0,1,*(User_16x32+Hour/16),0|Set_Hour);
LCD_SendImg16x32(1,1,*(User_16x32+Hour%16),0|Set_Hour);
LCD_SendImg16x32(2,1,*(User_16x32+10),0);
LCD_SendImg16x32(3,1,*(User_16x32+Minute/16),0|(!Set_Hour&&Menu0));
LCD_SendImg16x32(4,1,*(User_16x32+Minute%16),0|(!Set_Hour&&Menu0));
LCD_SendImg16x32(5,1,*(User_16x32+10),0);
LCD_SendImg16x32(6,1,*(User_16x32+Second/16),0);
LCD_SendImg16x32(7,1,*(User_16x32+Second%16),0);
//LCD_WriteCommand(0x30);
}
/*------------------------------------
Set the alarm interface filling
------------------------------------*/
void Display_Bell()
{
LCD_WriteCommand(0x34); //16x32 clock display, start expanding the command mode
LCD_WriteCommand(0x36); //Start drawing mode
LCD_SendImg16x32(0,1,*(User_16x32+Bell_Hour/10),Set_Bell==0);
LCD_SendImg16x32(1,1,*(User_16x32+Bell_Hour%10),Set_Bell==0);
LCD_SendImg16x32(2,1,*(User_16x32+10),0);
LCD_SendImg16x32(3,1,*(User_16x32+Bell_Minute/10),Set_Bell==1);
LCD_SendImg16x32(4,1,*(User_16x32+Bell_Minute%10),Set_Bell==1);
LCD_SendImg16x32(5,1,*(User_16x32+10),0);
LCD_SendImg16x32(6,1,*User_16x32,0);
LCD_SendImg16x32(7,1,*User_16x32,0);
}
/*------------------------------------
Read date
--------- --------------------------*/
void DS1302_ReadDate()
{
Year=DS1302_ReadByte(YEAR_ADDRESS);
Month=DS1302_ReadByte(MONTH_ADDRESS);
Date= DS1302_ReadByte(DATE_ADDRESS);
}
/*------------------------------------
Write date
------------------------------------*/
void DS1302_WriteDate()
{
DS1302_WriteByte(CONTROL_ADDRESS,0X00); //Turn off write protection
DS1302_WriteByte(DATE_ADDRESS,Date);
DS1302_WriteByte(MONTH_ADDRESS,Month);
DS1302_WriteByte(YEAR_ADDRESS,Year);//Clear seconds
DS1302_WriteByte(CONTROL_ADDRESS,0x80); //Turn on write protection
}
/*------------------------------------
Fill in large digits when setting the date
------------------------------------*/
void Display_SetDate()
{
LCD_WriteCommand(0x34);
LCD_WriteCommand(0x36);
LCD_SendImg16x32(0,1,*(User_16x32+Year/16),Set_Date==0);
LCD_SendImg16x32(1,1,*(User_16x32+Year%16),Set_Date==0);
LCD_SendImg16x32(2,1,*(User_16x32+11),0);
LCD_SendImg16x32(3,1,*(User_16x32+Month/16),Set_Date==1);
LCD_SendImg16x32(4,1,*(User_16x32+Month%16),Set_Date==1);
LCD_SendImg16x32(5,1,*(User_16x32+11),0);
LCD_SendImg16x32(6,1,*(User_16x32+Date/16),Set_Date==2);
LCD_SendImg16x32(7,1,*(User_16x32+Date%16),Set_Date==2);
//LCD_WriteCommand(0x30);
}
/*------------------------------------
Weekday conversion
---------- --------------------------*/
UINT8 Conver_Week(bit c,UINT8 year,UINT8 month,UINT8 date)
{
UINT8 temp1,temp2,week ;
temp1=year/16;
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=date/16;
temp2=date %16;
date=temp1*10+temp2;
if (c==0){year+=0x64;}
temp1=year/0x4;
temp2=year+temp1;
temp2=temp2%0x7;
temp2=temp2+date+WEEK_TAB[month-1];
if (year%0x4==0&&month<3)temp2-=1;
week=temp2 %0x7;
return week;
}
/*------------------------------------
Display date
--- ---------------------------------*/
void Display_Date()
{
//if(Set_Year&&!Set_Month)
// Year=~Year;
LCD_WriteCommand(0x30);
LCD_WriteCommand(0x06);
//Display date format 20XX-XX-XX
LCD_WriteCommand(0x80);
//Read year information from 1302
LCD_SendString("20");
LCD_WriteData(Year/16 |0x30); //Fill in year information
LCD_WriteData(Year%16|0x30);
LCD_WriteData('-'); //delimiter
//Read month information from 1302
LCD_WriteData(Month/16|0x30); //Fill LCD with month information
LCD_WriteData(Month%16|0x30);
LCD_WriteData('-'); //delimiter
//Read date information from 1302
LCD_WriteData(Date/16|0x30); //Fill LCD with date information
LCD_WriteData(Date%16|0x30);
}
/*------------------------------------
Convert and display the week
------------------------------------*/
void Display_Week()
{
LCD_WriteCommand(0x86);
//Week=DS1302_ReadByte(WEEK_ADDRESS);
Week=Conver_Week(0,Year,Month,Date);
LCD_SendString("周");
switch(Week)
{
case 0: LCD_SendString("日");
break;
case 1: LCD_SendString("一");
break;
case 2: LCD_SendString("二");
break;
case 3: LCD_WriteData(0xc8); //The font area code table gets the display code of "三".
LCD_WriteData(0xfd);
break;
case 4: LCD_SendString("四");
break;
case 5: LCD_SendString("五");
break;
case 6: LCD_SendString("六");
break;
default:return ;
}
}
/*------------------------------------
Initialization
------------------------------------*/
void DS1302_Init() //Initialize DS1302
{
DS_SCL_CLR;
DS_RST_CLR;
//DS1302_WriteByte(CONTROL_ADDRESS,0X00);
//DS1302_WriteByte(HOUR_ADDRESS,0x16);
//DS1302_WriteByte(CONTROL_ADDRESS,0x80);
}
#endif
Previous article:Microcontroller measuring bicycle speed program - with simulation
Next article:MCU + lcd12864 LCD perpetual calendar C program
Recommended ReadingLatest update time:2024-11-22 20:53
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
- stm32 + real-time clock DS1302 module my development source code
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- What does transparent transmission in communication networks mean?
- Flexible platform for networking automotive electronic systems
- Up to $180,000! Apple temporarily issues large bonuses to prevent employees from leaving Meta
- [Rawpixel RVB2601 development board trial experience] 5. Key interrupt test
- Xunwei IMX6Q development board QT system Modbus transplantation and use (I)
- Is there any chip that can convert analog signal (potentiometer and DC voltage) into PWM dimming signal?
- Oscilloscope Current
- Infrared thermometer
- Reading notes of the good book "Operational Amplifier Parameter Analysis and LTspice Application Simulation" 01 cover page
- Detailed explanation of Keil C51 usage