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

 void delay(int ms)

 {

     while(ms--)

 {

       uchar i;

  for(i=0;i<250;i++)  

   {

    ; ; ; ;

   }

 }

 } 

 

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

Function name: lcd_busy()

Function: Detect LCD busy status.

Input parameters: None

Return value: result When result is 1, busy waiting; when result is 0, idle, can write instruction data

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

bit lcd_busy()

  {                          

     bit result;

     LCD_RS = 0;

     LCD_RW = 1;

     LCD_EN = 1;

      delay_ms(1);

     result = (bit)(LCD_data&0x80);

     LCD_EN = 0;

     return(result); 

  }

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

 /*Write command data to LCD */

 /*RS=L, RW=L, E=high pulse, D0-D7=instruction code. */                                                                

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

 void lcd_wcmd(uchar cmd)

 {                          

    while(lcd_busy());

     LCD_RS = 0;

     LCD_RW = 0;

     LCD_EN = 0;

     delay_ms(1);

     LCD_data = cmd;

    delay_ms(1);

     LCD_EN = 1;

    delay_ms(1);

     LCD_EN = 0;  

 }

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

 /*Write display data to LCD */

 /*RS=H, RW=L, E=high pulse, D0-D7=data. */

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

 void lcd_wdat(uchar dat)

 {                          

    while(lcd_busy());

     LCD_RS = 1;

     LCD_RW = 0;

     LCD_EN = 0;

     LCD_data = dat;

    delay_ms(1);

     LCD_EN = 1;

   delay_ms(1);

     LCD_EN = 0; 

 }

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

 /* LCD initialization settings */

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

 void lcd_init()

 { 

 

 

    LCD_PSB = 1; //Parallel port mode

     

     lcd_wcmd(0x34); //Extended instruction operation

     delay_ms(5);

     lcd_wcmd(0x30); //Basic instruction operation

     delay_ms(5);

     lcd_wcmd(0x0C); //Display on and off cursor

     delay(5);

     lcd_wcmd(0x01); //Clear the LCD display content

     delay(5);

 }

 

 

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

 /* Set the display position X: number of rows Y: number of columns */

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

 void lcd_pos(uchar X,uchar Y)

 {                          

    uchar pos;

    if (X==0)

      {X=0x80;}

    else if (X==1)

      {X=0x90;}

    else if (X==2)

      {X=0x88;}

    else if (X==3)

      {X=0x98;}

    pos = X+Y ;  

    lcd_wcmd(pos); //display address

 }

 

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

 /* Display the character (string) at the set position */

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

 void zifu_dis (uchar X,uchar Y,uchar *dis)

 {

      uchar i;

  lcd_pos(X,Y);   

      i = 0;

     while(dis[i] != '')

      { //Display characters

        lcd_wdat(dis[i]);

        i++;

      }

 }

 

/**************dis_12864.h***************/#ifndef __LCD12864_H__#define __LCD12864_H__

 

#include "delay.h" #include #define uchar unsigned char #define uint unsigned int /*12864 port definition*/ #define LCD_data P0 //Data port sbit LCD_RS = P2^3; //Register selection input sbit LCD_RW = P2^4; //LCD read/write control sbit LCD_EN = P2^5; //LCD enable control sbit LCD_PSB = P3^3; //Serial/parallel control 

 

/*Function declaration*/ void delay(int ms); void lcd_init(); void beep(); void dataconv(); void lcd_pos(uchar X,uchar Y); //Determine the display position void zifu_dis (uchar X,uchar Y,uchar *dis); #endif



 

3. ds1302 clock


  Just give the program directly, and you can search for the corresponding information online.   


 


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

  

                    THE REAL TIMER DS1302 DRIVER LIB

  

              COPYRIGHT (c) 2005 BY JJJ.

                        -- ALL RIGHTS RESERVED --

  

   File Name: DS1302.h

   Author: Jiang Jian Jun

   Created: 2003/7/21

   Modified: NO

   Revision: 1.0

   re

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

#include "ds1302.h"

 

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

Function name: DS1302InputByte(unsigned char d)

Function: Write a byte to the real-time clock (internal function)

Input parameter: d data to be written

Return value: None

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

void DS1302InputByte(unsigned char d)

    unsigned char i;

    ACC = d;

    for(i=8; i>0; i--)

    {

        DS1302_IO = ACC0; //equivalent to RRC in assembly

        DS1302_CLK = 1;

        DS1302_CLK = 0;

        ACC = ACC >> 1; 

    } 

}

 

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

Function Name: DS1302OutputByte(void)

Function: Real-time clock reads a byte (internal function)

Input parameters: None

Return value: data read by ACC

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

unsigned char DS1302OutputByte(void)

    unsigned char i;

    for(i=8; i>0; i--)

    {

        ACC = ACC >> 1; // Equivalent to RRC in assembly

        ACC7 = DS1302_IO;

        DS1302_CLK = 1;

        DS1302_CLK = 0;

    } 

    return(ACC); 

}

 

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

Function name: Write1302(unsigned char ucAddr, unsigned char ucDa)

Function: Write data to the specified address of the real-time clock

Input parameter: ucAddr address to write data

  ucDa Data to be written

Return value: None

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

void Write1302(unsigned char ucAddr, unsigned char ucDa)

{

    DS1302_RST = 0;

    DS1302_CLK = 0;

    DS1302_RST = 1;

    DS1302InputByte(ucAddr); // address, command

    DS1302InputByte(ucDa); // Write 1Byte data

//DS1302_CLK = 1;

    DS1302_RST = 0;

 

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

Function name: Read1302(unsigned char ucAddr)

Function: Read data from a certain address of ds1302

Input parameter: ucAddr The address of the data to be read

Return value: ucData read data

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

unsigned char Read1302(unsigned char ucAddr) //Read data from a certain address of ds1302

{

    unsigned char ucData;

    DS1302_RST = 0;

    DS1302_CLK = 0;

    DS1302_RST = 1;

    DS1302InputByte(ucAddr|0x01); // address, command 

    ucData = DS1302OutputByte(); // Read 1Byte data

//DS1302_CLK = 1;

    DS1302_RST = 0;

    return(ucData);

}

 

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

Function name: DS1302_SetProtect(bit flag)        

Function: Write protection or not

Input parameter: flag

Return value: None 

Others: When flag is 1, the highest bit of the control register corresponding to 0x8E is 1, and write protection is enabled

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

void DS1302_SetProtect(bit flag) //Write protection or not

{

if(flag)

Write1302(0x8E,0x80);

else

Write1302(0x8E,0x00);

}

 

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

Function Name: DS1302_SetTime(unsigned char Address, unsigned char Value)        

Function: Write time to the specified register

Input parameter: Address register address

  Value The time to be written (hex code)

Return value: None 

Others: You can use macros to define the addresses of year, month, hour, etc.

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

void DS1302_SetTime(unsigned char Address, unsigned char Value) // Set time function

{

DS1302_SetProtect(0);

Write1302(Address, ((Value/10)<<4 | (Value%10))); //Convert hex code to BCD code

}

 

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

Function name: DS1302_GetTime(SYSTEMTIME *Time)

Function: Read the date and time and store them in the Time structure

Input parameter: *Time Address of the structure to store date and time

Return value: None 

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

void DS1302_GetTime(SYSTEMTIME *Time)

{

unsigned char ReadValue;

ReadValue = Read1302(DS1302_SECOND);

Time->Second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F); //Convert octal to decimal

ReadValue = Read1302(DS1302_MINUTE);

Time->Minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = Read1302(DS1302_HOUR);

Time->Hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = Read1302(DS1302_DAY);

Time->Day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = Read1302(DS1302_WEEK);

Time->Week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = Read1302(DS1302_MONTH);

Time->Month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

ReadValue = Read1302(DS1302_YEAR);

Time->Year = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0F);

}

 

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

Function name: DateToStr(SYSTEMTIME *Time)  

Function: Convert the read date into a character form that is easy to display

Input parameter: *Time structure to store characters

Return value: None 

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

void DateToStr(SYSTEMTIME *Time)  

{

Time->DateString[0] = Time->Year/10+0x30; //·Separate the ones and tens

Time->DateString[1] = Time->Year%10+0x30;

Time->DateString[2] = '-';

Time->DateString[3] = Time->Month/10+0x30;

Time->DateString[4] = Time->Month%10+0x30;

Time->DateString[5] = '-';

Time->DateString[6] = Time->Day/10+0x30;

Time->DateString[7] = Time->Day%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

Time->DateString[8] = '';

[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号