当前位置:网站首页>Cooperative development in embedded -- function pointer

Cooperative development in embedded -- function pointer

2022-07-06 09:43:00 Dafang teacher embedded

Cooperative development in embedded -- A function pointer

In embedded software development , A project often needs to be completed by multiple people .

If A It is necessary to complete the overall logical function of the project , The whole logic function includes many detailed small functions , but A There is no time or ability to realize these small functions , At this time, we can make B To help realize the functions inside the function .

The usual way of thinking is ,B After writing a function ,A Directly through B The declared function call is ok . But there will be some problems , for example B After writing the function ,A Only use B Declared function name to use , hypothesis B The naming rules of the declared function name are very inconsistent A The taste of ,A It's uncomfortable to use , ha-ha . Then how to do this ? intelligent A Be able to declare a function name you like , And through the function passed by the function pointer to obtain B The function of .

Let's talk about the detailed implementation :

Declared function pointer -A be responsible for

If A You need a function of summation , But he didn't write for long , He can declare a function pointer by himself :

//a Declare a pointer function in , The function inside its function needs another person b To complete  int (*mysum)(int, int)=0;

This looks like a normal function declaration , All have function names , Return value type and parameter type , But the function name is preceded by an asterisk , Indicates that it is a function pointer , In addition, its function entity can be initialized to 0.

Function pointer assignment -A be responsible for

This step is equivalent to function pointer initialization , It is also equivalent to function registration , Will be A Declared function pointer , By means of pointer assignment , To obtain a B Functions implemented , Quite so B Write a layer of functions A Shell of :

// Initialize pointer function , Pass in b The function name of the function written ,// Assign values through function pointers ,main Function declared in , Can use b Written function  int init_mysum_func(int(*func_handle)(int,int)){

    mysum = func_handle;

    return 0;}

Detailed implementation of function -B be responsible for

B This person can only follow A The format of the declared function pointer ( The return value is consistent with the parameter type , Function names can be arbitrary ) Complete the internal functions of the function , Here, take the simple summation as an example , stay b.h In file ,B The functions are as follows :

int sum_by_b(int a, int b){

    return a+b;}

Project collation logic -A be responsible for

The logic of the project is very simple , Is o 1+2 Value , At this time A Using self declared mysum Before , initialization ( register ) Check this function ( take B Write the function “sum_by_b” Put on A Written shell “mysum”), And then you can use it , Examples are as follows :

int main(){

    int res = 0;

    init_mysum_func(sum_by_b);// Initialize pointer function , take b The completed function is endowed with a Declared function

    res = mysum(1,2);// Use a The declared summation function sums , Its internal implementation is actually b Accomplished

    printf("mysum(1,2) = %d",res);

    return 0;}

test result :

mysum(1,2) = 3

--------------------------------

Process exited after 0.007424 seconds with return value 0

Please press any key to continue . . .

attach : The whole test code

b.h

int sum_by_b(int a, int b)

{

    return a+b;

}

a.c

#include #include #include "b.h"//a Declare a pointer function in , The function inside its function needs another person b To complete  int (*mysum)(int, int)=0;

// Initialize pointer function , Pass in b The function name of the function written ,// Assign values through function pointers ,main Function declared in , Can use b Written function  int init_mysum_func(int(*func_handle)(int,int)){

    mysum = func_handle;

    return 0;}

int main(){

    int res = 0;

    init_mysum_func(sum_by_b);// Initialize pointer function , take b The completed function is endowed with a Declared function

    res = mysum(1,2);// Use a The declared summation function sums , Its internal implementation is actually b Accomplished

    printf("mysum(1,2) = %d",res);

    return 0;}

Three embedded development program architectures that must be mastered !

In embedded software development , Including MCU development , Software architecture is a problem that developers must consider carefully . Software architecture is very important for the stability and reliability of the whole system , A suitable software architecture is not only clearly constructed , And easy to develop ?

Xinyingda embedded

The embedded C Advanced use of language ( The most detailed network )

PlayCodes

Some code specifications and styles in embedded development

Pentium's heart is published in those embedded ...

stm32 Example of project template

The surging heart

The first 2 section : In my eyes, there are four stages of learning SCM

Teach you the basics of SCM 2 speak :

The first stage : Learn to C Common grammar of language , be familiar with 51 SCM development platform software keil The operation of , Understand the general principle and characteristics of single chip microcomputer , Can read keys , Nixie tube , Running lights , Simple program of serial port , Be familiar with several common peripheral chip drivers . There are many excellent online tutorials , The serial I'm writing 《 Ten years in business , Teach you the basics of SCM 》 It is also one of these introductory tutorials .

The second stage : I think this stage is the most important of the four stages . Many beginners have completed the first stage of learning , When you really face a small goal , I still haven't started . They don't know the keys , Show , Communications , How applications relate to each other , They don't know how to deal with multi task items in parallel , What they lack most is the framework thinking of the program . There are many program framework ideas shared by enthusiastic people on the Internet , It is worth learning and using for reference . I usually do projects with state machine thinking , Just use switch Statement to achieve multi task switching , Add a timing interrupt to generate different duration timing , Interested friends can see the serial post I wrote on this forum last year 《 I have been employed for nearly ten years , Teach you the MCU program framework hand in hand 》.

The third stage : When you're working on a project , In addition to writing MCU software , It is also inevitable to deal with the hardware of the whole circuit , It is particularly important to master some common hardware circuit knowledge . resistance , capacitance , inductance , diode , triode , Application of lotus root , Relationship between voltage difference and reference ground , The reason why the two systems need to share land when communicating , The essence of isolation and non isolation , Common transformer rectifier circuit , Common peripheral drive circuits, etc . There are many excellent electronic basic tutorials in this regard , You should take the initiative to find information in this regard to further study , I plan to write a serial post about this in a year or two 《 Ten years in business , Explanation of common hardware knowledge of single chip microcomputer 》.

The fourth stage : With the first three stages of active learning and accumulation , You can do the project , Study in the project . According to the needs of work, choose which manufacturer of SCM to study , If PIC,AVR,stm32 Wait for MCU manufacturers ; Decide whether you need to study assembly language according to the needs of your work , There are some single-chip computers made in Taiwan that cannot be used C Language development , You can only use assembly ; According to the needs of the work, deeply study the hardware circuit knowledge required by relevant industries ; Learn relevant peripheral chip drivers according to the needs of work .

extension :

PCB Circuit design PADS_LOGIC Schematic design

STM32 Some important C Language knowledge points , Summary !

Preface a little partner who is a beginner of SCM asked me to recommend C Language books , because C The language foundation is relatively poor , Want to put C Learn the language again , And then to learn SCM , I used to have this idea when I was just learning SCM . Actually C Language can be ?

Embedded hodgepodge

Half a month of learning SCM knowledge summary

One 、 The most fundamental preparation in the early stage 1. A single chip development board  2. A computer  3. The corresponding software  4. Have a certain C Language foundation . I haven't met in my current study C Application of language pointer , So this is right C The language requirement is actually not ?

HouEn

Is it possible to learn SCM in ten days ? How long does it take to get started with SCM ?

A few years ago , I learned SCM tutorial and supporting development board entry SCM through teacher Guo's ten days . Recently, many questions asked by students are , How long does the learning cycle of single chip microcomputer take . Today, let me give you a unified reply . This mainly depends on what you learn ?

原网站

版权声明
本文为[Dafang teacher embedded]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060903026266.html