[综合信息] GPIO键盘扫描

[复制链接]
 楼主| huahuagg 发表于 2022-12-22 21:44 | 显示全部楼层 |阅读模式
  1. /*******************************************************************************
  2. * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software component is licensed by HDSC under BSD 3-Clause license
  5. * (the "License"); You may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. *                    opensource.org/licenses/BSD-3-Clause
  8. */
  9. /******************************************************************************/
  10. /** \file main.c
  11. **
  12. ** \brief The example for KEYSCAN function demonstration
  13. **
  14. **   - 2018-10-24 CDT First version for sample of keyscan module.
  15. **
  16. ******************************************************************************/

  17. /*******************************************************************************
  18. * Include files
  19. ******************************************************************************/
  20. #include "hc32_ddl.h"

  21. /*******************************************************************************
  22. * Local type definitions ('typedef')
  23. ******************************************************************************/

  24. /*******************************************************************************
  25. * Local pre-processor symbols/macros ('#define')
  26. ******************************************************************************/
  27. /* KEYOUT port, pin definition */
  28. #define  KEYOUT_PORT        (PortA)
  29. #define  KEYOUT0_PIN        (Pin04)
  30. #define  KEYOUT1_PIN        (Pin05)
  31. #define  KEYOUT2_PIN        (Pin06)

  32. /* KEYIN port, pin definition */
  33. #define  KEYIN_PORT         (PortD)
  34. #define  KEYIN0_PIN         (Pin12)
  35. #define  KEYIN1_PIN         (Pin13)
  36. #define  KEYIN2_PIN         (Pin14)

  37. /*******************************************************************************
  38. * Global variable definitions (declared in header file with 'extern')
  39. ******************************************************************************/

  40. /*******************************************************************************
  41. * Local function prototypes ('static')
  42. ******************************************************************************/

  43. /*******************************************************************************
  44. * Local variable definitions ('static')
  45. ******************************************************************************/

  46. /*******************************************************************************
  47. * Function implementation - global ('extern') and local ('static')
  48. ******************************************************************************/

  49. /**
  50. *******************************************************************************
  51. ** \brief ExtInt12 as key row 0 callback function
  52. **
  53. ** \param  None
  54. **
  55. ** \retval None
  56. **
  57. ******************************************************************************/
  58. void KeyRow0_Callback(void)
  59. {
  60.     if (Set == EXINT_Irq**Get(ExtiCh12))
  61.     {
  62.         BSP_LED_Toggle(LED_RED);

  63.         DDL_Printf("Key row 0, col %d is pressed.\n", KEYSCAN_GetColIdx());
  64.         /* wait key released */
  65.         while (Reset == PORT_GetBit(KEYIN_PORT, KEYIN0_PIN));
  66.         /* clear int request flag */
  67.         EXINT_Irq**Clr(ExtiCh12);
  68.     }
  69. }

  70. /**
  71. *******************************************************************************
  72. ** \brief ExtInt13 as key row 1 callback function
  73. **
  74. ** \param  None
  75. **
  76. ** \retval None
  77. **
  78. ******************************************************************************/
  79. void KeyRow1_Callback(void)
  80. {
  81.     if (Set == EXINT_Irq**Get(ExtiCh13))
  82.     {
  83.         BSP_LED_Toggle(LED_GREEN);

  84.         DDL_Printf("Key row 1, col %d is pressed.\n", KEYSCAN_GetColIdx());
  85.         /* wait key released */
  86.         while (Reset == PORT_GetBit(KEYIN_PORT, KEYIN1_PIN));
  87.         /* clear int request flag */
  88.         EXINT_Irq**Clr(ExtiCh13);
  89.     }
  90. }

  91. /**
  92. *******************************************************************************
  93. ** \brief ExtInt14 as key row 2 callback function
  94. **
  95. ** \param  None
  96. **
  97. ** \retval None
  98. **
  99. ******************************************************************************/
  100. void KeyRow2_Callback(void)
  101. {
  102.     if (Set == EXINT_Irq**Get(ExtiCh14))
  103.     {
  104.         BSP_LED_Toggle(LED_YELLOW);

  105.         DDL_Printf("Key row 2, col %d is pressed.\n", KEYSCAN_GetColIdx());
  106.         /* wait key released */
  107.         while (Reset == PORT_GetBit(KEYIN_PORT, KEYIN2_PIN));
  108.         /* clear int request flag */
  109.         EXINT_Irq**Clr(ExtiCh14);
  110.     }
  111. }

  112. /**
  113. *******************************************************************************
  114. ** \brief Key row 0 init function
  115. **
  116. ** \param  None
  117. **
  118. ** \retval None
  119. **
  120. ******************************************************************************/
  121. void KeyRow0_Init(void)
  122. {
  123.     stc_exint_config_t stcExtiConfig;
  124.     stc_irq_regi_conf_t stcIrqRegiConf;
  125.     stc_port_init_t stcPortInit;

  126.     /* configuration structure initialization */
  127.     MEM_ZERO_STRUCT(stcExtiConfig);
  128.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  129.     MEM_ZERO_STRUCT(stcPortInit);

  130.     /**************************************************************************/
  131.     /* External Int Ch.12 for keyin0                                          */
  132.     /**************************************************************************/
  133.     stcExtiConfig.enExitCh = ExtiCh12;

  134.     /* Filter setting */
  135.     stcExtiConfig.enFilterEn = Enable;
  136.     stcExtiConfig.enFltClk = Pclk3Div8;
  137.     /* falling edge for keyscan function */
  138.     stcExtiConfig.enExtiLvl = ExIntFallingEdge;
  139.     EXINT_Init(&stcExtiConfig);

  140.     /* Set PD12 as External Int Ch.12 input */
  141.     MEM_ZERO_STRUCT(stcPortInit);
  142.     stcPortInit.enExInt = Enable;
  143.     stcPortInit.enPullUp = Enable;
  144.     PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);

  145.     /* Select External Int Ch.12 */
  146.     stcIrqRegiConf.enIntSrc = INT_PORT_EIRQ12;

  147.     /* Register External Int to Vect.No.000 */
  148.     stcIrqRegiConf.enIRQn = Int000_IRQn;

  149.     /* Callback function */
  150.     stcIrqRegiConf.pfnCallback = &KeyRow0_Callback;

  151.     /* Registration IRQ */
  152.     enIrqRegistration(&stcIrqRegiConf);

  153.     /* Clear pending */
  154.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);

  155.     /* Set priority */
  156.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);

  157.     /* Enable NVIC */
  158.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
  159. }

  160. /**
  161. *******************************************************************************
  162. ** \brief Key row 1 init function
  163. **
  164. ** \param  None
  165. **
  166. ** \retval None
  167. **
  168. ******************************************************************************/
  169. void KeyRow1_Init(void)
  170. {
  171.     stc_exint_config_t stcExtiConfig;
  172.     stc_irq_regi_conf_t stcIrqRegiConf;
  173.     stc_port_init_t stcPortInit;

  174.     /* configuration structure initialization */
  175.     MEM_ZERO_STRUCT(stcExtiConfig);
  176.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  177.     MEM_ZERO_STRUCT(stcPortInit);

  178.     /**************************************************************************/
  179.     /* External Int Ch.13 for keyin1                                          */
  180.     /**************************************************************************/
  181.     stcExtiConfig.enExitCh = ExtiCh13;

  182.     /* Filter setting */
  183.     stcExtiConfig.enFilterEn = Enable;
  184.     stcExtiConfig.enFltClk = Pclk3Div8;
  185.     /* falling edge for keyscan function */
  186.     stcExtiConfig.enExtiLvl = ExIntFallingEdge;
  187.     EXINT_Init(&stcExtiConfig);

  188.     /* Set PD13 as External Int Ch.13 input */
  189.     MEM_ZERO_STRUCT(stcPortInit);
  190.     stcPortInit.enExInt = Enable;
  191.     stcPortInit.enPullUp = Enable;
  192.     PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);

  193.     /* Select External Int Ch.13 */
  194.     stcIrqRegiConf.enIntSrc = INT_PORT_EIRQ13;

  195.     /* Register External Int to Vect.No.001 */
  196.     stcIrqRegiConf.enIRQn = Int001_IRQn;

  197.     /* Callback function */
  198.     stcIrqRegiConf.pfnCallback = &KeyRow1_Callback;

  199.     /* Registration IRQ */
  200.     enIrqRegistration(&stcIrqRegiConf);

  201.     /* Clear Pending */
  202.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);

  203.     /* Set priority */
  204.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);

  205.     /* Enable NVIC */
  206.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
  207. }

  208. /**
  209. *******************************************************************************
  210. ** \brief Key row 2 init function
  211. **
  212. ** \param  None
  213. **
  214. ** \retval None
  215. **
  216. ******************************************************************************/
  217. void KeyRow2_Init(void)
  218. {
  219.     stc_exint_config_t stcExtiConfig;
  220.     stc_irq_regi_conf_t stcIrqRegiConf;
  221.     stc_port_init_t stcPortInit;

  222.     /* configuration structure initialization */
  223.     MEM_ZERO_STRUCT(stcExtiConfig);
  224.     MEM_ZERO_STRUCT(stcIrqRegiConf);
  225.     MEM_ZERO_STRUCT(stcPortInit);

  226.     /**************************************************************************/
  227.     /* External Int Ch.14 for keyin2                                          */
  228.     /**************************************************************************/
  229.     stcExtiConfig.enExitCh = ExtiCh14;

  230.     /* Filter setting */
  231.     stcExtiConfig.enFilterEn = Enable;
  232.     stcExtiConfig.enFltClk = Pclk3Div8;
  233.     /* falling edge for keyscan function */
  234.     stcExtiConfig.enExtiLvl = ExIntFallingEdge;
  235.     EXINT_Init(&stcExtiConfig);

  236.     /* Set PD14 as External Int Ch.14 input */
  237.     MEM_ZERO_STRUCT(stcPortInit);
  238.     stcPortInit.enExInt = Enable;
  239.     stcPortInit.enPullUp = Enable;
  240.     PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);

  241.     /* Select External Int Ch.14 */
  242.     stcIrqRegiConf.enIntSrc = INT_PORT_EIRQ14;

  243.     /* Register External Int to Vect.No.002 */
  244.     stcIrqRegiConf.enIRQn = Int002_IRQn;

  245.     /* Callback function */
  246.     stcIrqRegiConf.pfnCallback = &KeyRow2_Callback;

  247.     /* Registration IRQ */
  248.     enIrqRegistration(&stcIrqRegiConf);

  249.     /* Clear Pending */
  250.     NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn);

  251.     /* Set priority */
  252.     NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);

  253.     /* Enable NVIC */
  254.     NVIC_EnableIRQ(stcIrqRegiConf.enIRQn);
  255. }

  256. /**
  257. *******************************************************************************
  258. ** \brief Key column init function
  259. **
  260. ** \param  None
  261. **
  262. ** \retval None
  263. **
  264. ******************************************************************************/
  265. void KeyCol_Init(void)
  266. {
  267.     stc_port_init_t stcPortInit;
  268.     stc_keyscan_config_t stcKeyscanConfig;

  269.     /* configuration structure initialization */
  270.     MEM_ZERO_STRUCT(stcPortInit);

  271.     /* enable internal pull-up */
  272.     //stcPortInit.enPullUp = Enable;
  273.     PORT_Init(KEYOUT_PORT, (KEYOUT0_PIN | KEYOUT1_PIN | KEYOUT2_PIN), &stcPortInit);

  274.     /* Set corresponding pins to KEYSCAN function */
  275.     PORT_SetFunc(KEYOUT_PORT, (KEYOUT0_PIN | KEYOUT1_PIN | KEYOUT2_PIN), Func_Key, Disable);

  276.     /* enable KEYSCAN module source clock */
  277.     PWC_Fcg0PeriphClockCmd(PWC_FCG0_PERIPH_KEY, Enable);

  278.     /* output Hiz state 512 clocks*/
  279.     stcKeyscanConfig.enHizCycle = Hiz512;

  280.     /* output Low state 512 clocks */
  281.     stcKeyscanConfig.enLowCycle = Low512;

  282.     /* use HCLK as scan clock */
  283.     stcKeyscanConfig.enKeyscanClk = KeyscanHclk;

  284.     /* KEYOUT 0~3 are selected as column */
  285.     stcKeyscanConfig.enKeyoutSel = Keyout0To2;

  286.     /* KEYIN 12~14 are selected as row */
  287.     stcKeyscanConfig.u16KeyinSel = (Keyin12 | Keyin13 | Keyin14);

  288.     KEYSCAN_Init(&stcKeyscanConfig);
  289. }

  290. /**
  291. *******************************************************************************
  292. ** \brief  main function for KEYSCAN function
  293. **
  294. ** \param  None
  295. **
  296. ** \return int32_t Return value, if needed
  297. **
  298. ******************************************************************************/
  299. int32_t main(void)
  300. {
  301.     /* LED init */
  302.     BSP_LED_Init();

  303.     /* Uart printf port initialize */
  304.     DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_PortInit);

  305.     /* Key row 0~2 init */
  306.     KeyRow0_Init();
  307.     KeyRow1_Init();
  308.     KeyRow2_Init();

  309.     /* Key column 0~2 init */
  310.     KeyCol_Init();

  311.     /* Start KEYSCAN function*/
  312.     KEYSCAN_Start();

  313.     while(1)
  314.     {
  315.         ;
  316.     }
  317. }

  318. /*******************************************************************************
  319. * EOF (not truncated)
  320. ******************************************************************************/


软核硬核 发表于 2023-2-7 14:29 | 显示全部楼层
这是读取多少个引脚的数据0.0
weifeng90 发表于 2023-2-9 07:54 来自手机 | 显示全部楼层
我去,一来就粘贴代码啊。
woai32lala 发表于 2023-2-9 16:10 | 显示全部楼层
学习
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1408

帖子

2

粉丝
快速回复 返回顶部 返回列表