[STC MCU Learning] Lesson 10: Timers and Counters of MCU

Publisher:ShiningSmileLatest update time:2022-08-16 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1.10.4.3、TLx、THx

TL0+TH0: 16-bit register of timer 0

TL1+TH1: 16-bit register of timer 1

Suppose our pulse width is 0.5us, and we want to set the timing to 0.5ms and 4.444ms, then the counts are:

1000 = 0x3E8 = High 0x3 Low 0xE8 => TL0 = 0xE8 TH0 = 0x3

8888 = 0x22B8 = High 0x22 Low 0xB8 => TL0 = 0xB8 TH0 = 0x22


Correct a previous mistake, overflow means exceeding the maximum number of bits of the timer!


1.10.5. Timer Programming Practice 1

1.10.5.1 Experimental tasks

The delay function is used to implement the flashing delay. During the delay process, the CPU has to be used here and cannot do other things. This is the disadvantage of the previous


Interrupt + Timing:

The CPU can do the main task (static digital tube displays 0-F), the timer is set to 500ms, and an interrupt is generated when the timer expires. In the interrupt handler isr, the LED is made to flash! !


1.10.5.2 How to program

1. Ideas:

(1) Timing (timer initialization)


Assign a value to TMOD to determine the operating mode of T0.

Calculate the initial value of the timer count and write it into TH0 and TL0

Open the timer interrupt and assign values ​​to ET0 and EA

Set TR0 to start the timer.

(2) What should the main program do?

(3) Interrupt Handler


You can modify it directly in the key interrupt program in the previous lesson! [MCU Learning] Lesson 9: Key


2. Question to think about: How to calculate 0.5s?

     The machine cycle is the time required for the CPU to complete a basic operation. Machine cycle = 1/MCU clock frequency.


Our microcontroller's external crystal oscillator is 12MHz and works in 12T mode, so the internal clock frequency is 1MHz and the machine cycle is 1us. A maximum of 65535 (in 16-bit timer working mode) pulses can be set, which means the maximum timing time is 65535*1us=65535us=65.535ms.

If you need to set a longer time (for example, 500ms), the timer cannot meet this requirement directly. The solution is to add up multiple timings to form a long time.

    Timing method: first use the timer to set 50ms, then *10

    What is the initial value of our timing 50ms?


12T mode, external crystal oscillator 12MHz, internal clock frequency 1MHz, internal machine cycle: 1us

The number of timings is: 50ms/1us=50000

Initial value: Start counting from 65535-50000+1 (because the counter actually overflows when it counts to 65536) = 15536 ​​= 0x3CB0, and the initial value is equal to 0x3CB0

TL0 = 0xB0, TH0 = 0x3C (You can also understand it this way: TH0 = 15536/256, TL0 = 15536%256)

Let’s look at these two pictures together to analyze how we need to program!


MCU works at 12T, no choice

We use timer 0

Working mode configuration


Because it is an internal clock count, it is equivalent to the timer mode, GATE = 0

Because it is timer mode, C/T = 0

Because the 16-bit timer mode is selected, M1 = 0, M0 = 1

Combining the above three points: TMOD = 0x01

Set the timer initial value (note the manual, 16-bit timer mode, TH0 and TL0 need to be reloaded)


TH0 = 0x3C

TL0 = 0xB0

Interrupt Enable


ET0 = 1

EA = 1

Timer/Counter Control Configuration


Enable counting, TR0 = 1

No external interruption involved, no need for IT and IE

IF will automatically change when the count overflows, so don't worry about it.

Timer Interrupt Handler Reference Manual P141


The final procedure is as follows


#include

 

/**************Code description**************************

This program uses the timer interrupt method to realize the static digital tube display 0-F and LED1 flashes once every 0.5s.

P0--Static digital tube

P1.0 LED1

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

 

sbit led = P1^0; //LED is connected to P1.0

 

unsigned char CNT = 10; //Count 50ms each time, repeat 10 times

unsigned char  count;

 

//Segment code table of independent digital tube

unsigned char val[16] = 

{

0xc0, 0xf9, 0xa4, 0xb0, 

0x99, 0x92, 0x82, 0xf8, 

0x80, 0x90, 0x88, 0x83,

0xc6, 0xa1, 0x86, 0x8e

};

 

/*************Delay function*************/

void delay()

{

unsigned char i = 200,j = 100;

while(i--)

while(j--);

}

 

/******************************Timer interrupt service routine*************************/

void tm0_isr() interrupt 1 using 1

{

TH0 = 0x3C;

TL0 = 0xB0; //Manual reload

if (count--== 0)

{

// It means that the interruption has been 10 times and 0.5s has expired

led = !led; // LED inverted

count = CNT;

}

}

 

/***************************Main function************************************/

void main(void)

{

unsigned char i = 0;

/************Timer initialization*************/

TMOD = 0x01;

TH0 = 0x3C;

TL0 = 0xB0;

ET0 = 1;

EA = 1;

TR0 = 1;

led = 1;

count = CNT; 

while (1)

{

//main mission

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

{

P0 = choices[i];

delay();

}

}

}


1.10.6.3, Use of 51 Timer Widget

[1] [2]
Reference address:[STC MCU Learning] Lesson 10: Timers and Counters of MCU

Previous article:Application of STC15F2KA60S2 chip dust sensor
Next article:[STC MCU Learning] Lesson 17: LCD1602 Display

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

Issues to be aware of when using PIC microcontroller TIMER1
1. When TMR1H and TMR1L are initialized, the prescaler will be automatically cleared. 2. When the register is written to TMR1H and TMR1L, the prescaler will be cleared. When TMR1 is in operation, writing to TMR1H or TMR1L value may write undesired values. 3. When TMR1 works in asynchronous counting mode, it cannot b
[Microcontroller]
PIC16F877A microcontroller (interrupt and timer Timer0)
1 Basic principles 2 Implementation Code The code is mainly written based on FIGURE 5-1 and the logic block diagram of the interrupt, so that the code is highly readable and easy to understand. However, some registers may not be explained in the block diagram, so you also need to carefully read the official docu
[Microcontroller]
PIC16F877A microcontroller (interrupt and timer Timer0)
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号