KeyMatrix Library (STM32F4)
blogmedia-keypad-schematic-gif.gif (7.5 KB, 下載次數: 51)
下載附件
2017-12-19 19:15 上傳
Here is a library with which you can connect a keypad that is built as an X / Y matrix to the STM32F4.
It does not matter what the arrangement of the key matrix is (3 × 3, 4 × 3, 2 × 5, etc.).
No external diodes or pull-ups are needed.
In the H-file the size of the keyboard matrix is set (in the example 3 × 4)
and in the C-File the GPIO pins are assigned to the X and Y direction.
There is a function to read out the pressed key, which returns "0" if no key is pressed. If one or more keys are pressed, the key number of the first key found is returned. (In Example 1 to 12)
To debounce the buttons no timer but only a loop counter is used.
(this event needs to be adapted for you)
Note: if the return value is> 0 even if no keyboard is connected, external pull-ups (4k7) must be attached to the Y-lines.
Images :
//--------------------------------------------------------------
// File : main.c
// Datum : 02.05.2014
// Version : 1.0
// Autor : UB
// EMail : mc-4u(@)t-online.de
// CPU : STM32F4
// IDE : CooCox CoIDE 1.7.4
// GCC : 4.7 2012q4
// Module : CMSIS_BOOT, M4_CMSIS_CORE
// Funktion : Demo der Key-Matrix-Library
// Hinweis : Diese zwei Files muessen auf 8MHz stehen
// "cmsis_boot/stm32f4xx.h"
// "cmsis_boot/system_stm32f4xx.c"
//--------------------------------------------------------------
#include "main.h"
#include "stm32_ub_keymatrix.h"
#include "stm32_ub_led.h"
int main(void)
{
uint8_t key_value;
SystemInit(); // Quarz Einstellungen aktivieren
// init der LEDs
UB_Led_Init();
// init der Tastatur-Matrix
UB_KeyMatrix_Init();
while(1)
{
// Tastatur-Matrix abfragen
key_value=UB_KeyMatrix_Scan();
// LEDs je nach Tasten-Nr schalten
if(key_value==1) UB_Led_On(LED_GREEN); else UB_Led_Off(LED_GREEN);
if(key_value==2) UB_Led_On(LED_RED); else UB_Led_Off(LED_RED);
if(key_value==3) UB_Led_On(LED_BLUE); else UB_Led_Off(LED_BLUE);
if(key_value==4) UB_Led_On(LED_ORANGE); else UB_Led_Off(LED_ORANGE);
}
}
全部資料51hei下載地址:
Demo_83_KEY_Matrix.zip
(173.43 KB, 下載次數: 12)
2017-12-19 19:16 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|