MCS51 MCU counter/timer working mode 3 programming example

Publisher:Susan苏Latest update time:2017-10-12 Source: eefocusKeywords:mcs51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

2 8-bit modes. Mode 3 is only applicable to timer 0. If timer 1 is set to mode 3, timer 1 will be in the off state. 
When T0 is in mode 3, THo and TL0 are divided into 2 independent 8-bit counters . Among them, TL0 can be used as both a timer and a counter, and uses all the control bits of the original T0 and its timer return to zero flag and interrupt source. TH0 can only be used as a timer, and uses the control bit TRl, return to zero flag TFl and interrupt source of T1, see the figure below.


Normally, T0 does not run in working mode 3. It can only be used when T1 is in working mode 2 and does not require interruption. At this time, T1 is often used as a serial port baud rate generator (see 1.4), TH0 is used as a timer, and TL0 is used as a timer or counter. Therefore, mode 3 is specially provided for applications where the microcontroller has an independent timer/counter, a timer, and a serial port baud rate generator. At this time, timer 1 can be used in working mode 2, and timer 0 can be used in working mode 3.



 


At this time, T1 is often used as a serial port baud rate generator, TH0 is used as a timer, and TL0 is used as a timer or counter. Therefore, mode 3 is specially provided for the application of the microcontroller with an independent timer/counter, a timer, and a serial port baud rate generator. At this time, timer 1 can be used in working mode 2, and timer 0 can be used in working mode 3.


Understanding the content


Timer/Counter and Interrupt Comprehensive Application Example

Example: Clock timing program design. 


The so-called clock timing is the timing in seconds, minutes and hours. This program can be regarded as a typical representative of timer/counter and interrupt application, which can be regarded as a review of these two parts.


⑴ Basic method of realizing clock timing display with MCS-51 single chip microcomputer

 

① First, calculate the initial value of the count


The key issue of clock timing is the generation of seconds, because seconds are the smallest clock unit. However, when using the timer/counter of MMCS-51 for timing, even in working mode 1, its maximum timing time can only reach 131 milliseconds, which is far from 1 second. Therefore, we implement the second timing by combining hardware timing and software counting, that is, setting the timing time of the timer to 125 milliseconds, so that when the count overflows 8 times, 1 second can be obtained, and the 8-time count can be implemented by software methods.

 

To get 125ms timing, we can use timer/counter 0 and work in mode 1. When the microcontroller is set to a 6MHz crystal oscillator and the initial value of the design number is X, the following equation is obtained:


(216-X)×2us=125000us 


The calculated initial count value X = 3036,

 

The binary representation is 0000101111001101 and the hexadecimal representation is 0BCDH.


② The timer timing is completed in an interrupt mode, so that the overflow times (125 milliseconds each time) can be accumulated through the interrupt service program. When 8 times are counted, the second timing is obtained.

  

③ Realize timing from seconds to minutes and from minutes to hours by accumulating and comparing values ​​in the program 


④ Set the clock display and display buffer

 


Assume that the clock time is displayed on a six-digit LED digital tube (LED5 to LED0) (hour, minute, and second each occupy two digits). To this end, a display buffer must be set in the internal RAM, with a total of 6 units (79H to 7EH), and the corresponding relationship with the digital tube is:

 

      LED5→7EH, LED4→7DH, LED3→7CH, LED2→7BH, LED1→7AH, LED0→79H.


That is, the display buffer stores the values ​​of hours, minutes, and seconds from left to right.

⑤ Assume that the LED display program is SMXS and can be called


⑵ Procedure flow


① Main program (MAIN)

 

The main function of the main program is to initialize the timer/counter and then wait for the 125ms timing interrupt by repeatedly calling the display subroutine.

 

② Interrupt service routine (PIT0)


The main function of the interrupt service program is to perform timing operations. The program starts by determining whether the count overflows for 8 times. If it is less than 8 times, it means that the minimum timing unit second has not been reached, and the interrupt returns; if it is full 8 times, it means that the minimum timing unit second has been reached, and the program continues to execute and perform timing operations.


③ Add 1 subroutine (DAAD1)


The add 1 subroutine is used to complete the operation of adding 1 to the second, minute and hour. There are three places in the interrupt service program to call this subroutine when adding 1 to the second, minute and hour. The add 1 operation includes the following three contents: 

 

Composite number


 

Since each LED display corresponds to an 8-bit buffer unit, the time value represented by the two-bit BCD code occupies one buffer unit each, and only occupies the lower 4 bits. Therefore, before adding 1, the values ​​stored in the two buffer units need to be combined to form a byte, and then the addition operation can be performed. This is where the word "combination" comes from.

 

Decimal Adjustment 

    Add 1 and make a decimal adjustment

 


Fraction 

    The time value after adding 1 is split into two bytes and sent back to their respective buffer units. 

  

⑶ Program List

    The entire reference program list is as follows:


                 ORG 8000H

 

 START:AJMP MAIN


                ORG 800BH 

 

                AJMP PITO 


                ORG 8100H 

  

 MAIN: MOV SP, #60H; establish the stack area 


              MOV R0, #79H ; Display buffer first address 

 

              MOV R7, #06H; Display digits


 ML1: MOV @R0, #00H; clear the display buffer unit to 0 


              INC R0


              DJNZ R7, ML1


              MOV TMOD, #01H; Timer 0, working mode 1

 

              MOV TL0, #0CDH; load the initial value of the counter


              MOV TH0, #0BH 


              SETB 8CH; TR0 is set to 1, timing starts


              SETB AFH; EA is set to 1, interrupts are always enabled


              SETB A9H; ET0 is set to 1, timer 0 interrupt is enabled

 

              MOV 30H, #08H; the required number of count overflows, i.e. the number of loops

 

 ML0: LCALL SMXS ; Call display subroutine


              SJMP ML0 


PITO: PUSH PSW; interrupt service routine, context protection 


             PUSH ACC

 

             SETB PSW.3; RS1RS0=01, select 1 group of general registers


             MOV TL0, #0CDH; counter reload

 

             MOV TH0, #0BH


             MOV A, 30H; the number of cycles decreases by 1


             DEC A

 

            MOV 30H, A

 

            JNZ RET0; less than 8 times, go to RET0 and return

 

            MOV 30H, #08H; after 8 times, start timing operation


             MOV R0, #7AH; Second display buffer unit address

 

           ACALL DAAD1; add 1 per second


           MOV A, R2; add 1 and the seconds value is in R2


           XRL A, #60H; Check whether 60 seconds have passed


           JNZ RET0 ; if not reached, go to RETO to return 

 

           ACALL CLR0; clear the display buffer unit to 0 after 60 seconds 


           MOV R0, #7CH; Display buffer unit address



           ACALL DAAD1 ; points plus 1 


           MOV A, R2 


           XRL A, #60H; Determine whether it has reached 60 points

 

           JNZR 


           ACALL CLR0; At 60 minutes, the minute display buffer unit is cleared to 0


           MOV R0, #7EH ; displays the buffer unit address



           ACALL DAAD1 ; add 1



            MOV A,R2] 

 

            XRL A, #24H; Determine whether it is 24 hours


           JNZR


           ACALL CLR0; At 24 o'clock, the time display buffer unit is cleared to 0

 

 RET0:POP ACC; scene recovery


            POP PSW


           RETI ;Interrupt return 


DAAD1:MOV A, @R0; add 1 subroutine, send the tens digit to A 

 


           DEC R0 



           SWAP A; the tens digit occupies the upper 4 digits 


           ORL A, @R0; the unit digit occupies the lower 4 digits


           ADD A, #01H; add 1


           DA A ; Decimal adjustment


           MOV R2, A; store the full value in R2 temporarily


           ANL A, #0FH; shield the tens digit and extract the ones digit


           MOV @R0, A ; Send the unit value to the display buffer unit

 

           MOV A,R2


           INC R0


           ANL A, #F0H; shield the unit digit and extract the ten digit


           SWAP A; make the tens digit occupy the lower 4 digits

  

           MOV @R0, A ; Send the tens digit to the display buffer unit

 

           RET ; Return


      CLR0:CLR A; Clear buffer unit subroutine


           MOV @R0, A ; clear the tens digit buffer unit to 0 

 

           DEC R0


           MOV @R0, A ; clear the single-digit buffer unit to 0


Keywords:mcs51 Reference address:MCS51 MCU counter/timer working mode 3 programming example

Previous article:Programming of MCS-51 single chip microcomputer driving stepper motor
Next article:Write a single-chip counter program to count pulses

Recommended ReadingLatest update time:2024-11-15 17:51

PIC16F72 Timer1 Usage
PIC16F72 timer 1 is used, and the program is executed as a result, and the LED flashes. Please refer to the datasheet for details of the registers used.   #include pic.h __CONFIG(11111110111001);//bit13-bit7=1;bit6 undervoltage enable (1 enable);bit5=1;bit4 code protection (0 protect); //bit3 power-on delay
[Microcontroller]
PIC16F877A TIMER1 timing operation
/**********************  Title:PIC16F877A TIMER1 timing operation  Author:hnrain  Date:2010-12-28 Use pre-divider  T1CKPS1 T1CKPS1       0 0 1 Division TMR1 clock is crystal clock/(4*1)     0 1 2 Division TMR1 clock is crystal clock/(4*2)     1 0 4 Division TMR1 clock is crystal clock/(4*4)     1 1 8 Division TMR1 clo
[Microcontroller]
TIMER0 controls a single LED to flash
TIMER0 controls a single LED to flash #include p IC .h   __CONFIG(0x3B31);  unsigned char num=0;  void init()  {      TRISB0=0;      RB0=1;     }  void TMR0_init()  {      OPTION=0x02; //0 has pull-up resistor 000 0 010 1:8 frequency division       GIE=1;      PEIE=1;      T0IF=0;      TMR0=250;      T0IE=1;      }  v
[Microcontroller]
[AVR Application] Timer1 measures the width of the positive pulse
#include #include delay.h #define ICP PIND.6 sfrw ICR1=0x26;  flash const unsigned char tabel ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; unsigned char ledbuff ={0x8c,0x8c,0x8c,0x8c,0x8c,0x8c}; unsigned char ov_counter; //counter for timer1 overflow unsigned int rising_edge,falling_edge; //storage
[Microcontroller]
STM8L timer2 timing interrupt
Introduction This article introduces how to use timer2 to generate timer interrupts in the STM8L series. experiment platform Compiler software: IAR for STM8 1.42.2 Hardware platform: stm8l101f3p6 development board Emulator: ST-LINK Library version: STM8L_STMTouch_Lib_V1.1.0 Experimental procedures 1. A
[Microcontroller]
STM8L timer2 timing interrupt
tiny6410 bare metal programming-----Timer (1)
Clock initialization is a rather tedious process, with a long process, involving a large number of port operations, and a lot of basic knowledge to understand. This article first analyzes the principle of clock initialization, and then explains how to implement the process programmatically. 1. What & Why M
[Microcontroller]
tiny6410 bare metal programming-----Timer (1)
What kind of anti-interference technology does MCS51 MCU use?
Since the popular MCS-51 series microcontrollers (such as 8031 ​​and 8032) have no reset instructions and no integrated WATCHDOS circuit, what kind of anti-interference technology does the popular MCS-51 microcontroller use? If there is no external hardware WATCHDOG circuit, software anti-interference technology must b
[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号