C51 MCU————Timer Counter

Publisher:BeaLaity0170Latest update time:2022-04-21 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

51 MCU timer/counter

Timing (timing control, measurement, delay, etc.) and counting (counting the number of external events).

image.png

Software Timing | Digital Circuit Timing | Programmable Timer/Counter

How are they achieved?

Uh! Guess

. . .

Software Timing


The machine executes a program that has no other use but to delay time! In order to achieve the purpose of timing.


Digital Circuit Hardware Timing

Devices such as 555 complete the timing


Programmable timer/counter

Hardware timing, but it is programmable and can be initialized by software to set the timing requirements.


1. Structure

Timer Structure

The timer/counter is essentially an add-one counter that can work in two modes. In fact, both are counting pulses, but the source of the pulses is different!

1. Timing


  1. Add one to the count.

  2. The pulse comes from the oscillator's 12-divided pulse (Fosc/12), which simply means counting the machine cycles.

  3. The initial timing value (TH, TL) needs to be preset.

  4. The initial value setting needs to be a complement code. If the count is increased by one (the complement code is added, the original value is subtracted) and the overflow is added (the original value is reduced to 0), an overflow interrupt occurs.


2. Count


  1. Add one to the count.

  2. The initial counting value (TH, TL) needs to be preset. 

  2. The pulse comes from the T0 or T1 pin.

  2. When the control switch in the figure above is closed (control = 1), the counter starts counting the falling edge generated by the external pulse.

  3. When the counter is full, the initial value becomes 0 and an overflow interrupt is generated.

  4. The maximum frequency of the counting pulse cannot exceed Fosc/24


2. Timing counter related registers

We need to consider several aspects of interruption


Is interrupt turned on?

How to trigger an interrupt

Interrupted start and stop

How to respond after multiple interrupts are triggered

(1) TMOD timing counting mode register (address 89h)

image.png

GATE:

1. Gate signal, GATE=0, TRx=1 self-start mode

2. GATE=1, TRx=1, INTx=1 to start the timer/counter (a method that uses INTx as an external start condition)

C/-T:

Timing, counting selection—1 timing 0 counting

M1M0:

Four working modes of the timing counter

image.png

(2) TCON interrupt request and external interrupt selection (address 88h)

image.png

3. Timing counter working mode

Method 0:

When timer/counter T0 operates in mode 0, only 13 bits of the 16-bit counter are used, namely the upper 8 bits of TH0 and the lower 5 bits of TL0, forming a 13-bit timer/counter (maximum count value 8192).


Method 1:

Timer T0 working mode 1 is similar to working mode 0, the difference lies in the number of bits of the counter. Working mode 0 uses a 13-bit counter to participate in counting, while working mode 1 uses a 16-bit counter to participate in counting (maximum count value 65536).


Working mode 1 is a 16-bit counter. This is the only difference between working mode 1 and working mode 0 in the counting method.


Method 2:

The working mode of automatically reloading the preset number. At this time, M1M0=10.

Automatically reload THx to store the initial value (preset number), TLx is used for counting, and the value of THx is automatically assigned to TLx after counting is completed. (Except for reloading the initial value and the maximum count value, the rest is the same as mode 0)


Method 3: (only applicable to T0)


In this operation mode, timer/counter 0 is split into two independent timers/counters. Among them, TL0 can form an 8-bit timer or counter, while TH0 can only be used as a timer. We know that when used as a timer or counter, it needs control, and overflow flags are required when the count is full. T0 is divided into two, so two sets of control and overflow flags are needed. Where do they come from? TL0 still uses the original T0 flag, while TH0 borrows the T1 flag.

Normally, T0 is allowed to operate in mode 3 only when T1 is operating in mode 2 (when used as a baud rate generator).


3. Timing counter count initial value and initialization

Calculation of the initial value of the count:

Counting method: initial value C = modulus - X (number of pulses to be counted)

Timing method: initial value C = [t /MC] complement = modulus - [t /MC] (t timing time, MC machine cycle, MC = 12/fosc)

Count initial value example:

Mode 0: Initial value C = (64H) complement = 2000H - 64H = 1F9CH

Method 1: Initial value C = (64H) complement = 10000H - 64H = FF9CH

Method 2: Initial value C = (64H) complement = 100H - 64H = 9CH

initialization:

Calculate initial value

TMOD Settings

Write the initial count value THx, TLx

Start the timer, TRx = 1

EA = 1 (turn on the interrupt master switch)

ETx = 1 Enable interrupt

Writing an interrupt subroutine

#include

unsigned char i=0;

int0() interrupt 1 //Timer 0 interrupt function

{

  i++; //Calculate the number of interrupts

}

main()//Main function

{

        //Calculate initial value

    TMOD = 0x01; //TMOD setting

    TH0 = 0x3c;

    TL0 = 0xb0; //initial value

    TR0 = 1 //Start the timer, TRx = 1

    EA=1; //Interrupt main switch

    ET0=1//Timer 0 enables interrupt

}


ORG 0000H

AJMP MAIN

ORG 000bH ;Interrupt subroutine entry

AJMP COUNTING ;Interrupt function name

ORG 0030H

MAIN: 

    MOV TMOD,#01H ;TMOD settings

    MOV TH0 ,#3cH;

    TL0 = #0b0H; ; Initial value

    SETB TR0 ; Start timer, TRx = 1

    SETB EA ;Interrupt main switch

    SETB ET0 ;Timer 0 enables interrupt

HERE:AJMP HERE

COUNTING:

    INC A ;a++

    RETI

END

Keywords:C51 Reference address:C51 MCU————Timer Counter

Previous article:C51 MCU interrupt experiment
Next article:C51 MCU————Interrupt system

Recommended posts

What do the lines outside of pads in KiCAD mean?
ThereisaredcircleoutsidethepadinKiCAD,whichlooksuncomfortable.Whatisitfor?Canitberemoved? Thislinecanbehidden. Theredcircleindicatesthattheareawillnotbecoveredbythesoldermask. Inthe3Dimage,
littleshrimp PCB Design
CC2640R2F: A must-read for beginners
Letmeexplainafewbasicpointsfirst: 1.Theideaoflowpowerconsumption.Manypeopledon'tunderstandthisconcept.Letmeexplainthisfirst.Ifyouunderstandthis,manythingsaboutBLEwillbemuchclearer. Lowpowerconsumptionforpeop
Jacktang Wireless Connectivity
Playing with Zynq Serial 14 - Using GIT for Project Backup and Version Management 6
2.6ApplicationofGITbranches IntheShowlog,wecanseethatthelatestversionofthecurrentmasterisver2.Ifwewanttogobacktover1tochangethefile,wecannotreset"master"tothis...rollbacktover1undermaster
ove学习使我快乐 FPGA/CPLD
Understanding the CAN bus from the bottom up
1.Introduction CANbuswasdevelopedbyBOSCHofGermany,withamaximumspeedof1Mbps.CANhasastrongfaulttolerance,andtheCANcontrollerhasabuilt-inpowerfulerrordetectionandprocessingmechanism. Inaddition,unliketraditio
Jacktang Embedded System
What are the main contents of intelligent sensing based on LoRa and WiFi communication technology?
ThemaincontentofsmartsensingbasedonLoRaandWiFicommunicationtechnologyistorealizeinformationexchangebetweensmartdevicesandthecloud.LoRaisalow-power,long-distancewirelesscommunicationtechnologysuitableforIoTapplicati
成都亿佰特 RF/Wirelessly
[Homemade DA14531 bracelet (similar to Xiaomi bracelet 4/5) development kit] Supporting tutorial 1: Development environment + Getting started..
Otherlinksforthisseriesoftutorialsareasfollows: Supportingtutorial3:ProjectgitdescriptionandmyBSPhttps://en.eeworld.com/bbs/thread-1270515-1-1.html Pastethecontentdirectoryofthefollowingfloor: 1-0.
辛昕 DIY/Open Source Hardware
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号