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

边栏推荐
- How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
- Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
- POI excel cell wrap
- Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
- Using onvif protocol to operate the device
- 触摸与屏幕自动旋转调试
- 帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
- Intel 13th generation core flagship exposure, single core 5.5ghz
- Linear table sequence table comprehensive application problem p18
- Hal - General
猜你喜欢

ASP.NET-酒店管理系统

基于I2C协议的驱动开发

ASP.NET-酒店管理系統

AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...

MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)

Cadence background color setting

行业唯一!法大大电子合同上榜36氪硬核科技企业

Driver development based on I2C protocol

FL Studio 20无限试用版水果编曲下载

Application of high-precision indoor positioning technology in safety management of smart factory
随机推荐
聊聊Flink框架中的状态管理机制
Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
Matlab extracts numerical data from irregular txt files (simple and practical)
Dynamic programming (interval DP)
Hal -- writing hardware drivers
按键切换:按F1-F12都需要按Fn
One hot code
C language AES encryption and decryption
Oracle 11g single machine cold standby database
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
项目管理精华读书笔记(七)
基于turtlebot3实现SLAM建图及自主导航仿真
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
Reading notes: heart like Bodhi, Cao Dewang
一些常用术语
Kotlin's use of the no Arg compiler plug-in in gradle
Internet socket (non) blocking write/read n bytes
2022 东北四省赛 VP记录/补题
After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials