Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the callback function in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use the callback function in c language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn how to use callback functions in c language.

First, through this course, you can master the following knowledge:

Master the core ideas or requirements of the program architecture. Master the function of callback function master the programming of callback function master the application of callback function in product

Second, the core concepts and requirements of the program architecture

Many people may say that a good program architecture, ah, is that the code is very compact and efficient.

In fact, this is very one-sided, not entirely right, which can only show that your program algorithm is well written, but the architecture is not necessarily good.

Even if it is the architecture, it naturally focuses on the "overall situation". Thinking can not be limited to the current product functions, but also to consider the increase and tailoring of functions in the future, then for single-chip microcomputer development, I think a good program architecture should at least meet the following requirements:

The program code of the hardware layer and the application layer are separated, the control and communication between each other use interfaces, and will not share global variables or arrays.

Here, this is my request, do not underestimate this requirement, because this requirement contains a lot of knowledge, such as professionally called portability and scalability.

Well, let's imagine the way we usually write single-chip microcomputer code, ah, basically a .c file solution at 51, including register configuration and product functions.

This is a program without architecture, and then when we evolve to STM32, the single-chip microcomputer, the program gets bigger, and we slowly add a few folder directories in the project files to separate the hardware layer from the application layer code.

So we will write some different peripheral functions, such as Led, keys, serial port and other peripheral function codes in different .c files, and then use the function interface to call it.

For example, to control a LED light on, write a function to drive the state of the led light directly in the led.c file and then call it externally.

Well, we see that the control function of this kind of Led really meets the needs of the program architecture. The code of the hardware layer and the application layer are separated, and the application layer is controlled by the interface provided by the hardware layer, and there will not be all the variables or arrays shared by the hardware layer and the application layer. Isn't it easy like this?

Well, I don't know if you have encountered another situation, that is, the application needs to collect data from the hardware layer, such as serial port to receive data, button acquisition, ADC value collection.

How to tell the application layer to take the data of this kind of hardware layer, or how to give it actively?

In the past, our most simple and rude way is to use a global variable, for example, the hardware layer serial port to receive the data, then we throw the data into the array, and then mark the position 1 of the received global variable.

For example, the global variable is named RcvFlag, and then the application layer program polls to determine the RcvFlag==1? If so, start to extract and parse the data in the array.

Many people will say, you see, I can still achieve the function in this way, ah, why do I need to learn other architectures?

Of course, this can achieve functionality, but there will be poor portability.

For example, your boss asks you to package the hardware layer of this serial port for the customer to use, but can not let the customer see the source code of your implementation, only provide the interface (function name) to the other party.

So at this time, do you want to tell the customer to determine which variable is 1 first, and then take the data from which array is so LOW?

Then if you are a knowledgeable customer, you will doubt whether your company's technical strength is the level of primary school students.

So what can be done conveniently and professionally? Here we need to use the callback function.

Third, the function of callback function

Well, before talking about callback functions, I generally have two types of function calls:

1. Output type

I don't know if you have ever used some library functions provided with C language, such as the function of sizeof () to obtain the length of data, and memcpy () is a memory copy function. After we call this function, we can complete the corresponding function.

There are some program functions based on single-chip microcomputer, such as controlling LED to turn on and off, relay suction and disconnection, LCD driver and so on.

Well, these are what I generally call output functions.

We play a leading role in the output function, and we know when to call it.

two。 Input type

The input type is also called a responsive function.

What is a responsive function?

For example, to receive serial data, we do not know when the data will come.

For example, if we press the key to detect the function, we do not know when the key will be pressed, then these will be defined as responsive functions, and responsive functions can be implemented with callback functions.

So through the analysis of these two types, we can know that callback functions are basically used in input processing.

For example, serial port data reception, then the data is input into the single-chip microcomputer, the single-chip microcomputer is in the role of slave.

Press the key to detect, the state of the key is input to the single-chip microcomputer.

For example, ADC value acquisition, ADC value is also input to the single-chip microcomputer.

Then the time nodes they enter are unknown, and these can be handled with callback functions.

We will use the code to give you an example of how to deal with it later.

The callback function is also used to encapsulate the code.

For example, for chip or module manufacturers, we take the typical STM32 as an example. Interrupt functions such as external interrupts, timers, serial ports and other interrupt functions are callback functions. The purpose of this function is to transmit the collected data to the user, or the application layer.

So the core function of the callback function is:

1. A LOW method of passing data from one .c file to another, rather than using global variables to share data.

two。 For this way of data transmission, the callback function is more conducive to code encapsulation.

Fourth, master the programming of callback function

I have mentioned a lot of conceptual things, which may be difficult for everyone to understand. The callback function is ultimately realized by function pointers.

So I'm going to use some examples of simulated keys to demonstrate how to deal with them by adjusting functions.

Here is our c-free project, which is used to simulate convenience points:

From the idea of modular programming, the whole project is divided into two parts, application layer main.c file, hardware layer key.c and key.h file.

No matter how complex the program is, we have to dig down step by step from the main function. The code for the main function is as follows.

Int main (int argc, char * argv []) {KeyInit (); KeyScanCBSRegister (KeyScanHandle); KeyPoll (); return 0;}

KeyInit (); is the key initialization function of the key.c file

KeyScanCBSRegister (KeyScanHandle); is the function pointer registration function of key.c.

This function may be a little confusing, please follow our rhythm, let's start the brain-burning link, which is also a necessary step to write the callback function.

To understand this callback function registration function, we need to start with the function pointer definition of the hardware layer (key.h) header file, as shown in the following figure.

Here we customize a function pointer type with two formal parameters.

Then, we define a function pointer variable in the key.c file.

The point is, we use this function pointer to point to the function address (function name) of the application layer.

How to realize the direction? Is to register the function through the function pointer.

This function is called in the main function, and it is also very flexible to register the function in this way, and you can call it wherever you want to use the keystroke function in any .c file.

Note here that the main.c file defines a function to receive data from the hardware layer (key.c).

The definition here is not random, it must be consistent with the return value and parameter of that custom function pointer type.

Then copy the function name directly to the formal parameter of the KeyScanCBSRegister function.

After this call, the pointer to the pKeyScanCBS of our key.c file actually points to the KeyScanHandle function.

In other words, when you execute pKeyScanCBS, you execute the KeyScanHandle function.

The function that specifically detects the key is the KeyPoll function, which is called in the main function.

When input from the keyboard is detected, pKeyScanCBS is eventually called.

The final execution is the KeyScanHandle function of the main.c file.

So, let's take a look at the output.

If it's still a little vague, let me explain the process of writing and using callback functions:

Custom function pointers, which serve as data to be passed to the application layer in the hardware layer.

The hardware layer defines a function pointer and a function pointer to register functions.

The application layer defines a function that returns values and parameters that are consistent with the function pointer.

The application layer calls the function pointer to register the function and passes in the defined function name as a formal parameter.

Thank you for your reading. the above is the content of "how to use the callback function in c language". After the study of this article, I believe you have a deeper understanding of how to use the callback function in c language. the specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report