51 MCU time/counter interrupt

Publisher:ularof不加糖Latest update time:2020-05-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Knowledge about CPU timing


Oscillation period: The period of the oscillation source that provides the timing signal for the microcontroller (crystal oscillator period or external oscillation period)


State cycle: 2 oscillation cycles are 1 state cycle, represented by S. The oscillation cycle is also called the S cycle or clock cycle.


Machine cycle: 1 machine cycle contains 6 state cycles and 12 oscillation cycles.


Instruction cycle: The total time taken to complete one instruction, which is measured in machine cycles.


For example: When the external crystal oscillator is 12MHz, the specific value of the relevant cycle of the 51 microcontroller is:


Oscillation period = 1/12us;


Status cycle = 1/6us;


Machine cycle = 1us; 


Instruction cycle = 1~4us;


How does the timer/counter work?


The timer/counter is essentially an add-1 counter. It increments by 1 as the input pulse of the counter, that is, the counter automatically increments by 1 for each pulse. When the counter is full of 1, another pulse will return the counter to zero, and the overflow of the counter will set the corresponding interrupt flag to 1, sending an interrupt request to the CPU (when the timer/counter interrupt is allowed). If the timer/counter works in the timing mode, it means that the timing time has expired; if it works in the counting mode, it means that the count value is full.


It can be seen that the counter value is the value of the counter when it overflows minus the initial count value plus 1.


51 MCU timer structure


The essence of the timer/counter is a 16-bit add-on counter, which consists of two registers, THx and TLx, with the upper 8 bits and the lower 8 bits. TMOD is the working mode register of the timer/counter, which determines the working mode and function; TCON is the control register, which controls the start of T0 and T1.

Timer/Counter Control


The operation of the 51 MCU timer/counter is controlled by two special function registers: TMOD is used to set its working mode; TCON is used to control its startup and interrupt request.


1. Working mode register TMOD


The working mode register TMOD is used to set the working mode of the timer/counter. The lower four bits are used for T0 and the upper four bits are used for T1. Its format is as follows:

GATE is the gate bit. When GATE=0, it is used to control whether the start of the timer is affected by the external interrupt source signal. As long as TR0 or TR1 in TCON is 1 by software, the timer/counter can be started; when GATA=1, TR0 or TR1 must be 1 by software, and the external interrupt pin INT0/1 must also be high to start the timer/counter. That is, the start condition of the timer at this time adds the condition that the INT0/1 pin is high.


C/T: Timing/counting mode selection bit. C/T = 0 is timing mode; C/T = 1 is counting mode.


M1M0: Working mode setting bit. The timer/counter has four working modes.

2. Control register TCON


The lower 4 bits of TCON are used to control external interrupts, which have been introduced earlier.


Set the start and interrupt request of the timer/counter. The format is as follows:

TF1 (TCON.7): T1 overflow interrupt request flag. When T1 counts overflow, TF1 is automatically set to 1 by hardware. After the CPU responds to the interrupt, TF1 is automatically cleared to 0 by hardware. When T1 is working, the CPU can query the status of TF1 at any time. Therefore, TF1 can be used as a flag for query testing. TF1 can also be set to 1 or cleared to 0 by software, which has the same effect as setting 1 or clearing 0 by hardware.


TR1 (TCON.6): T1 operation control bit. When TR1 is set to 1, T1 starts to work; when TR1 is set to 0, T1 stops working. TR1 is set to 1 or cleared to 0 by software. Therefore, the start and stop of the timer/counter can be controlled by software.


TF0 (TCON.5): T0 overflow interrupt request flag, its function is similar to TF1.


TR0 (TCON.4): T0 operation control bit, its function is similar to TR1.


How the timer/counter works


1. Method 0

Mode 0 is a 13-bit count, consisting of the lower 5 bits of TL0 (the upper 3 bits are unused) and the 8 bits of TH0. When the lower 5 bits of TL0 overflow, they carry to TH0. When TH0 overflows, the TF0 flag in TCON is set and an interrupt request is sent to the CPU.

In timer mode: N = t/ Tcy


The formula for calculating the initial value of the count is: X=213-N.


The initial value of the timer can also be obtained by directly complementing the number of counts.


In counting mode, the counting pulse is an external pulse on the T0 pin.


The gate bit GATE has a special function. When GATE=0, the inverted


Then the output of the OR gate is 1. At this time, only TR0 controls the opening of the AND gate, and the output of the AND gate is


When GATE=1, the control switch is turned on and counting starts; when GATE=1, the external interrupt


The pin signal controls the output of the OR gate. At this time, the opening of the AND gate is controlled by the external interrupt pin.


The signal is controlled together with TR0. When TR0=1, the external interrupt pin signal pin


A high level starts counting, and a low level of the external interrupt pin signal pin stops counting.


This method is often used to measure the width of the positive pulse on the external interrupt pin. 


How the timer/counter works


2. Method 1

The number of count bits in mode 1 is 16 bits, with TL0 as the lower 8 bits and TH0 as the lower 8 bits.


As the high 8 bits, a 16-bit plus 1 counter is formed.

The relationship between the number of counts and the initial value of the count is: X = 216 - N


How the timer/counter works


3. Method 2


Mode 2 is an 8-bit counting mode with automatic reload of initial value.


The relationship between the number of counts and the initial value of the count is: X = 28-N


Working mode 2 is particularly suitable for use as a more accurate pulse signal generator


4. Method 3


Mode 3 is only applicable to timer/counter T0. When timer T1 is in mode 3, it is equivalent to TR1=0 and counting stops.

Working mode 3 divides T0 into two independent 8-bit counters TL0 and TH0. 


What to do when using a timer


The initialization procedure should complete the following tasks:


Assign values ​​to TMOD to determine how T0 and T1 work.


Calculate the initial value and write it into TH0, TL0 or TH1, TL1.


In interrupt mode, assign values ​​to EA and enable timer interrupt.


Set TR0 or TR1 to start the timer/counter timing or counting


Calculation of initial value of counter


A machine cycle is the time it takes for the CPU to complete a basic operation.


Machine cycle = 1/microcontroller clock frequency.


The internal clock frequency of the 51 microcontroller is 12 times the external clock frequency. That is to say, when the frequency of the external crystal oscillator is input into the microcontroller, it must be divided by 12. For example, if you use a 12MHZ crystal oscillator, the internal clock frequency of the microcontroller is 12/12MHZ. When you use a 12MHZ external crystal oscillator, the machine cycle = 1/1M = 1us.


What is the initial value of our timing 1ms? 1ms/1us=1000. That is, to count 1000 numbers, the initial value = 65535-1000+1 (because in fact the counter overflows when it counts to 66636)


Experimental phenomenon: D2 lights up once every 1s.

By calling the timing interrupt, an interrupt is requested from the CPU every 1ms, i is increased by 1, and the cumulative increase is 1000. After 1s, the LED state is reversed.


#include "reg52.h" //This file defines some special function registers of the microcontroller

 

typedef unsigned int u16;   //declare and define the data type

typedef unsigned char u8;

 

sbit led=P2^0;

 

void Timer0Init()

{

//TMOD high four bits T1, low four bits T0 0000 0001 0X01

TMOD |= 0x01;

 

//Initial value = 65535-1000+1 = 64536 = FC18H timing 1ms

TH0=0XFC;

TL0=0X18;

ET0=1; //Turn on T0 timer interrupt

EA=1; // Total interrupt  

TR0=1;

}

 

void Timer1Init()

{

//TMOD high four bits T1, low four bits T0 0000 0001 0X01

TMOD |= 0x10;

 

//Initial value = 65535-1000+1 = 64536 = FC18H timing 1ms

TH0=0XFC;

TL0=0X18;

ET1=1; //Turn on T1 timer interrupt

EA=1; // Total interrupt  

TR1=1;

}

 

void main()

{

Timer1Init();

while(1);

}

 

void Time0() interrupt 1

{

static u16 i ;

TH1=0XFC;

TL1=0X18;

i++;

if(i==1000)//1s

{

i = 0;

led=~led;

}

}

 

void Time1() interrupt 3

{

static u16 i ;

TH1=0XFC;

TL1=0X18;

i++;

if(i==1000)//1s

{

i = 0;

led=~led;

}

}

Reference address:51 MCU time/counter interrupt

Previous article:[51 MCU learning process record] 7 Interrupt timer counter 0 understanding before operation
Next article:51 single chip microcomputer (twenty-one) - timer counting function

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号