当前位置:网站首页>Modular programming of single chip microcomputer
Modular programming of single chip microcomputer
2022-07-03 11:32:00 【Ch_ champion】
summary
1、 Bronze example :
#include <stdio.h>
enum {
LED1,
LED2,
LED3,
LED_NUM
};
void Func_LED1(unsigned char state);
void Func_LED2(unsigned char state);
void Func_LED3(unsigned char state);
/**
* Function pointer array definition format
* Function return type (* Pointer variable name [ Array size ])( Function parameter list );
* @param state
* @return
*
*/
void (*func[LED_NUM])(unsigned char state) = {Func_LED1, Func_LED2, Func_LED3};
void Func_LED1(unsigned char state)
{
if (state)
printf("LED1 ON\r\n");
else
printf("LED1 OFF\r\n");
}
void Func_LED2(unsigned char state)
{
if (state)
printf("LED2 ON\r\n");
else
printf("LED2 OFF\r\n");
}
void Func_LED3(unsigned char state)
{
if (state)
printf("LED3 ON\r\n");
else
printf("LED3 OFF\r\n");
}
int main() {
// for (int i = 0; i < LED_NUM; ++i) {
// func[i](1);
// }
func[LED1](1);
func[LED2](1);
func[LED3](1);
return 0;
}
Running results :

2、 Silver example :
key.h file
//
// Created by champion on 2021/11/30.
//
#ifndef UNTITLED1_KEY_H
#define UNTITLED1_KEY_H
typedef enum {
KEY1,
KEY2,
KEY_NUM,
}KEY_ID_TYDEF;
typedef enum {
KEY_IDLE, // Free
KEY_PRESS, // Short press
KEY_LONG_PRESS, // Long press
KEY_RELEASE, // Release
}KEY_STATE_TYDEF;
typedef void (*pKeyScanCallBack) (KEY_ID_TYDEF KEY_ID, KEY_STATE_TYDEF KEY_STATE);
void keyScanCBRegister(pKeyScanCallBack pCB);
void keyInit(void);
void keyPoll(void);
#endif; //UNTITLED1_KEY_H
key.c file
//
// Created by champion on 2021/11/30.
//
#include <stdio.h>
#include "key.h"
KEY_ID_TYDEF keyID;
KEY_STATE_TYDEF keyState;
pKeyScanCallBack pkeyScanCB;
void keyInit(void)
{
keyID = 0;
keyState = KEY_IDLE;
pkeyScanCB = NULL;
}
void keyScanCBRegister(pKeyScanCallBack pCB)
{
if (pkeyScanCB == NULL) {
pkeyScanCB = pCB;
}
}
void keyPoll(void)
{
printf("Please Enter key ID: \r\n");
if (scanf("%d", &keyID) == 1) {
printf("\r\n");
printf("Please Enter key state: \r\n");
if (scanf("%d", &keyState) == 1) {
//keyScanHandle(keyID, keyState) This is equivalent to the following sentence , In this way , Encapsulate the hardware layer and the application layer API
if (pkeyScanCB != NULL) {
pkeyScanCB(keyID, keyState);
}
}
}
}main.c file
#include <stdio.h>
#include "key.h"
#include "stdint.h"
typedef struct {
uint8_t value;
void (*run_func)(void);
}FuncTYpedef;
void keyScanHandle(KEY_ID_TYDEF KEY_ID, KEY_STATE_TYDEF KEY_STATE)
{
printf("KEY_ID :%d, KEY_STATE :%d \r\n", KEY_ID, KEY_STATE);
}
void run_fun(void)
{
keyInit();
keyScanCBRegister(keyScanHandle);
keyPoll();
}
int main() {
/**
* Define structure variables
* */
FuncTYpedef init_fun = {
.value = 10,
.run_func = run_fun,
};
init_fun.value = 100;
init_fun.run_func();
/**
* Structure variable pointer variable
*/
// FuncTYpedef *pFun = {
// // pFun->value = 0,
// pFun->run_func = run_fun,
// };
// pFun->value = 200;
// pFun->run_func();
return 0;
}
Running results :

边栏推荐
- 【obs】封装obs实现采集的基础流程
- C language two-dimensional array
- Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
- Linear table sequence table comprehensive application problem p18
- Static library vs shared library
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
- How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
- AMS Series 1 - AMS startup process
- 表空间创建管理及控制文件管理
- [vtk] source code interpretation of vtkpolydatatoimagestencil
猜你喜欢

Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation

Redis things

Matlab extracts numerical data from irregular txt files (simple and practical)

(2) Base

DS90UB949

Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises

C language AES encryption and decryption

C语言 AES加解密

Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche

After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
随机推荐
Qt+VTK+OCCT读取IGES/STEP模型
ASP.NET-酒店管理系統
Understand go language context in one article
Execute kubectl on Tencent cloud container service node
Touch and screen automatic rotation debugging
[OBS] encapsulate the basic process of OBS acquisition
2022 东北四省赛 VP记录/补题
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
C语言二维数组
AIDL
Internet socket (non) blocking write/read n bytes
VPP three-layer network interconnection configuration
Double linked list of linear list
CorelDRAW Graphics Suite 2022新版功能详情介绍
Using onvif protocol to operate the device
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
C语言日志库zlog基本使用
鸿蒙第三次培训(项目实训)
Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
Intel 13th generation core flagship exposure, single core 5.5ghz