1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #ifndef __MATRIXKEY_H #define __MATRIXKEY_H
#include "Config"
void MK_Init();
void MK_Scan(void(*key_down) (u8, u8), void(*key_up) (u8, u8));
#endif =========================================================================
#include "MatrixKey.h"
#define COL1 P03 #define COL2 P06 #define COL3 P07 #define COL4 P17 #define ROW1 P34 #define ROW2 P35 #define ROW3 P40 #define ROW4 P41
#define is_up(status, row, col) ((status & (1 << 4 * row + col)) > 0)
#define is_down(status, row, col) ((status & (1 << 4 * row + col)) == 0)
#define set_up(status, row, col) (status |= (1 << 4 * row + col))
#define set_down(status, row, col) (status &= (1 << 4 * row + col)) u16 status = 0xFFFF; void GPIO_Config_MatrixKey(void) { GPIO_InitTypeDef GPIO_Init; GPIO_Init.Mode = GPIO_PullUp; GPIO_Init.Pin = GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_Inilize(GPIO_P0, &GPIO_Init); GPIO_Init.Pin = GPIO_Pin_7; GPIO_Inilize(GPIO_P1, &GPIO_Init); GPIO_Init.Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_Inilize(GPIO_P3, &GPIO_Init); GPIO_Init.Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Inilize(GPIO_P4, &GPIO_Init); } void MK_Init(void) { GPIO_Config_MatrixKey(); }
void set_row(u8 row) { ROW1 = row == 0? 0 : 1; ROW2 = row == 1? 0 : 1; ROW3 = row == 2? 0 : 1; ROW4 = row == 3? 0 : 1; }
u8 get_col(u8 col) { if(col == 0) return COL1; else if(col == 1) return COL2; else if(col == 2) return COL3; else if(col == 3) return COL4; }
int i, j;
void MK_Scan(void(*key_down) (u8, u8), void(*key_up) (u8, u8)) { for( i = 0; i < 4; i++) { set_row(i); for(j = 0; j < 4; j++) { if(get_col(j) == 0 && is_up(status, i, j)) { set_down(status, i, j); if (key_down != null) key_down(i j); }else if(get_col(j) == 1 && is_down(status, i, j)) { set_up(status, i, j); if (key_up != null) key_down(i j); } } } } =========================================================================
#include "STC8G_H_GPIO.h" #include "STC8G_H_NVIC.h" #include "STC8G_H_UART.h" #include "STC8G_H_Switch.h" #include "STC8G_H_Delay.h" #include "MatrixKey.h"
void UART_Config(void) { COMx_InitDefine UART_Init; UART_Init.UART_Mode = UART_8bit_BRTx; UART_Init.UART_BRT_Use = BRT_Timer1; UART_Init.UART_BaudRate = 115200ul; UART_Init.Morecommunicate = DISABLE; UART_Init.UART_RxEnable = ENABLE; UART_Init.BaudRateDouble = DISABLE; UART_Configuration(UART1, &UART_Init); NVIC_UART1_Init(ENABLE, Priority_1); UART1_SW(UART1_SW_P30_P31); }
void down(u8 row, u8 col) { printf("按下:%d-%d\n", (int)row, (int)col); }
void up(u8 row, u8 col) { printf("弹起:%d-%d\n", (int)row, (int)col); }
void main() { EA = 1; UART_Config(); MK_Init(); while(1) { MK_Scan(down, NULL); delay_ms(20); } }
|