- /*******************************************************************************
- * Include files
- ******************************************************************************/
- #include "hc32_ddl.h"
- /*******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /*******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- /* KEYOUT port, pin definition */
- #define KEYOUT_PORT (PortA)
- #define KEYOUT0_PIN (Pin04)
- #define KEYOUT1_PIN (Pin05)
- #define KEYOUT2_PIN (Pin06)
- /* KEYIN port, pin definition */
- #define KEYIN_PORT (PortD)
- #define KEYIN0_PIN (Pin12)
- #define KEYIN1_PIN (Pin13)
- #define KEYIN2_PIN (Pin14)
- /* LED0 Port/Pin definition */
- #define LED0_PORT (PortE)
- #define LED0_PIN (Pin06)
- /* LED1 Port/Pin definition */
- #define LED1_PORT (PortA)
- #define LED1_PIN (Pin07)
- /* LED2 Port/Pin definition */
- #define LED2_PORT (PortB)
- #define LED2_PIN (Pin05)
- /* LED3 Port/Pin definition */
- #define LED3_PORT (PortB)
- #define LED3_PIN (Pin09)
- /* LED0~3 toggle definition */
- #define LED0_TOGGLE() (PORT_Toggle(LED0_PORT, LED0_PIN))
- #define LED1_TOGGLE() (PORT_Toggle(LED1_PORT, LED1_PIN))
- #define LED2_TOGGLE() (PORT_Toggle(LED2_PORT, LED2_PIN))
- #define LED3_TOGGLE() (PORT_Toggle(LED3_PORT, LED3_PIN))
- /* uncomment this line if wants to print information to Terminal I/O window */
- #define __PRINT_TO_TERMINAL
- /*******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /*******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- /*******************************************************************************
- * Local variable definitions ('static')
- ******************************************************************************/
- /*******************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- /**
- *******************************************************************************
- ** \brief ExtInt12 as key row 0 callback function
- **
- ** \param None
- **
- ** \retval None
- **
- ******************************************************************************/
- unsigned int keynum;
- /**
- *******************************************************************************
- ** \brief ExtInt13 as key row 1 callback function
- **
- ** \param None
- **
- ** \retval None
- **
- ******************************************************************************/
- void delay_ms(unsigned int k)
- {
- unsigned int i,j;
- for(i=0;i<k;i++)
- for(j=0;j<300;j++);
- }
- /**
- *******************************************************************************
- ** \brief LED 0~3 init function
- **
- ** \param None
- **
- ** \retval None
- **
- ******************************************************************************/
- void Led_Init(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_Out;
- /* LED0 Port/Pin initialization */
- PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);
- /* LED3 Port/Pin initialization */
- PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
- }
- void Key_Init(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_Out;
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_In;
- /* enable internal pull-up */
- stcPortInit.enPullUp = Enable;
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
- }
- void Hang_Output(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_Out;
-
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
- }
- void Hang_Input(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
-
- stcPortInit.enPinMode = Pin_Mode_In;
- /* enable internal pull-up */
- stcPortInit.enPullUp = Enable;
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYIN_PORT, KEYIN2_PIN, &stcPortInit);
- }
- void Lie_Output(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_Out;
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);
-
- }
- void Lie_Input(void)
- {
- stc_port_init_t stcPortInit;
- /* configuration structure initialization */
- MEM_ZERO_STRUCT(stcPortInit);
- stcPortInit.enPinMode = Pin_Mode_In;
- /* enable internal pull-up */
- stcPortInit.enPullUp = Enable;
- /* LED0 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT0_PIN, &stcPortInit);
- /* LED1 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT1_PIN, &stcPortInit);
- /* LED2 Port/Pin initialization */
- PORT_Init(KEYOUT_PORT, KEYOUT2_PIN, &stcPortInit);
-
- }
- unsigned int GetKey(void)
- {
-
- unsigned int keyvalue = 0;
- uint16_t hangvalue=0,lievalue;
-
- // 第1回合第1步
- PORT_ResetBits(KEYOUT_PORT, KEYOUT0_PIN);
- PORT_ResetBits(KEYOUT_PORT, KEYOUT1_PIN);
- PORT_ResetBits(KEYOUT_PORT, KEYOUT2_PIN);
-
- hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
-
- if (hangvalue != 0x07)// 从IO口输入,读IO口
- {
- // 读出的不是0x0f说明有按键被按下
- // 第1回合第2步:读出端口从读出值来判断是哪一行
- delay_ms(10);
- Hang_Output();
- Lie_Input();
- // 第一回合中算出行号
- switch (hangvalue)
- {
- case 0x3:
-
- PORT_ResetBits(KEYIN_PORT, KEYIN0_PIN);
- lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
- switch(lievalue)
- {
- case 0x03:
- keyvalue=1;
- break;
- case 0x05:
- keyvalue=2;
- break;
- case 0x06:
- keyvalue=3;
- break;
-
- }
- break;
- case 0x5:
-
- PORT_ResetBits(KEYIN_PORT, KEYIN1_PIN);
- lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
- switch(lievalue)
- {
- case 0x03:
- keyvalue=4;
- break;
- case 0x05:
- keyvalue=5;
- break;
- case 0x06:
- keyvalue=6;
- break;
-
- }
- break;
- case 0x6:
-
- PORT_ResetBits(KEYIN_PORT, KEYIN2_PIN);
- lievalue=((PORT_GetData(KEYOUT_PORT)&0x70)>>4);
- switch(lievalue)
- {
- case 0x03:
- keyvalue=7;
- break;
- case 0x05:
- keyvalue=8;
- break;
- case 0x06:
- keyvalue=9;
- break;
-
- }
- break;
-
- default: break;
-
- }
- Hang_Input();
- Lie_Output();
- PORT_ResetBits(KEYOUT_PORT, KEYOUT0_PIN);
- PORT_ResetBits(KEYOUT_PORT, KEYOUT1_PIN);
- PORT_ResetBits(KEYOUT_PORT, KEYOUT2_PIN);
- hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
-
- while (hangvalue != 0x07)
- {
- hangvalue=((PORT_GetData(KEYIN_PORT)&0x7000)>>12);
- }
- return keyvalue;
- }
- return 0;
- }
- /**
- *******************************************************************************
- ** \brief main function for KEYSCAN function
- **
- ** \param None
- **
- ** \return int32_t Return value, if needed
- **
- ******************************************************************************/
- int32_t main(void)
- {
-
- /* LED init */
- Led_Init();
- #ifdef __PRINT_TO_TERMINAL
- /* Uart printf port initialize */
- Ddl_UartInit();
- #endif
- Key_Init();
-
- while(1)
- {
- keynum=GetKey();
- if(keynum!=0)
- printf("Key %d is pressed.\n", keynum);
- }
- }
- /*******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
效果图:
工程:
Keyboard.rar
(803.44 KB, 下载次数: 27)