Principle of MCU timer interrupt

Publisher:落霞与孤鹜Latest update time:2015-11-02 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#define _1231_C_

#include "reg51.h"

 

//sbit OE=P2^3;

unsigned int SystemTime;

void timer0(void) interrupt 1 using 3 //Interrupt code, see the explanation below

{

   TH0 = 0xdb;

   TL0 = 0xff;

//    TF0 = 0;

   SystemTime++;

}

void main()

{

   TMOD &= 0xF0;

   TMOD |= 0x01; //The value of TMOD indicates the timer working mode selection

   TH0 = 0xdb; //Write the initial value, which can determine how long the timing is

   TL0 = 0xff;

//According to the barrel metaphor below, if TH0 = 0x00; TL0 = 0x00; it means filling the barrel with water from the bottom.

//TH0 = 0xdb;TL0 = 0xff; This means that there is already some liquid lead in the barrel.

//TH0 and TL0 represent the height of the liquid lead in the barrel, that is, the barrel can only be filled with water from above the height of the liquid lead.

//TH0 = 0xff; TL0 = 0xff; which means the highest position of the bucket.

   TF0 = 0; //When the count reaches TF0, it is 1, that is, when TH0 = 0xff; TL0 = 0xff; run one more step TF0 = 1;

   TR0 = 1; //Start counting. From this point on, TH0 and TL0 will increase with each step until TH0 = 0xff; TL0 = 0xff;

                //Equivalent to turning on the tap, if TR0=0, TH0 and TL0 remain unchanged

   ET0 = 1; // Enable timer 0 interrupt

   EA=1;   //Open the general interrupt

//The following is an infinite loop. TH0 and TL0 will increase with each step of the program. When TH0 = 0xff; TL0 = 0xff;

//The MCU will exit from the infinite loop and execute the interrupt code, i.e. start running void timer0(void) interrupt 1 using 3{}

//After running the interrupt part of the code, continue to execute the code in the infinite loop.

//Note: When TH0 = 0xff; TL0 = 0xff; runs again, TF0 does not change from 0 to 1. I guess that the interrupt was triggered when TF0=1; and it was reset to zero.

//If ET0 = 1; and EA=1; are commented out, when TH0 = 0xff; TL0 = 0xff; is run again, TF0 will become 1, and the interrupt code will not be executed.

   while(1)

   {

       if ((SystemTime0)<50)       //SystemTime divided by 100, the remainder is less than 50, which is true

       {

          …………;

       }

       else

       {

            …………;

       }

   };

}

Explanation: void Timer0() interrupt 1 using 1

Timer0   is the function name, just randomly chosen

interrupt   xx   using   y

 The xx value following  interrupt   is the interrupt number, which means the interrupt port number to which this function corresponds, usually in 51

0External   interrupt 0   

1Timer   0

2External   interrupt 1

 Timer 1

4Serial   interrupt

In fact, when compiling, the entry address of your function is set to the jump address of the corresponding interrupt.

The y in using    refers to the register group used by this interrupt function. There are generally 4 groups  of registers  r0    r7 in 51, a total of 32. If your terminal function and other programs do not use the same register group, the register group will not be pushed into the stack when entering the interrupt, and it will not be brought out when returning, saving code and time.

Initial value algorithm: The timer generates an interrupt when the total reaches FFFFH! If you want it to count to 10000, should you use FFFF (hexadecimal) minus 10000 (decimal) as the initial value? TH0=-(10000/256); TL0=-(10000%6) is the same as FFFF (hexadecimal) minus 10000 (decimal). Start counting from TH0=-(10000/256); TL0=-(10000%6), and count to 10000. It is the same as using FFFF (hexadecimal) minus 10000 (decimal)!!! It is easier to write, no calculation required!!!

Just look at the original code and the complement code to know. The complement code of a positive number is the corresponding binary number with the sign bit as zero. The complement code of a negative number is the binary number corresponding to its absolute value, inverted bit by bit and then added by one, with the sign bit as one. Unsigned numbers do not consider the sign, so the result is the same as subtracting its absolute value from FFFF.

We have learned how to use commands to delay flashing, but using commands to flash has the disadvantage that the CPU cannot do other work.

In this lesson, we will learn how to use the timer method to make the light blink.

Interrupted understanding.

This involves the application of microcontroller interrupts. In the process of the CPU running according to the instructions step by step (main program), there may be other more urgent things to do (interrupt service program), which requires the CPU to temporarily stop the current program (main program). After completing (interrupt service program), it can continue to run the previous program (main program). It's like you are eating and filling the bucket with water. While eating, the water is full, you have to quickly turn off the faucet or change an empty bucket before coming back to eat.

The timer of the microcontroller is like a bucket. You start it, which means the faucet is turned on; it starts to fill with water; the timer automatically increases by 1 in each machine cycle and finally overflows; the water in the bucket keeps increasing and eventually it is full; when the timer overflows, you have to deal with it; when the bucket is full, you should also deal with it; after processing, the microcontroller can go back to where it just stopped and continue running; the bucket has been processed, and you can continue what you were doing before.

The main program of the microcontroller starts running from 0x0000. Where does the microcontroller service program start running? In 51, there are multiple interrupt service program entries. Entry 0 is external interrupt 0, the address is 0x0003; entry 1 is timer 0, at 0x000B; entry 2 is external interrupt 1, the address is 0x0013, entry 3 is timer 2, the address is 0x001B, and so on. When an interrupt occurs, the program records the current running position, jumps to the corresponding interrupt entry to run the interrupt service program, and after running, jumps back to the original position to continue running.

In C51, you don't need to worry about where the interrupt service routine is placed or how it will jump. You just need to mark a function as an interrupt service function. When the corresponding interrupt occurs, this function will be automatically run.

Please read the relevant 51 hardware books to learn more about the register settings of the timer. You can also learn after the experiment, because the routines have already set them for you.

Please look at the program. The loop in the main program is an infinite loop and does nothing. In actual application, the main program is placed here.

In the timer service function, you need to re-insert the timer value to ensure that each overflow is the time you specify. Here, 0x0006 is inserted, and it takes 0x10000-0x0006 machine cycles to overflow. Converted to decimal, it means an interruption every 65530 machine cycles. The crystal oscillator we simulated is 22118400HZ, and one machine cycle is every 12 clocks. 65530×12/22118400=0.036 seconds. That is, the flashing frequency is about 28HZ.

Because the maximum value of the 51 timer is only 0xffff, it overflows very quickly and cannot produce a longer flashing frequency. In this lesson, we will first observe the frequency of about 28HZ. In the next lesson, we will use the static variable method to make a LED flashing frequency of up to 1 second.

In addition, since the time from the occurrence of an interrupt to the entry of an interrupt is uncertain, it is 3 to 8 machine cycles. We reset the initial value of the timer after entering the interrupt, which will cause a timing error. In other words, it is not precise timing. If you want precise timing, you need to use the timer automatic loading method, that is, when the timer overflows, the hardware logic automatically loads the initial value of the timer, instead of assigning the initial value in the interrupt service program. In this way, precise timing can be achieved, and the error only occurs in the frequency of the crystal oscillator. This is the content of the next one.

Now please study the program carefully, compile it, enter simulation, run it at full speed, and observe the results. We can see that the LED on P10 is flashing quickly.

By the way, please also practice debugging methods such as stop, single step, breakpoint, etc.

A special point is that when using DX516 to run in single-step mode, you may not be able to enter the interrupt service function. This is because the interrupt function may have run past the moment of single-step processing. If you want to debug the interrupt service function in single-step mode, please set a breakpoint in the interrupt service function and then click full speed. After a while, it will stop at the breakpoint and you can continue to run in single-step mode.

Keywords:MCU Reference address:Principle of MCU timer interrupt

Previous article:MSP430 interrupt C function template
Next article:MSP430 MCU ADC Module

Recommended ReadingLatest update time:2024-11-16 16:38

51 Microcontroller Basics Dot Matrix LED8X8
principle: There is something wrong with his diagram. Don’t be misled. For example, if I display an arrow and follow this schematic diagram, then the straight line in the middle is 0xff, indicating that the high level is valid, but according to this diagram, P0 is a low level. It's valid, so I think it's only when t
[Microcontroller]
51 Microcontroller Basics Dot Matrix LED8X8
AT90CAN MCU CAN Communication Module Introduction and Software Programming
1 AT90CAN MCU CAN Controller Features The CAN controller of the AT90CAN microcontroller is compatible with CAN2.0A and CAN2.0B communication protocols. It has a 120-byte mailbox space inside, which consists of 15 MOBs (Message Objects) and CAN DATA BUFFER. MOB is used to describe a complete frame of CAN message
[Microcontroller]
AT90CAN MCU CAN Communication Module Introduction and Software Programming
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
Introduction: Let me first explain that the chip driver of the LCD1602 I am going to talk about below is HD44780. If your LCD1602 driver chip is not HD44780, then the following content is not applicable. This time I will share my LCD1602 programming experience: Let me first explain that the chip driver of the LCD160
[Microcontroller]
C51 MCU Programming Skills: LCD1602 Programming Experience Sharing
MCS-51 single-chip computer instruction system "read-modify-write" mode instructions
The 51 MCU has four 8-bit parallel interfaces. Due to the characteristics of the internal structure, the parallel interface has different capabilities when outputting 0 and 1. When outputting 0, the capability is strong, but when outputting 1, the capability is very poor. For the P0 port, an external pull-up resistor
[Microcontroller]
STC89C52 interrupt system ---- 51 single chip microcomputer core self-study notes
1. Interrupt System 1.1 Concept When the CPU is processing an event A, another event B occurs, requiring the CPU to process it quickly (an interrupt occurs); cup temporarily interrupts the current work and switches to handle event B (interrupt response and interrupt service); After the CPU finishes processing even
[Microcontroller]
STC89C52 interrupt system ---- 51 single chip microcomputer core self-study notes
Design of digital controlled DC voltage regulated power supply based on single chip microcomputer
With the continuous emergence of new power electronic devices and circuit topologies suitable for higher switching frequencies, traditional application technologies have minimized the impact of switching power supply performance due to the limitations of power device performance. New power supply circuit topologies
[Power Management]
Design of digital controlled DC voltage regulated power supply based on single chip microcomputer
Use PIC low-end microcontroller to simulate serial communication.
Serial communication generally requires a microcontroller with a serial communication interface, but such microcontrollers are generally expensive. Can ordinary microcontrollers be used for serial communication? The answer is yes, but it is usually used to send data, and receiving data is relatively troublesome. I w
[Microcontroller]
Design of 32-bit MCU development board based on STM32F100VBT6
  The STM32F100VBT6 uses the ARM Cortex-M3 32-bit RISC core, operates at a frequency of 24MHz, integrates high-speed embedded memory (up to 128kB flash memory, up to 8kB SRAM) and various enhanced peripherals and I/O connected to two APB buses. All devices provide two I2C, two SPI, one HDMI CEC and up to 3 USART Peuge
[Microcontroller]
Design of 32-bit MCU development board based on STM32F100VBT6
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号