MCU Programming Skills-Powerful Clock Interrupt

Publisher:温柔心情Latest update time:2018-11-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In the design of single-chip microcomputer program, setting a good clock interrupt will enable one CPU to play the role of two CPUs, greatly facilitate and simplify the compilation of programs, and improve the efficiency and operability of the system. We can put some routine programs that need to be executed regularly in the clock interrupt, and we can also use the clock interrupt to assist the main program to complete timing, delay and other operations.


The following takes the AT89C51 system with a 6MHz clock as an example to illustrate the application of clock interruption.


Timer initial value and interrupt cycle The clock interrupt does not need to be too frequent, generally 20mS (50Hz) is sufficient. If a time base signal of one hundredth of a second is required, 10mS (100Hz) can be used. Here we take 20mS, and use timer T0 to work in 16-bit timer mode (mode 1). The working mode of T0 is: it automatically adds 1 after each machine cycle. When the count reaches 0FFFFh and is about to overflow, an interrupt will be generated, and the hardware will set the corresponding flag bit for software query. That is, when the interrupt occurs, N+1 machine cycles have passed compared to when it was started. Therefore, we only need to pre-store a number that is N less than the full value 0FFFFh in T0, and then start the timer, and an interrupt will be generated after N machine cycles. This value is the so-called "initial value". The following is the calculation of the initial value we need: the clock is 6MHz, 12 clock cycles are one machine cycle, and there are 10,000 machine cycles in 20mS. (10000)10=(2710)16, then 0FFFFh-2710h+1=0D8F0h. Since it takes 7~8 machine cycles to respond to interrupt, protect the context and reload the initial value, add 7 to this value, that is, the initial value that should be loaded into T0 is 0D8F7h. After each interrupt is entered, the values ​​of A and PSW are pushed into the stack first, and then 0D8F7h is loaded into T0.

Set a unit, add 1 every time an interruption We can take a unit in the internal RAM and name it INCPI (Increase Per Interrupt). In the interruption, after loading the initial value of T0, use the INC INCPI instruction to increase it by 1. From this unit, both the interrupt program and the main program can obtain any integer multiple of 20mS between 1 and 256. For example: there is a program that sends display to the digital tube, which needs to be executed every 0.5 seconds to refresh the display. You can set a unit (called a waiting unit) W_DISP, use the /MOV A,INCPI/ADD A,#25/MOV W_DISP,A/ statement to make it 25 greater than the current INCPI value, and then check whether it is equal to the INCPI value in each interruption. If it is equal, it means that 25 interrupt cycles have passed, then the display sending program will be executed, and W_DISP will be added with 25, waiting for the next 0.5 seconds. We can set multiple waiting units to extract multiple different time base signals. Let the interrupt program query each waiting unit in turn to see if it is equal to INCPI at each interrupt. If they are equal, execute the corresponding processing and reset the value of the waiting unit, otherwise skip it. For example: refresh or flash the display with a 0.5 second signal, generate a real-time clock with a 1 second signal, or output a square wave of a certain frequency, query the input device at a certain interval, etc.


Reading keys in interrupts Usually, we read the keyboard in the main program, the steps are: scan the keyboard, if a key is pressed, delay for tens of milliseconds to debounce, confirm again that the key is indeed pressed, and then process the work corresponding to the key, and repeat the above steps after completion. But there are two shortcomings: 1. The key input cannot be latched when processing the corresponding work, that is, the key may be missed. 2. The CPU cannot do other things during the delay debounce, which is not efficient. If the key reading is put into the clock interrupt, the above shortcomings can be avoided. The method is: if the same key is read as pressed in two adjacent interrupts, the key is valid (the debounce purpose is achieved), and it is latched into the first-in-first-out (queue) keyboard buffer, waiting for the main program to process. In this way, the main program can still respond to keyboard input while processing the key. The buffer depth can usually be set to 8 levels. If the number of latched keys is more than 8, the new key is ignored, and an alarm is issued to remind the user that the new key will be invalid. If the keyboard buffer queue stagnates for much longer than the maximum time required for the main program to process a key press, it means that the main program has made an error or run away. In this case, you can use an instruction to reset the system during an interrupt, thus fulfilling the purpose of a watchdog.


The delay in the main program has a normally open clock interrupt, so when the main program needs a shorter delay with higher precision, the clock interrupt should be temporarily closed. If the program needs a longer delay with lower precision, the following writing method can be used to avoid multi-layer nested loop delays.


Example: Output a high level pulse for 1 second on P1.1


MOV A,INCPI


INC A


CJNE A,INCPI$ ; Wait for an interrupt to complete


SETB P1.1; Set P1.1 to H, pulse starts


ADD A,#50; 50 20mS equals 1 second


CJNE A,INCPI,$ ; Wait for the interruption to increase INCPI by 1 50 times


CLR P1.1; Set P1.1 to L, the pulse ends


Conclusion: From the above, we can see that we should flexibly apply clock interrupts, reasonably allocate tasks to interrupts and main programs, and the two should have clear division of labor and simple interfaces. The skills in this area still need to be explored and experienced in practice. In addition, we should pay attention to: the execution time of the interrupt handler should be shortened as much as possible, and it should not be longer than 20mS.


Keywords:MCU Reference address:MCU Programming Skills-Powerful Clock Interrupt

Previous article:The Three-wire Structure Design of Serial Peripheral Interface Circuit of Single Chip Microcomputer
Next article:Software configuration problem of using the "external counting" TIMx_ETR pulse measurement of the stm8 microcontroller

Recommended ReadingLatest update time:2024-11-16 12:50

C8051F340 microcontroller control design for pesticide application sprayer
The fruit planting industry is an important part of my country's agricultural development, and the prevention and control of fruit tree diseases and insect pests is also receiving more and more attention. At present, the level of pesticide application in my country is generally low, mainly due to problems such as back
[Microcontroller]
C8051F340 microcontroller control design for pesticide application sprayer
How to learn MCU, here are 12 suggestions for you
A microcontroller is an integrated circuit chip. It uses ultra-large-scale integrated circuit technology to integrate a central processing unit (CPU) with data processing capabilities, random access memory (RAM), read-only memory (ROM), multiple I/O ports and interrupt systems, timers/counters and other functions (may
[Microcontroller]
Analyze the difference between ARM and MCU
1. Software This is probably the biggest difference. The operating system was introduced. Why was the operating system introduced? What are the benefits? 1) Convenience. This is mainly reflected in the later development, that is, developing applications directly on the operating system. Unlike a single-chip microc
[Microcontroller]
51 MCU-Register
1. MCU internal resources You should be familiar with the following picture. But if we see this picture, it will feel strange As we are familiar with P0, P1, P2, P3, including TCON in the figure above and IE, SCON in the "#include reg52.h " header file, they are all called registers. We roughly think of these reg
[Microcontroller]
51 MCU-Register
Talking about MCU packaging: Sometimes the product's exterior is just as important
 My young children love playing with gift boxes and wrapping paper, and have as much fun with them as they do with the toys they wrap. I can tell you why. Packaging is fun, especially in the world of microcontrollers. But why? I sat down with our MCU Product Engineering team to discuss my questions - read on to see why
[Microcontroller]
AVR MCU EEPROM Operation
This program simply demonstrates how to use the EERPOM of ATMEGA16      Introduction to EEPROM      Write operation of EEPROM      Read operation of EEPROM In order to simplify the program, various data are not output to the outside. It is recommended to use the JTAG ICE hardware emulator when learning.  After op
[Microcontroller]
A Brief Discussion on the Minimum System of Single Chip Microcomputer
We already have a general idea of ​​what a microcontroller is, so let’s officially start learning about it. Microcontrollers are a very practical course. If you simply learn theoretical knowledge without practicing, it will be difficult to fully master microcontrollers. Although the microcontroller is an intelli
[Microcontroller]
Development of heat meter communication module based on 89C2051 single chip microcomputer
1 Introduction The popularization of centralized heating and the implementation of metered heating are effective means of building energy conservation, but this needs to be achieved through the automation of heating system operation and management. In order to study the control and regulation methods of the
[Microcontroller]
Development of heat meter communication module based on 89C2051 single chip microcomputer
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号