今天搞了一下矩阵键盘。
行线:PC0,PC1,PC2,PC3。列线:PA4,PA5,PA6,PA7。
原理图:
串口打印键值。
代码:
- #include "main.h"
- /** @addtogroup Examples
- @{
- */
- #define Hang_00_L GPIO_ResetBit(GPIOC,GPIO_PIN_0)
- #define Hang_00_H GPIO_SetBit(GPIOC,GPIO_PIN_0)
-
- #define Hang_01_L GPIO_ResetBit(GPIOC,GPIO_PIN_1)
- #define Hang_01_H GPIO_SetBit(GPIOC,GPIO_PIN_1)
-
- #define Hang_02_L GPIO_ResetBit(GPIOC,GPIO_PIN_2)
- #define Hang_02_H GPIO_SetBit(GPIOC,GPIO_PIN_2)
-
- #define Hang_03_L GPIO_ResetBit(GPIOC,GPIO_PIN_3)
- #define Hang_03_H GPIO_SetBit(GPIOC,GPIO_PIN_3)
-
- #define Lie_00_V GPIO_ReadInputBit(GPIOA,GPIO_PIN_4)
- #define Lie_01_V GPIO_ReadInputBit(GPIOA,GPIO_PIN_5)
- #define Lie_02_V GPIO_ReadInputBit(GPIOA,GPIO_PIN_6)
- #define Lie_03_V GPIO_ReadInputBit(GPIOA,GPIO_PIN_7)
- #define Lie_04_V ((GPIO_ReadInputPort(GPIOA)>>4) & 0x0FU)
- #define Hang_10_L GPIO_ResetBit(GPIOA,GPIO_PIN_4)
- #define Hang_10_H GPIO_SetBit(GPIOA,GPIO_PIN_4)
-
- #define Hang_11_L GPIO_ResetBit(GPIOA,GPIO_PIN_5)
- #define Hang_11_H GPIO_SetBit(GPIOA,GPIO_PIN_5)
-
- #define Hang_12_L GPIO_ResetBit(GPIOA,GPIO_PIN_6)
- #define Hang_12_H GPIO_SetBit(GPIOA,GPIO_PIN_6)
-
- #define Hang_13_L GPIO_ResetBit(GPIOA,GPIO_PIN_7)
- #define Hang_13_H GPIO_SetBit(GPIOA,GPIO_PIN_7)
-
- #define Lie_10_V GPIO_ReadInputBit(GPIOC,GPIO_PIN_0)
- #define Lie_11_V GPIO_ReadInputBit(GPIOC,GPIO_PIN_1)
- #define Lie_12_V GPIO_ReadInputBit(GPIOC,GPIO_PIN_2)
- #define Lie_13_V GPIO_ReadInputBit(GPIOC,GPIO_PIN_3)
- #define Lie_14_V ((GPIO_ReadInputPort(GPIOC)<<4) & 0xF0U)
- #define jpys 20 //??????jpys==20?????????
- uint8_t temp;
- uint8_t txBuffer[50];
- uint8_t rxBuffer[10];
- uint8_t cord_h,cord_l;//???????
- volatile uint32_t nBytesRead = 0;
- //volatile bool txThresholdEventReceived = false;
- //volatile bool rxThresholdEventReceived = false;
- /** @addtogroup USART_Printf
- @{
- */
- /** @addtogroup USART_Printf_Variables Variables
- @{
- */
- volatile uint32_t tick = 0;
- uint8_t txBuf[] = "Key Value = : \r\n";
- /**@} end of group USART_Printf_Variables */
- void DelayMs(uint16_t nms)
- {
- uint16_t i,j;
- for(i=0;i<nms;i++)
- for(j=0;j<200;j++);
- }
- /** @addtogroup USART_Printf_Functions Functions
- @{
- */
- void PORT_Initialize1(void)
- {
- /************************** GROUP 0 Initialization *************************/
- GPIO_Config_T GPIO_ConfigStruct;
- /************************** GROUP 1 Initialization *************************/
- RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOC);
-
- GPIO_ConfigStruct.mode = GPIO_MODE_OUT_PP;
- GPIO_ConfigStruct.pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(GPIOC, &GPIO_ConfigStruct);
- GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
- GPIO_ConfigStruct.pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(GPIOA, &GPIO_ConfigStruct);
- }
- void PORT_Initialize2(void)
- {
- GPIO_Config_T GPIO_ConfigStruct;
- /************************** GROUP 1 Initialization *************************/
- RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOC);
-
- GPIO_ConfigStruct.mode = GPIO_MODE_OUT_PP;
- GPIO_ConfigStruct.pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(GPIOA, &GPIO_ConfigStruct);
- GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
- GPIO_ConfigStruct.pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(GPIOC, &GPIO_ConfigStruct);
- }
- unsigned char KeyScan(void) //????????????????
- {
- uint16_t i,j;
-
- PORT_Initialize1();
- for(i=0;i<1250;i++);
- Hang_00_L;
- Hang_01_L;
- Hang_02_L;
- Hang_03_L;
- for(i=0;i<1250;i++);
- //cord_h=(Lie_04_V&0x0f);
- cord_h=(uint8_t)(Lie_04_V);
- //cord_h=Lie_01_V|Lie_02_V|Lie_03_V|Lie_04_V;
- if(cord_h != 0xf0) //?????????
- {
- DelayMs(10); //??
- cord_h=(uint8_t)(Lie_04_V);
-
- if(cord_h!=0xf0)
- {
- cord_h=(uint8_t)(Lie_04_V); //?????
- for(i=0;i<1250;i++);
- PORT_Initialize2();
- for(i=0;i<1250;i++);
- if((cord_h&0x01)==0x01)
- Hang_10_H;
- else
- Hang_10_L;
- if((cord_h&0x02)==0x02)
- Hang_11_H;
- else
- Hang_11_L;
- if((cord_h&0x04)==0x04)
- Hang_12_H;
- else
- Hang_12_L;
- if((cord_h&0x08)==0x08)
- Hang_13_H;
- else
- Hang_13_L;
- for(i=0;i<1250;i++);
- cord_l=(uint8_t)(Lie_14_V); //?????
- while(1) //???????
- {
- j=(uint8_t)(Lie_14_V);
- if(j==0xF0)
- break;
- for(i=0;i<1250;i++);
- }
- return(cord_h+cord_l);//????????
- }
- }
- return(0xff); //????*/
- }
- unsigned char KeyPro(void)
- {
- switch(KeyScan())
- {
- case 0x7e:return 0;break;//0 ??????????????
- case 0x7d:return 1;break;//1
- case 0x7b:return 2;break;//2
- case 0x77:return 3;break;//3
- case 0xbe:return 4;break;//4
- case 0xbd:return 5;break;//5
- case 0xbb:return 6;break;//6
- case 0xb7:return 7;break;//7
- case 0xde:return 8;break;//8
- case 0xdd:return 9;break;//9
- case 0xdb:return 10;break;//a
- case 0xd7:return 11;break;//b
- case 0xee:return 12;break;//c
- case 0xed:return 13;break;//d
- case 0xeb:return 14;break;//e
- case 0xe7:return 15;break;//f
- default:return 0xff;break;
- }
- }
- /*!
- * [url=home.php?mod=space&uid=247401]@brief[/url] Main program
- *
- * @param None
- *
- * @retval None
- *
- */
- int main(void)
- {
- uint8_t i;
- GPIO_Config_T GPIO_ConfigStruct;
- USART_Config_T USART_ConfigStruct;
- APM_MINI_LEDInit(LED2);
- RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));
- GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
- GPIO_ConfigStruct.pin = GPIO_PIN_9;
- GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
- GPIO_Config(GPIOA, &GPIO_ConfigStruct);
- USART_ConfigStruct.baudRate = 115200;
- USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
- USART_ConfigStruct.mode = USART_MODE_TX;
- USART_ConfigStruct.parity = USART_PARITY_NONE;
- USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
- USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
- USART_Config(USART1, &USART_ConfigStruct);
- USART_Enable(USART1);
- SysTick_Config(SystemCoreClock / 1000);
- while(1)
- {
- temp=KeyPro();
- if(temp!=0xff)
- {
- if((temp>=0)&&(temp<=9))
- txBuf[14]=temp+0x30;
- else if((temp>=10)&&(temp<=15))
- txBuf[14]=temp+55;
- for(i = 0; i < sizeof(txBuf); i++)
- {
- while(USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);
- USART_TxData(USART1, txBuf[i]);
- }
- }
- }
- }
- /*!
- * @brief Delay
- *
- * @param None
- *
- * @retval None
- *
- */
- void Delay(void)
- {
- tick = 0;
- while(tick < 500);
- }
- /**@} end of group USART_Printf_Functions */
- /**@} end of group USART_Printf */
- /**@} end of group Examples */
效果图:
|