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

边栏推荐
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- 项目管理精华读书笔记(六)
- Encapsulation attempt of network request framework of retro + kotlin + MVVM
- P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
- 图解网络:什么是虚拟路由器冗余协议 VRRP?
- Nestjs配置服务,配置Cookie和Session
- MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
- C语言二维数组
- Function details of CorelDRAW graphics suite 2022
- Qt+VTK+OCCT读取IGES/STEP模型
猜你喜欢

软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。

DS90UB949

MATLAB提取不规则txt文件中的数值数据(简单且实用)

LeetCode 46:全排列

Analysis of EPS electric steering system

高精度室内定位技术,在智慧工厂安全管理的应用

软考中级软件设计师该怎么备考

How to clean up v$rman_ backup_ job_ Details view reports error ora-02030

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

Matlab extracts numerical data from irregular txt files (simple and practical)
随机推荐
How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)
LeetCode 46:全排列
聊聊Flink框架中的状态管理机制
Asyncio warning deprecationwarning: there is no current event loop
phpcms 提示信息頁面跳轉showmessage
Machine learning 3.2 decision tree model learning notes (to be supplemented)
[vtk] interpretation of source code comments of vtkwindowedsincpolydatafilter
AMS Series 1 - AMS startup process
C语言 AES加解密
大厂技术专家:工程师如何提升沟通能力?
JGG专刊征稿:时空组学
帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
触摸与屏幕自动旋转调试
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func mai
Oracle 11g single machine cold standby database
Analysis of JMM memory model
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
ASP. Net hotel management system
ORACLE 11G 单机冷备数据库