Course Design Topic 33: Electronic Clock Based on Single Chip Microcomputer

Publisher:科技飞翔Latest update time:2019-09-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1) Design task: Design an electronic clock that can display the current year, month, day, hour, minute and second and has an alarm function.


2) Index requirements


(1) It can realize the conversion between 12-hour system and 24-hour system.


(2) Year, month, day, hour and minute can be set individually. The item will flash during setting. 


(3) Timing accuracy error: ≤1 second/day.


(4) Liquid crystal display. 


(5) It can realize the alarm clock function.


(6) Power supply: 1 AA dry cell battery (1.5V).


1. Protues simulation diagram (adding temperature display function):


2. Protues simulation diagram (12-24 base conversion):

3. Program source code (add temperature display function):

Because the annotations are very complete, I will not explain them here.

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

================================================================================

【Platform】STC89C51 platform

【Written by】Sumjess

【E-mail】1371129880@qq.com

【Software version】V2.0

【Last updated】September 6, 2018

[For more information, please refer to the following address]

【Website】

  Sumejss blog https://blog.csdn.net/qq_38351824

  Electronics Enthusiasts http://bbs.elecfans.com/zhuti_mcu_1.html

  China Electronics Network http://bbs.21ic.com/icfilter-typeid-11-214.html

---------------------------------------------------------------------------------

【dev.env.】MDK4.14 and above

【Target】STC89C51

First revision: 2019/05/23

Second revision: 2018/05/24

Third revision: 2018/05/26

================================================================================

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

#include //Call the MCU header file

#define uchar unsigned char //unsigned character macro definition variable range 0~255

#define uint unsigned int //Unsigned integer macro definition variable range 0~65535

#include "E2PROM52_Sumjess.h"

#include "Dateconversion_Sumjess.h"

#include "intrins.h"

 

 

bit flag_200ms;

bit flag_100ms;

sbit beep = P1^4; //Buzzer definition

bit flag_beep_en;

uint clock_value; //used as an alarm clock

 

sbit dq = P2^0; //18b20 IO port definition

 

 

uint temperature ; //temperature variable

uchar flag_nl; //Lunar calendar and solar calendar display flag

 

 

uchar menu_1,menu_2;

 

 

 

uchar key_time, flag_value; //Used as an intermediate variable for continuous addition

bit key_500ms;

uchar n_nian,n_yue,n_ri; //Lunar calendar display function

 

 

#include "DS1302_Sumjess.h"

#include "LCD1602_Sumjess.h"

 

 

 

 

/******************Save the data to the eeprom inside the microcontroller******************/

void write_eeprom()

{

SectorErase(0x2000);

byte_write(0x2000, fen1);

byte_write(0x2001, shi1);

byte_write(0x2002, open1);

byte_write(0x2058, a_a);

}

 

/******************Read the data from the eeprom inside the microcontroller*****************/

void read_eeprom()

{

fen1 = byte_read(0x2000);

shi1 = byte_read(0x2001);

open1 = byte_read(0x2002);

a_a = byte_read(0x2058);

}

 

/**************Power on self-test eeprom initialization*****************/

void init_eeprom()

{

read_eeprom(); //Read first

if(a_a != 1) //New MCU initial MCU internal eeprom

{

fen1 = 3;

shi1 = 8;

open1 = 1;

a_a = 1;

write_eeprom(); //Save data

}

}

 

/***************************18b20 initialization function********************************/

void init_18b20()

{

bit q;

dq = 1; //Put the bus high

delay_uint(1); //15us

dq = 0; // give reset pulse

delay_uint(80); //750us

dq = 1; //Put the bus high and wait

delay_uint(10); //110us

q = dq; //Read 18b20 initialization signal

delay_uint(20); //200us

dq = 1; //Pull the bus high to release the bus

}

 

/*************Write the data in 18b20***************/

void write_18b20(uchar dat)

{

uchar i;

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

{ //Write data starts from low bit

dq = 0; // Pull the bus low to start the write time interval 

dq = dat & 0x01; //Write data to the 18b20 bus

delay_uint(5); // 60us

dq = 1; //Release the bus

dat >>= 1;

}

}

 

/****************Read the data in 18b20****************/

uchar read_18b20()

{

uchar i,value;

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

{

dq = 0; //Put the bus low to start the read time interval 

value >>= 1; //Read data starting from low bit

dq = 1; //Release the bus

if(dq == 1) //Start reading and writing data 

value |= 0x80;

delay_uint(5); //60us Reading a time gap requires at least 60us

}

return value; //Return data

}

 

/****************The temperature value read is a decimal***************/

uint read_temp()

{

uint value;

uchar low; //If the interrupt is too frequent when reading the temperature, the interrupt should be turned off, otherwise it will affect the timing of 18b20

init_18b20(); //Initialize 18b20

write_18b20(0xcc); //Skip 64-bit ROM

write_18b20(0x44); //Start a temperature conversion command

delay_uint(50); //500us

 

init_18b20(); //Initialize 18b20

write_18b20(0xcc); //Skip 64-bit ROM

write_18b20(0xbe); //Issue a command to read the temporary register

EA = 0;

low = read_18b20(); //Read temperature low byte

value = read_18b20(); //Read temperature high byte

EA = 1;

value <<= 8; //Shift the high bit of the temperature left by 8 bits

value |= low; //Put the low bit of the temperature read into the low eight bits of value

value *= 0.625; //Convert to decimal temperature value

return value; //Return the read temperature with decimals

}

 

 

----Due to the length of the page, only a part of it is shown. Please download and watch it yourself. The program is very complete-----

-----Follow the official account to download for free-----

4. Program source code (12-24 hexadecimal conversion):

Because the annotations are very complete, I will not explain them here.

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

================================================================================

【Platform】STC89C51 platform

【Written by】Sumjess

【E-mail】1371129880@qq.com

【Software version】V2.0

【Last updated】September 6, 2018

[For more information, please refer to the following address]

【Website】

  Sumejss blog https://blog.csdn.net/qq_38351824

  Electronics Enthusiasts http://bbs.elecfans.com/zhuti_mcu_1.html

  China Electronics Network http://bbs.21ic.com/icfilter-typeid-11-214.html

---------------------------------------------------------------------------------

【dev.env.】MDK4.14 and above

【Target】STC89C51

First revision: 2019/05/23

Second revision: 2018/05/24

Third revision: 2018/05/26

================================================================================

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

#include //Call the MCU header file

#define uchar unsigned char //unsigned character macro definition variable range 0~255

#define uint unsigned int //Unsigned integer macro definition variable range 0~65535

#include "E2PROM52_Sumjess.h"

#include "Dateconversion_Sumjess.h"

#include "intrins.h"

 

bit flag_200ms;

bit flag_100ms;

sbit beep = P1^4; //Buzzer definition

bit flag_beep_en;

uint clock_value; //used as an alarm clock

 

uchar flag_nl, flag_n2; //Lunar calendar and solar calendar display flag

 

 

uchar menu_1,menu_2;

 

 

 

uchar key_time, flag_value; //Used as an intermediate variable for continuous addition

bit key_500ms;

uchar n_nian,n_yue,n_ri; //Lunar calendar display function

 

 

 

#include "DS1302_Sumjess.h"

#include "LCD1602_Sumjess.h"

 

 

 

/******************Save the data to the eeprom inside the microcontroller******************/

void write_eeprom()

{

SectorErase(0x2000);

byte_write(0x2000, fen1);

byte_write(0x2001, shi1);

byte_write(0x2002, open1);

byte_write(0x2058, a_a);

}

 

/******************Read the data from the eeprom inside the microcontroller*****************/

void read_eeprom()

{

fen1 = byte_read(0x2000);

shi1 = byte_read(0x2001);

open1 = byte_read(0x2002);

a_a = byte_read(0x2058);

}

 

/**************Power on self-test eeprom initialization*****************/

void init_eeprom()

{

read_eeprom(); //Read first

if(a_a != 1) //New MCU initial MCU internal eeprom

{

fen1 = 3;

shi1 = 8;

open1 = 1;

a_a = 1;

write_eeprom(); //Save data

}

}

 

 

 

/******************1ms delay function***********************/

void delay_1ms(uint q)

{

uint i,j;

for(i=0;i for(j=0;j<120;j++);

}

 

/******************Write week function***********************/

void write_week(uchar hang,uchar add,uchar week) // write week function

{

if(hang==1)   

write_com(0x80+add);

else

write_com(0x80+0x40+add);   

switch(week)

{

case 1:write_data('M'); //When the week number is 1, display

   write_data('O');

   write_data('N');

   break;

   

case 2:write_data('T'); //Display when the week data is 2

   write_data('U');

   write_data('E');

   break;

case 3:write_data('W'); //Display when the week data is 3

   write_data('E');

   write_data('D');

   break;

case 4:write_data('T'); //The week data is 4 and is displayed

   write_data('H');

   write_data('U');

   break;

case 5:write_data('F'); //Display when the week data is 5

   write_data('R');

   write_data('I');

   break;

case 6:write_data('S'); //Display when the week data is 6

   write_data('T');

   write_data('A');

   break;

case 7:write_data('S'); //Display when the week data is 7

   write_data('U');

   write_data('N');

   break;

}

}

 

/****************Clock display****************/

void init_1602_ds1302()

{

if(flag_n2 == 0) //display 24

{

write_sfm2_ds1302(1,1,shi); //Display

write_sfm2_ds1302(1,4,fen); //Display points

write_sfm2_ds1302(1,7,miao); //Display seconds

write_week(2,12,week);

}

else

{

if(shi==0x20) shi=0x08;

else if(shi==0x13) shi=0x01;

else if(shi==0x14) shi=0x02;

else if(shi==0x15) shi=0x03;

else if(shi==0x16) shi=0x04;

else if(shi==0x17) shi=0x05;

else if(shi==0x18) shi=0x06;

else if(shi==0x19) shi=0x07;

else if(shi==0x21) shi=0x09;

else if(shi==0x22) shi=0x10;

else if(shi==0x23) shi=0x11;

 

write_sfm2_ds1302(1,1,shi); //Display

write_sfm2_ds1302(1,4,fen); //Display points

write_sfm2_ds1302(1,7,miao); //Display seconds

write_week(2,12,week);

}

if(flag_nl == 0) //Display the Gregorian calendar

{

write_sfm2_ds1302(2,2,nian); //Display year

write_sfm2_ds1302(2,5,yue); //Display month

[1] [2]
Reference address:Course Design Topic 33: Electronic Clock Based on Single Chip Microcomputer

Previous article:Course Design Question 2: 7-person majority voting machine
Next article:Course Design Question 1: Eight-person buzzer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号