当前位置:网站首页>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 :

边栏推荐
- Understand go language context in one article
- Qt+VTK+OCCT读取IGES/STEP模型
- MATLAB提取不规则txt文件中的数值数据(简单且实用)
- 在腾讯云容器服务Node上执行 kubectl
- phpcms 提示信息页面跳转showmessage
- Some common terms
- Intel 13th generation core flagship exposure, single core 5.5ghz
- CSRF
- Technical experts from large factories: how can engineers improve their communication skills?
- AIDL
猜你喜欢

机器学习 3.2 决策树模型 学习笔记(待补)

Expandablelistview that can expand and shrink (imitating the list page of professional selection of Zhilian recruitment)

基于turtlebot3实现SLAM建图及自主导航仿真

Intel 13th generation core flagship exposure, single core 5.5ghz

How should intermediate software designers prepare for the soft test

Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡

鸿蒙第三次培训(项目实训)

2022 东北四省赛 VP记录/补题

Multi dimensional monitoring: the data base of intelligent monitoring

Processes and threads
随机推荐
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
聊聊Flink框架中的状态管理机制
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
ASP.NET-酒店管理系统
Phpcms prompt message page Jump to showmessage
How PHP solves the problem of high concurrency
How should intermediate software designers prepare for the soft test
[vtk] source code interpretation of vtkpolydatatoimagestencil
Asyncio warning deprecationwarning: there is no current event loop
ORACLE 11G 单机冷备数据库
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
如何成为一名高级数字 IC 设计工程师(1-3)Verilog 编码语法篇:Verilog 行为级、寄存器传输级、门级(抽象级别)
CorelDRAW Graphics Suite 2022新版功能详情介绍
如何成为一名高级数字 IC 设计工程师(1-5)Verilog 编码语法篇:操作数
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
用了这么久线程池,你真的知道如何合理配置线程数吗?
Matlab extracts numerical data from irregular txt files (simple and practical)
AIDL
Spl06-007 air pressure sensor (example of barometer)
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap