找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1912|回复: 0
收起左侧

KeyMatrix-Library (STM32F4)源码

[复制链接]
ID:263845 发表于 2017-12-19 19:16 | 显示全部楼层 |阅读模式
KeyMatrix Library (STM32F4)
blogmedia-keypad-schematic-gif.gif
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)

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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