AT89C51 digital electronic clock

Publisher:mancozcLatest update time:2018-01-08 Source: eefocusKeywords:AT89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This digital clock can achieve a variable precision clock with an accuracy error of ≤ 1S/day, and can easily adjust the clock, time, timing time, etc.  
   
       LED1 bit p1.0; LED definition 
       led2 bit p1.1      
       led3 bit p1.2 
       led4 bit p1.3

       led5 bit p1.4
       led6 bit p1.5 
       led7 bit p1.6 
       led8 bit p1.7 

       s1 bit p0.0 ;digit definition of digital tube digits  
       s2 bit p0.1 
       s3 bit p0.2 
       s4 bit p0.3  
       s5 bit p0.4  
       s6 bit p0.5 
       s7 bit p0.6 
       s8 bit p0.7  

       led_data equ p2 ;digit definition of digital tube 

       key1 bit p3.5 ;key definition 
       key2 bit p3.6 
       key3 bit p3.7 
       key equ 56h 

       time_h equ 57h ;timing initial value high bit 
       time_l equ 58h ;low bit  

       t_h equ 60h ;address corresponding to hours, minutes and seconds 
       t_s equ 61h  
       t_m equ 62h 
       time equ 63h ;clock counting unit 
       timer_h equ 64h ;timing unit 
       timer_m equ 65h ; Timing sub-unit 
       
       timset bit 00h ; Set time mark 

       disstart equ 70h ; Display unit first address  

       int_data equ 45h ; Interrupt data address 
       count_data equ 44h ; Counting unit address 
       timer_data equ 43h ; Timing address 
; ************** The above is the predefined part  

; ************** The following program starts  
       org 00h ; Program start address  
       jmp main ; Jump to the beginning of the code  

       org 1bh ; Timer T1 interrupt service program entry 
       jmp tim1  

       org 030h ; Main program start address 30H  

     main: MOV sp,#30h ; First define the stack 
       lcall rest ; Initialize 
       lcall pro_set ; Set the timer to start working 
     lpp: lcall time_set ; Accept the user set time  
       lcall timer ; Clock processing  
       lcall time_pro ; Time format processing, code type change, etc.  
       lcall time_display ; Display 
       jmp lpp 
; ************* Initialization program *************************** 
     rest: MOV a,#00h ; Accumulator cleared  
        MOV b,#00h 
        MOV p0,#0 ; Digital tube disabled  
        MOV t_h,#0 ; Hour unit 
        MOV t_m,#0 ; Minute unit 
        MOV t_s,#0 ; Second unit 
        MOV time,#00h ; Count overflow times, 20 overflows for one second  
       clr timset ; Timing set flag, 0-> increase 1-> decrease 
        MOV timer_h,#12 ; Timer hour unit, set timing to 12:00  
        MOV timer_m,#00h ; Timer minute unit 
        MOV p2,#255 ; Disable digital tube  
       clr beep ; Disable buzzer  
       ret ; Return  
; *************** Timer T1 interrupt service program ****  
     tim1:clr tr1 ; First stop timing operation 
        MOV th1,time_h ; 
        MOV tl1,time_l  
       inc time ; self-increment 
        MOV a,time ; get the overflow count 
       cjne a,#20,retend ; if it reaches 20, it means 1 second  
       cpl p1.0 ; negate p1.0, p1.1 to indicate seconds  
       cpl p1.1 
        MOV time,#00h ;Restart to wait for 1 second 
       inc t_s ;Increase the second unit by 1  
    retend:setb tr1 ;Start timing  
       reti ;Return from interruption 
; ***************** Set the timer initialization, the timing time is 50ms **** 
   pro_set: MOV dptr,#0000h ;Clear the data pointer  
        MOV tmod,#10h ;Set timer 1 to work in mode 1 
        MOV time_h,#3ch ;Calculate the initial value required for timing 50ms 
        MOV th1,time_h ;Save the high bit 
        MOV time_l,#0c1h ;Low bit  
        MOV tl1,time_l ;Save the low bit 
       setb ea ;Total interrupt enable  
       setb et1 ;Timer 1 enable 
       setb tr1 ;Timer 1 starts running 
       ret ;Return  

; ****************** timer program mainly completes data processing ********** 
    timer: MOV a,t_s ;Get the second unit data 
       cjne a,#60,tend ;If the seconds are less than 60, return 
        MOV t_s,#00h ;Clear the seconds unit 
       inc t_m ;Add 1 to the minute unit 
        MOV a,t_m ;Get the minute unit data 
       cjne a,#60,tend ;If the minute is not equal to 60, return  
        MOV t_m,#00h ;Clear the minute unit  
       setb beep ;Short beep reminder on the hour 
       lcall delay ;Beep delay 
       clr beep ;Stop the buzzer 
       inc t_h ;Add 1 to the time unit 
        MOV a,t_h ;Get the time unit data 
       cjne a,#24,timetest ;If it is not equal to 24, check the timing 
        MOV t_h,#00h ;Clear the time unit 
       jmp tend ;Return  
   timetest:cjne a,timer_h,tend ;If it is not equal to the time unit of the timing, return  
        MOV a,t_m ;Get the minute unit of the timing 
       cjne a,timer_m,tend ;If the current minute is not equal to the timed minute unit, return 
       setb beep ;When the time is up, beep to remind  
       lcall delay 
       clr beep  
       lcall delay  
       setb beep 
       lcall delay 
       clr beep ;Continuous short sound reminder  
     tend:ret ;Return  

; ************ time_display program is mainly used to display time value ************* 
time_display: MOV r0,#disstart ;Get the first address of the display unit  
        MOV r1,#01h ;Start from the first digital tube 
        MOV r2,#06h ;Total 6 digital tubes 
    dislp: MOV led_data,@r0 ;Get the current unit data 
       inc r0 ;Point to the next unit  
        MOV p0,r1 ;Digital display 
        MOV a,r1 ;Prepare for the next number 
       rl a ;Next unit  
        MOV r1,a ;Save  
       lcall delay5ms ;In order to ensure the brightness of the digital tube,  
                     ;but to prevent flickering, delay 5ms 
       djnz r2,dislp ;Repeat display until all data are refreshed  
       ret ;Return  

; ******* time_pro time processing, mainly BCD code conversion, table lookup ***********  
  time_pro:lcall bcd ;BCD code conversion 
        MOV r0,#disstart ;Get the first address of the display unit 
        MOV r2,#06h ;The number of units to be converted 
    prlp: MOV a,@r0 ;Get the data that needs to be converted 
        MOV dptr,#tab_nu ;Get the table header 
        MOV c a,@a+dptr ;Get the converted data 
        MOV @r0,a ;Save it back 
       inc r0 ;Point to the next 
       djnz r2,prlp ;Repeat the conversion until all 6 are completed 
       ret ;Return 

; **************** Code conversion **************************************** 
     bcd: MOV r0,#disstart ;Get the first address 
        MOV a,t_s ;Get the low bit to be converted 
        MOV b,#10 ;Convert the base. If you want to convert the base to decimal, change it to 10  
       div ab ;Calculate A/B     
        MOV @r0,b ;The first bit is converted. Save the converted low bit data  
       inc r0 ;Self-increment 
        MOV @r0,a ;Save the high bit 
       inc r0 ;Get the second data address 
        MOV a,t_m ;Get the second data to be converted 
        MOV b,#10 ;hexadecimal 
       div ab ;calculate 
        MOV @r0,b ;store low bit  
       inc r0 
        MOV @r0,a ;store high bit 
       inc r0 ;the third bit 
        MOV a,t_h ;get data 
        MOV b,#10 ;hexadecimal  
       div ab ;calculate 
        MOV @r0,b ;store low bit 
       inc r0  
        MOV @r0,a ;store high bit  
       ret ;finished, return 
;*************************************************************************************** 
;******** time_set set time ***********************************************************  
; * * 
; * Detect user key press, 1-> set time unit 2-> set subunit, 3-> set increase and decrease mode* 
; * If you need to increase, first set the mode to increase (default is decrease), that is, press KEY3 once, * 
; * Then press KEY1 ,If you want to reduce the time unit, you need to press KEY3 again, and then press KEY1; * 
; * The same is true for the minute setting. * 
;************************************************************************************  
  time_set: MOV p0,#00h ;Disable the digital tube display 
        MOV p2,#255 ;Prevent flashing when pressing keys 
       lcall pro_key ;Search for user key presses  
        MOV a,key ;Search for key values 
       ​​jz tsend ;If it is equal to 0, it means that no key is pressed, and it returns directly 
       cjne a,#1,tset1 ;Is it equal to 1? No next processing procedure 
        MOV key,#00h ; equal to 1, indicating that the time setting should be zero, otherwise it will cause repeated settings 
       jb timset,tset10 ; time setting flag, 1 -> decrease, 0 -> increase 
        MOV a,t_h ; flag 0, increase, get the time unit  
       cjne a,#23,ts1 ; if the time unit is not equal to 23, transfer to the increase operation 
       jmp tsend ; equal to 23 directly return 
     ts1: inc t_h ; the time unit increases by 1  
       jmp tsend ; return  
   tset10: MOV a,t_h ; here it is handled as a decrease 
       jz tsend ; if the time unit is 0, directly return 
       dec t_h ; otherwise, the data is reduced by 1  
       jmp tsend ; return, the following settings for points are the same  
    tset1:cjne a,#2,tset2 ;If the key is not 2, then transfer to the next process 
        MOV key,#00h ;It is 2, indicating setting 
       jb timset,tset20 ;The rest is the same as above  
        MOV a,t_m  
       cjne a,#59, 
       ts2 jmp tsend 
     ts2:inc t_m 
       jmp tsend 
   tset20: MOV a,t_m 
       jz tsend  
       dec t_m  
       jmp tsend 
    tset2:cjne a,#3,tsend ;Equal to 3 means setting the flag 
        MOV key,#00h ;Clear 
       cpl timset ;Invert the flag 
    tsend:ret ;Return 
;************ Key processing Read the keyboard ************************* 
;See the instructions for keyboard query, and the previous related program 
   pro_key: ;Keyboard query subroutine  
       setb key1 ;First output high level and detect the arrival of low level  
       setb key2 ;Different circuits may have different detection methods  
       setb key3 
       jb key1,ke1 ;If the user does not press the first key, go to the next processing part, 
        MOV key,#1 ;Indicates that the user pressed the first key  
       lcall delay20ms ;Software delay to prevent interference 
       jmp pro_key ;Re-query until the user releases the key 
     ke1:jb key2,ke2 ;Process the second key, if not go to the next processing part 
        MOV key,#2 ;The following is similar to the first processing unit. 
       lcall delay20ms 
       jmp pro_key 
     ke2:jb key3,ke3 
        MOV key,#3  
       lcall delay20ms 
       jmp pro_key  
     ke3:ret  
;************************************** 
;******** Timer T0 setting **************  
;The timer works in mode 1. To improve the accuracy, the total timing time is 50ms. 
;The timer overflows 20 times for 1 second. 

  pro_timer: MOV tmod,#01h ;Set the timer to timing mode 1 
        MOV th0,#0ffh ;Initialize the initial value of the timing 
        MOV tl0,#0a1h ; 
       setb ea ;Total interrupt enable 
       setb et0 ;Timer 0 enable 
       setb tr0 ;Start timing 
       ret ;Return  
; ********************************************* 
; *************** Software delay ************* 
    delay:push psw ;Save the original register content 
       clr psw.3 ; 
       clr psw.4 ;Set a new register group 
        MOV r0,#2 ;Delay parameter 1  
        MOV r1,#250 ;Delay parameter 2 
        MOV r2,#2 ;Delay parameter 3 
     dl1:djnz r0,dl1 ;Delay loop 1 
        MOV r0,#250 ; 
     dl2:djnz r1,dl1 ;Delay loop 2 
        MOV r0,#240 ; 
        MOV r1,#248 ; 
     dl3:djnz r2,dl1 ;Delay loop 3 
       nop ;Timing accuracy adjustment 
       pop psw ;Restore the original register 
       ret ;Return 
;********************************************  
;*************** Keyboard delay *************** 
  delay20ms:push psw 
       clr psw.3 
       clr psw.4 
        MOV r0,#250  
        MOV r1,#40 
     d20:djnz r0,d20 
        MOV r0,#250  
       djnz r1,d20 
       pop psw 
       ret 
;************************************** 
;*********** Delay 5ms **************** 
  delay5ms:push psw 
       clr psw.3  
       setb psw.4 
        MOV r0,#250 
        MOV r1,#10 
     d5:djnz r0,d5  
        MOV r0,#250 
       djnz r1,d5  
       pop psw 
       ret 
;************************************************************************************  
;This is a digital display table, in which the numbers with decimal points are 16 larger than the numbers without decimal points 
; For example, the display code of 0 is 0; then the display code of 0 is 16; and so on  
   tab_nu:db 0c0h, 0f9h, 0a4h, 0b0h, 99h , 92h , 82h, 0f8h ; digits 0-7 without decimal point codedb 
       80h , 90h, 88h , 83h , 0c6h, 0a1h, 86h, 8eh ; digits 8-f without decimal point codedb  
       40h , 79h, 24h , 30h , 19h , 12h , 02h, 78h ; digits 0-7 with decimal point codedb 
       00h , 10h, 08h , 03h , 46h , 21h , 06h, 0eh ; digits 8-f with decimal point code 


Keywords:AT89C51 Reference address:AT89C51 digital electronic clock

Previous article:Seconds timer program
Next article:10-bit temperature sensor AD7416 driver

Recommended ReadingLatest update time:2024-11-16 13:56

Design of a fast charger for electric bicycle based on AT89C51
1. Introduction Electric bicycles are favored by many users because of their pollution-free, noise-free, light and beautiful features. However, their limitations are also exposed in use, such as the battery running out halfway, and the battery life will gradually shorten as the use time increases. This paper a
[Microcontroller]
Design of a fast charger for electric bicycle based on AT89C51
Design and implementation of AT89C51ED2 in automobile driving record system
       With the development of society, cars are becoming more and more popular. With the increase in the number of cars, the probability of traffic accidents is also increasing. It is difficult to analyze, judge and repair after an accident using traditional methods. This raises a question for people: how to analyze t
[Microcontroller]
Design and implementation of AT89C51ED2 in automobile driving record system
A wireless intelligent alarm system
With the continuous improvement of material life, people are paying more attention to the safety of people and property, and are looking forward to the birth of intelligent alarm systems that can play the role of watchdogs. The wireless intelligent alarm system introduced in this article can promptly detect imminent e
[Microcontroller]
A wireless intelligent alarm system
Design of automatic range switching voltage measurement system based on AT89C51 microcontroller
In the process of electronic system design and debugging, voltage measurement is often an indispensable item in the measurement and control or measurement system. For voltage measurement, if it changes within a small dynamic range, it is not difficult to achieve accurate measurement regardless of the level. But when t
[Microcontroller]
Design of automatic range switching voltage measurement system based on AT89C51 microcontroller
The simplest temperature measurement system based on AT89C51 and DS18B20
Introduction Temperature measurement and control are widely used in the use of lasers, fiber gratings and other industrial and agricultural production and scientific research. The traditional method of temperature detection is to use analog temperature sensors such as thermocoupl
[Microcontroller]
Improved noise suppression voice collection solution using LMV1090 amplifier and AT89C51 microcontroller
The airborne ultra-shortwave radio is responsible for communicating with the ground tower and friendly aircraft in the fighter aircraft. The quality of its communication directly affects the pilot's combat and training quality. Currently, old-fashioned analog voice radio stations are widely used on aircraft. Such radi
[Microcontroller]
Improved noise suppression voice collection solution using LMV1090 amplifier and AT89C51 microcontroller
Circuit design of infrared remote control signal transmitter for AT89C51 microcontroller
TC9012F is a general-purpose CMOS large-scale integrated circuit for infrared remote control signal transmission. It is suitable for remote control operation of televisions (TVs), video tape recorders (VTR), laser record players and other equipment. In the market, the 9012-type infrared remote control with TC9012F as
[Microcontroller]
Circuit design of infrared remote control signal transmitter for AT89C51 microcontroller
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号