MCU Basics (VI): MCU Timing/Controller Control Interface

Publisher:zonhenyLatest update time:2022-05-07 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Control interface of microcontroller timer/controller

5 Steps to Timer/Event Counter Programming

1. Select the working mode according to the requirements, determine the mode control word, and write it into the TMOD register

2. Calculate the count value of the timer/counter, find the initial count value, and write it into the initial value register

3. Open the interrupt option of the timer/counter as needed

4. Set the timer/counter control register TCON and start the timer

5. Wait for the timing time to expire and use query or interrupt mode to handle the situation accordingly


Timer/Counter Application Examples

  The basic idea of ​​using timers to generate periodic actions:


Generate periodic timing

When the scheduled time comes, take corresponding measures

 For example, if the system clock frequency is 12MHz, the timer/counter T0 is required to output a square wave with a period of 500μs from P1.0 (i.e., periodic high and low level switching).


analyze:

  To output a square wave with a period of 500μs from P1.0, it is only necessary to invert P1.0 every 250μs.

  When the system clock is 12MHz, the oscillation frequency is (1/12M)s, that is, (1/12)μs. One machine cycle is 12 oscillation cycles, so one machine cycle is 1μs, that is, one count is 1μs, then the count value is 250, select counting mode 2, and the initial count value is 256 of mode 2 - 250 = 6

  Working mode 2, mode control word 0x02, count initial value 6, TH0 = TL0 = 0x06


Code

Query method: always query the status of TF and handle overflow


/*

*12MHz crystal oscillator makes the P1.0 port output a square wave with a period of 500μs

*/

#include

sbit P1_0 = P1^0;

void main(){

TMOD = 0x02;

TH0 = TL0 = 0x06;

TR0 = 1;

for(;;){

if(TF0){//Query overflow status

TF0 = 0; //Home

P1_0 = ~P1_0; //Processing

}

}

}


Interrupt method: The overflow state triggers an interrupt, and the interrupt service is used to handle the overflow


#include

sbit P1_0 = P1^0;

void main(){

TMOD = 0x02;

TH0 = TL0 = 0x06;

EA = 1; //

ET0 = 1; //Interrupt control, see the interruption chapter of this series of blogs for details

TR0 = 1;

while(1); //Keep the program running

}


//Interrupt service routine

void time0_int(void) interrupt 1 {

P1_0 = !P1_0;

}


Long time timing


  Generally, the crystal frequency is in MHz, and the machine cycle is in μs. Such a short cycle sometimes cannot meet the needs, but we can use programming to achieve longer timing, such as setting a count variable, and adding one to the count variable every 100μs. When the count variable reaches 10000, the timing of 1s is achieved. This can flexibly adapt to the needs

Keywords:MCU Reference address:MCU Basics (VI): MCU Timing/Controller Control Interface

Previous article:Microcontroller Basics (IV): C51 Extensions to C Language
Next article:MCU Basics (VII): Serial Communication Concept and Working Principle

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

Using single chip microcomputer and 24064 LCD to make a simple oscilloscope
I saw the program on the Internet. I happened to use some of the sub-functions in it. I think it is pretty good. I will share it with you for reference. I hope it will be helpful to friends like me. #include iom16v.h #include macros.h #define    BusyCheck                              (PIND&0x40)                   
[Microcontroller]
Design of wireless charger based on MSP430 microcontroller
  At present, the charging of portable electronic devices such as mobile phones, MP3 and laptops mainly adopts the traditional charging method of connecting one end to the AC power supply and the other end to the portable electronic device rechargeable battery. This method has many disadvantages, such as frequent plug
[Microcontroller]
Design of wireless charger based on MSP430 microcontroller
Lesson 1: Preliminary understanding of 8051 microcontroller development
      After talking so much, I guess for novices, they still don’t know what a microcontroller looks like. The following is a physical picture of the microcontroller we commonly use: Figure 1 MCU physical object and pin diagram             Of course, a single-chip microcomputer alone cannot work, so we have
[Microcontroller]
51 single chip microcomputer dynamic digital tube display
First, let's take a look at the circuit schematic on the development board: This development board uses P22, P23, and P24 to control the 3-8 decoder to select the bit of the digital tube, and controls the segment selection of the digital tube through the driver 573 through the P0 port. The enable end of 573 is contr
[Microcontroller]
51 single chip microcomputer dynamic digital tube display
51 MCU implements the universal program framework of the serial port to judge the end of the data to receive a string of data
1. Use proteus to draw a simple circuit diagram for subsequent simulation 2. Programming /******************************************************************************************************************** ---- @Project: USART ---- @File: main.c ---- @Edit: ZHQ ---- @Version: V1.0 ---- @CreationTime: 20
[Microcontroller]
51 MCU implements the universal program framework of the serial port to judge the end of the data to receive a string of data
Design of automatic tuning system based on FPGA and AVR microcontroller
1 Introduction The transmitter is one of the important equipment for wireless communication, and is widely used in the fields of radio and television, mobile communications, marine transportation, and national defense. Automatic control technology is playing an increasingly important role in the field of wireless comm
[Microcontroller]
Design of automatic tuning system based on FPGA and AVR microcontroller
Practice of Common Anti-interference Methods in Single-Chip Microcomputer System Hardware Design
The main factors that affect the reliable and safe operation of the single-chip microcomputer system mainly come from various electrical interferences inside and outside the system, and are affected by the system structure design, component selection, installation, and manufacturing process. These constitute the interf
[Microcontroller]
51 single chip GPIO structure block diagram and working principle
Preface No matter what microcontroller you study, the simplest peripheral is the high and low level control of the IO port. This tutorial will introduce to you how to output high and low levels by operating the GPIO port of the 51 microcontroller on the created project template. 1. GPIO concept GPIO (general purpose
[Microcontroller]
51 single chip GPIO structure block diagram and working principle
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号