当前位置:网站首页>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 :
边栏推荐
- Stm32hal library upgrades firmware based on flash analog U disk (detailed explanation)
- 2. Hal hardware abstraction layer
- Program process management tool -go Supervisor
- Viewing binary bin files with notepad++ editor
- Android log system
- Driver development based on I2C protocol
- 鸿蒙第四次培训
- Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
- CSRF
- 如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
猜你喜欢
Viewing binary bin files with notepad++ editor
After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
用了这么久线程池,你真的知道如何合理配置线程数吗?
高精度室内定位技术,在智慧工厂安全管理的应用
How should intermediate software designers prepare for the soft test
Kibana~Kibana的安装和配置
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
DS90UB949
Qt+VTK+OCCT读取IGES/STEP模型
Encapsulation attempt of network request framework of retro + kotlin + MVVM
随机推荐
C language log base zlog basic use
[VTK] vtkWindowedSincPolyDataFilter 源码注释解读
C语言二维数组
Asyncio warning deprecationwarning: there is no current event loop
Multi dimensional monitoring: the data base of intelligent monitoring
Google Earth engine (GEE) -- when we use the front and back images to make up for the interpolation effect, what if there is no effect?
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
Ext file system mechanism principle
Programmers' entrepreneurial trap: taking private jobs
C language AES encryption and decryption
FL Studio 20无限试用版水果编曲下载
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
在腾讯云容器服务Node上执行 kubectl
Qt+VTK+OCCT读取IGES/STEP模型
.\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
触摸与屏幕自动旋转调试
CSRF
AMS series - application startup process
一些常用术语
Hal -- writing hardware drivers