[综合信息] 【华大测评】+矩阵键盘

[复制链接]
 楼主| Cjy_JDxy 发表于 2020-6-7 11:11 | 显示全部楼层 |阅读模式
原理图:
3.jpg
程序:
  1. /*******************************************************************************
  2. * Include files
  3. ******************************************************************************/
  4. #include "hc32_ddl.h"

  5. /*******************************************************************************
  6. * Local type definitions ('typedef')
  7. ******************************************************************************/

  8. /*******************************************************************************
  9. * Local pre-processor symbols/macros ('#define')
  10. ******************************************************************************/
  11. /* KEYOUT port, pin definition */
  12. #define  KEYOUT_PORT        (PortA)
  13. #define  KEYOUT0_PIN        (Pin04)
  14. #define  KEYOUT1_PIN        (Pin05)
  15. #define  KEYOUT2_PIN        (Pin06)

  16. /* KEYIN port, pin definition */
  17. #define  KEYIN_PORT         (PortD)
  18. #define  KEYIN0_PIN         (Pin12)
  19. #define  KEYIN1_PIN         (Pin13)
  20. #define  KEYIN2_PIN         (Pin14)


  21. /* LED0 Port/Pin definition */
  22. #define  LED0_PORT          (PortE)
  23. #define  LED0_PIN           (Pin06)

  24. /* LED1 Port/Pin definition */
  25. #define  LED1_PORT          (PortA)
  26. #define  LED1_PIN           (Pin07)

  27. /* LED2 Port/Pin definition */
  28. #define  LED2_PORT          (PortB)
  29. #define  LED2_PIN           (Pin05)

  30. /* LED3 Port/Pin definition */
  31. #define  LED3_PORT          (PortB)
  32. #define  LED3_PIN           (Pin09)

  33. /* LED0~3 toggle definition */
  34. #define  LED0_TOGGLE()      (PORT_Toggle(LED0_PORT, LED0_PIN))
  35. #define  LED1_TOGGLE()      (PORT_Toggle(LED1_PORT, LED1_PIN))
  36. #define  LED2_TOGGLE()      (PORT_Toggle(LED2_PORT, LED2_PIN))
  37. #define  LED3_TOGGLE()      (PORT_Toggle(LED3_PORT, LED3_PIN))

  38. /* uncomment this line if wants to print information to Terminal I/O window */
  39. #define  __PRINT_TO_TERMINAL

  40. /*******************************************************************************
  41. * Global variable definitions (declared in header file with 'extern')
  42. ******************************************************************************/

  43. /*******************************************************************************
  44. * Local function prototypes ('static')
  45. ******************************************************************************/

  46. /*******************************************************************************
  47. * Local variable definitions ('static')
  48. ******************************************************************************/

  49. /*******************************************************************************
  50. * Function implementation - global ('extern') and local ('static')
  51. ******************************************************************************/

  52. /**
  53. *******************************************************************************
  54. ** \brief ExtInt12 as key row 0 callback function
  55. **
  56. ** \param  None
  57. **
  58. ** \retval None
  59. **
  60. ******************************************************************************/
  61. unsigned int keynum;

  62. /**
  63. *******************************************************************************
  64. ** \brief ExtInt13 as key row 1 callback function
  65. **
  66. ** \param  None
  67. **
  68. ** \retval None
  69. **
  70. ******************************************************************************/
  71. void delay_ms(unsigned int k)
  72. {
  73.         unsigned int i,j;
  74.         for(i=0;i<k;i++)
  75.                 for(j=0;j<300;j++);
  76. }

  77. /**
  78. *******************************************************************************
  79. ** \brief LED 0~3 init function
  80. **
  81. ** \param  None
  82. **
  83. ** \retval None
  84. **
  85. ******************************************************************************/
  86. void Led_Init(void)
  87. {
  88.     stc_port_init_t stcPortInit;

  89.     /* configuration structure initialization */
  90.     MEM_ZERO_STRUCT(stcPortInit);

  91.     stcPortInit.enPinMode = Pin_Mode_Out;

  92.     /* LED0 Port/Pin initialization */
  93.     PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);

  94.     /* LED1 Port/Pin initialization */
  95.     PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);

  96.     /* LED2 Port/Pin initialization */
  97.     PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);

  98.     /* LED3 Port/Pin initialization */
  99.     PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
  100. }

  101. void Key_Init(void)
  102. {
  103.     stc_port_init_t stcPortInit;

  104.     /* configuration structure initialization */
  105.     MEM_ZERO_STRUCT(stcPortInit);

  106.     stcPortInit.enPinMode = Pin_Mode_Out;

  107.     /* LED0 Port/Pin initialization */
  108.     PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);

  109.     /* LED1 Port/Pin initialization */
  110.     PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);

  111.     /* LED2 Port/Pin initialization */
  112.     PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);

  113.                 stcPortInit.enPinMode = Pin_Mode_In;
  114.                 /* enable internal pull-up */
  115.     stcPortInit.enPullUp = Enable;
  116.     /* LED0 Port/Pin initialization */
  117.     PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);

  118.     /* LED1 Port/Pin initialization */
  119.     PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);

  120.     /* LED2 Port/Pin initialization */
  121.     PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
  122. }


  123. void Hang_Output(void)
  124. {
  125.                 stc_port_init_t stcPortInit;

  126.     /* configuration structure initialization */
  127.     MEM_ZERO_STRUCT(stcPortInit);

  128.     stcPortInit.enPinMode = Pin_Mode_Out;

  129.    
  130.     /* LED0 Port/Pin initialization */
  131.     PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);

  132.     /* LED1 Port/Pin initialization */
  133.     PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);

  134.     /* LED2 Port/Pin initialization */
  135.     PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
  136. }

  137. void Hang_Input(void)
  138. {
  139.                 stc_port_init_t stcPortInit;

  140.     /* configuration structure initialization */
  141.     MEM_ZERO_STRUCT(stcPortInit);

  142.    

  143.                 stcPortInit.enPinMode = Pin_Mode_In;
  144.                 /* enable internal pull-up */
  145.     stcPortInit.enPullUp = Enable;
  146.     /* LED0 Port/Pin initialization */
  147.     PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);

  148.     /* LED1 Port/Pin initialization */
  149.     PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);

  150.     /* LED2 Port/Pin initialization */
  151.     PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
  152. }

  153. void Lie_Output(void)
  154. {
  155.                 stc_port_init_t stcPortInit;

  156.     /* configuration structure initialization */
  157.     MEM_ZERO_STRUCT(stcPortInit);

  158.     stcPortInit.enPinMode = Pin_Mode_Out;

  159.     /* LED0 Port/Pin initialization */
  160.     PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);

  161.     /* LED1 Port/Pin initialization */
  162.     PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);

  163.     /* LED2 Port/Pin initialization */
  164.     PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);

  165.                
  166. }

  167. void Lie_Input(void)
  168. {
  169.                 stc_port_init_t stcPortInit;

  170.     /* configuration structure initialization */
  171.     MEM_ZERO_STRUCT(stcPortInit);

  172.     stcPortInit.enPinMode = Pin_Mode_In;
  173.                 /* enable internal pull-up */
  174.     stcPortInit.enPullUp = Enable;

  175.     /* LED0 Port/Pin initialization */
  176.     PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);

  177.     /* LED1 Port/Pin initialization */
  178.     PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);

  179.     /* LED2 Port/Pin initialization */
  180.     PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);

  181.                
  182. }

  183. unsigned int GetKey(void)
  184. {
  185.        
  186.         unsigned int keyvalue = 0;
  187.         uint16_t hangvalue=0,lievalue;
  188.        
  189. // 第1回合第1步
  190.         PORT_ResetBits(KEYOUT_PORT, KEYOUT0_PIN);
  191.         PORT_ResetBits(KEYOUT_PORT, KEYOUT1_PIN);
  192.         PORT_ResetBits(KEYOUT_PORT, KEYOUT2_PIN);
  193.        
  194.         hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
  195.        
  196.         if (hangvalue != 0x07)// 从IO口输入,读IO口
  197.         {
  198.         // 读出的不是0x0f说明有按键被按下
  199.         // 第1回合第2步:读出端口从读出值来判断是哪一行
  200.                 delay_ms(10);
  201.                 Hang_Output();
  202.                 Lie_Input();
  203.         // 第一回合中算出行号
  204.                 switch (hangvalue)
  205.                 {
  206.                         case 0x3:
  207.                                        
  208.                                         PORT_ResetBits(KEYIN_PORT, KEYIN0_PIN);
  209.                                         lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
  210.                                         switch(lievalue)
  211.                                         {
  212.                                                 case 0x03:
  213.                                                         keyvalue=1;
  214.                                                         break;
  215.                                                 case 0x05:
  216.                                                         keyvalue=2;
  217.                                                         break;
  218.                                                 case 0x06:
  219.                                                         keyvalue=3;
  220.                                                         break;
  221.                                                
  222.                                         }
  223.                                 break;
  224.                         case 0x5:
  225.                                        
  226.                                         PORT_ResetBits(KEYIN_PORT, KEYIN1_PIN);
  227.                                         lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
  228.                                         switch(lievalue)
  229.                                         {
  230.                                                 case 0x03:
  231.                                                         keyvalue=4;
  232.                                                         break;
  233.                                                 case 0x05:
  234.                                                         keyvalue=5;
  235.                                                         break;
  236.                                                 case 0x06:
  237.                                                         keyvalue=6;
  238.                                                         break;
  239.                                                
  240.                                         }
  241.                                 break;
  242.                         case 0x6:
  243.                                        
  244.                                         PORT_ResetBits(KEYIN_PORT, KEYIN2_PIN);
  245.                                         lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
  246.                                         switch(lievalue)
  247.                                         {
  248.                                                 case 0x03:
  249.                                                         keyvalue=7;
  250.                                                         break;
  251.                                                 case 0x05:
  252.                                                         keyvalue=8;
  253.                                                         break;
  254.                                                 case 0x06:
  255.                                                         keyvalue=9;
  256.                                                         break;
  257.                                                
  258.                                         }
  259.                                 break;
  260.                        
  261.                         default: break;
  262.                        
  263.                 }
  264.                 Hang_Input();
  265.                 Lie_Output();
  266.                 PORT_ResetBits(KEYOUT_PORT, KEYOUT0_PIN);
  267.                 PORT_ResetBits(KEYOUT_PORT, KEYOUT1_PIN);
  268.                 PORT_ResetBits(KEYOUT_PORT, KEYOUT2_PIN);
  269.                 hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
  270.                
  271.                 while (hangvalue != 0x07)
  272.                 {
  273.                         hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
  274.                 }
  275.                 return keyvalue;
  276.         }
  277.         return 0;
  278. }

  279. /**
  280. *******************************************************************************
  281. ** \brief  main function for KEYSCAN function
  282. **
  283. ** \param  None
  284. **
  285. ** \return int32_t Return value, if needed
  286. **
  287. ******************************************************************************/
  288. int32_t main(void)
  289. {
  290.                
  291.     /* LED init */
  292.     Led_Init();

  293. #ifdef __PRINT_TO_TERMINAL
  294.     /* Uart printf port initialize */
  295.     Ddl_UartInit();
  296. #endif

  297.    Key_Init();


  298.    

  299.     while(1)
  300.     {
  301.         keynum=GetKey();
  302.                                 if(keynum!=0)
  303.                                         printf("Key %d is pressed.\n", keynum);
  304.     }
  305. }

  306. /*******************************************************************************
  307. * EOF (not truncated)
  308. ******************************************************************************/
效果图: 1.jpg 2.jpg
工程:
Keyboard.rar (803.44 KB, 下载次数: 27)
中子家 发表于 2020-6-8 09:12 | 显示全部楼层
不错,点个赞
 楼主| Cjy_JDxy 发表于 2020-6-8 09:17 | 显示全部楼层
caizhiwei 发表于 2020-6-27 22:13 | 显示全部楼层
厉害了小哥哥
凌空天馥 发表于 2020-7-29 10:21 | 显示全部楼层
插上USB 无法识别么 搂主 需要驱动吗
 楼主| Cjy_JDxy 发表于 2020-7-30 07:59 | 显示全部楼层
凌空天馥 发表于 2020-7-29 10:21
插上USB 无法识别么 搂主 需要驱动吗

好像不需要吧,我记得没装驱动
yiyigirl2014 发表于 2020-7-30 20:31 | 显示全部楼层
矩阵键盘。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:绿水本无忧因风皱面,青山原不老为雪白头。

553

主题

3530

帖子

20

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