当前位置:   article > 正文

单片机模块化编程_单片机模块化编程实例 数组怎么写

单片机模块化编程实例 数组怎么写

概述

        1、青铜示例:

  1. #include <stdio.h>
  2. enum {
  3. LED1,
  4. LED2,
  5. LED3,
  6. LED_NUM
  7. };
  8. void Func_LED1(unsigned char state);
  9. void Func_LED2(unsigned char state);
  10. void Func_LED3(unsigned char state);
  11. /**
  12. * 函数指针数组定义格式
  13. * 函数返回类型 (*指针变量名[数组大小])(函数参数列表);
  14. * @param state
  15. * @return
  16. *
  17. */
  18. void (*func[LED_NUM])(unsigned char state) = {Func_LED1, Func_LED2, Func_LED3};
  19. void Func_LED1(unsigned char state)
  20. {
  21. if (state)
  22. printf("LED1 ON\r\n");
  23. else
  24. printf("LED1 OFF\r\n");
  25. }
  26. void Func_LED2(unsigned char state)
  27. {
  28. if (state)
  29. printf("LED2 ON\r\n");
  30. else
  31. printf("LED2 OFF\r\n");
  32. }
  33. void Func_LED3(unsigned char state)
  34. {
  35. if (state)
  36. printf("LED3 ON\r\n");
  37. else
  38. printf("LED3 OFF\r\n");
  39. }
  40. int main() {
  41. // for (int i = 0; i < LED_NUM; ++i) {
  42. // func[i](1);
  43. // }
  44. func[LED1](1);
  45. func[LED2](1);
  46. func[LED3](1);
  47. return 0;
  48. }

运行结果:

2、白银示例:

key.h文件

  1. //
  2. // Created by champion on 2021/11/30.
  3. //
  4. #ifndef UNTITLED1_KEY_H
  5. #define UNTITLED1_KEY_H
  6. typedef enum {
  7. KEY1,
  8. KEY2,
  9. KEY_NUM,
  10. }KEY_ID_TYDEF;
  11. typedef enum {
  12. KEY_IDLE, //空闲
  13. KEY_PRESS, //短按
  14. KEY_LONG_PRESS, //长按
  15. KEY_RELEASE, //释放
  16. }KEY_STATE_TYDEF;
  17. typedef void (*pKeyScanCallBack) (KEY_ID_TYDEF KEY_ID, KEY_STATE_TYDEF KEY_STATE);
  18. void keyScanCBRegister(pKeyScanCallBack pCB);
  19. void keyInit(void);
  20. void keyPoll(void);
  21. #endif; //UNTITLED1_KEY_H

key.c文件

  1. //
  2. // Created by champion on 2021/11/30.
  3. //
  4. #include <stdio.h>
  5. #include "key.h"
  6. KEY_ID_TYDEF keyID;
  7. KEY_STATE_TYDEF keyState;
  8. pKeyScanCallBack pkeyScanCB;
  9. void keyInit(void)
  10. {
  11. keyID = 0;
  12. keyState = KEY_IDLE;
  13. pkeyScanCB = NULL;
  14. }
  15. void keyScanCBRegister(pKeyScanCallBack pCB)
  16. {
  17. if (pkeyScanCB == NULL) {
  18. pkeyScanCB = pCB;
  19. }
  20. }
  21. void keyPoll(void)
  22. {
  23. printf("Please Enter key ID: \r\n");
  24. if (scanf("%d", &keyID) == 1) {
  25. printf("\r\n");
  26. printf("Please Enter key state: \r\n");
  27. if (scanf("%d", &keyState) == 1) {
  28. //keyScanHandle(keyID, keyState) 这里相当于下面这句话,通过这样的方法,封装好硬件层于应用层的API
  29. if (pkeyScanCB != NULL) {
  30. pkeyScanCB(keyID, keyState);
  31. }
  32. }
  33. }
  34. }

main.c文件

  1. #include <stdio.h>
  2. #include "key.h"
  3. #include "stdint.h"
  4. typedef struct {
  5. uint8_t value;
  6. void (*run_func)(void);
  7. }FuncTYpedef;
  8. void keyScanHandle(KEY_ID_TYDEF KEY_ID, KEY_STATE_TYDEF KEY_STATE)
  9. {
  10. printf("KEY_ID :%d, KEY_STATE :%d \r\n", KEY_ID, KEY_STATE);
  11. }
  12. void run_fun(void)
  13. {
  14. keyInit();
  15. keyScanCBRegister(keyScanHandle);
  16. keyPoll();
  17. }
  18. int main() {
  19. /**
  20. * 定义结构体变量
  21. * */
  22. FuncTYpedef init_fun = {
  23. .value = 10,
  24. .run_func = run_fun,
  25. };
  26. init_fun.value = 100;
  27. init_fun.run_func();
  28. /**
  29. * 结构体变量指针变量
  30. */
  31. // FuncTYpedef *pFun = {
  32. // // pFun->value = 0,
  33. // pFun->run_func = run_fun,
  34. // };
  35. // pFun->value = 200;
  36. // pFun->run_func();
  37. return 0;
  38. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/435992
推荐阅读
相关标签
  

闽ICP备14008679号