51 MCU drives ds1302 program (12864 LCD display)

Publisher:BlissfulSunriseLatest update time:2015-06-26 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere




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

Reference address:51 MCU drives ds1302 program (12864 LCD display)

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

STC 1TMCU control DS1302 program
SCLK BIT P1.0 ;DS1302 clock port P1.0 IO BIT P1.1 ;DS1302 data port P1.1 RST BIT P1.2 ;DS1302 chip select port P1.2 NOW DATA 40H ORG 0     JMP Reset ORG 100H Reset:     CALL DS1302_Initial ;Initialize DS1302      MOV DPTR,#INIT     CALL DS1302_SetTime ;Set initial time      MOV R0,#NOW     CALL DS1302_GetTime
[Microcontroller]
PIC microcontroller controls the clock chip DS1302 program
#include p IC .h //Include the predefined  __CONFIG(0x1832)        of the internal resources of the microcontroller ; //Chip configuration word, watchdog off, power-on delay on, power-off detection off, low-voltage programming off, encryption, 4M crystal HS oscillation  #define i_o RB4 //Define the data port of DS
[Microcontroller]
DS1302 clock chip driver based on STM32
/************************************************************************************************************************ * Function: DS1302.H * Function: DS1302 header file * Parameter: None * Return: None * Dependency: Low-level read and write function * Last modified time:  * Description: DS1302 uses GPIO to simula
[Microcontroller]
DS1302 CVAVR Program
//Serial digital tube displays year-month-day hour-minute-second //Chip ATMEGA16L  //Clock 4MHz internal // DS1302 reads and writes data on the rising edge // IO port pull-up is invalid when DS1302 reads data //Short delay 2us wait(); Long delay n ms delay_nms(); #include mega16.h     //#include delay.h    #def
[Microcontroller]
Share your experience in debugging the DS1302 clock chip
       These days, I started to change the perpetual calendar that I used to make with the DS12C887 clock chip to DS1302. I felt quite easy when writing the code for DS12C887 before, but it was a bit tricky to write the code for DS1302, and it was even more difficult during the debugging process. I started writing the
[Microcontroller]
DS1302 C Program Controlled by AVR
The following is a C program for the DS1302 AVR microcontroller. The program structure is very good. However, the webmaster has not debugged the program for this AVR microcontroller. #define ds1302_rst PC0  #define ds1302_io PC1  #define ds1302_sclk PC2  #define set_ds1302_rst_ddr() DDRC|=1 ds1302_rst  #define set_ds1
[Microcontroller]
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号