Intelligent Vehicle Learning (vii)--implementation of key matrix

Source: Internet
Author: User

First, the principle of explanation

is the key matrix code to write a description, that is, K5 to K7 first output high, and K1 and K4 are tuned into the pull-up input, if detected K1 to K4 has a change to 0, indicating that there is a key press down, immediately convert, yes K1 to K4 set to output high level, and then read K5 to K7 for the pull-up input, Then read to the point of 0, the two data will be able to determine that the button was pressed.

Second, based on KVL25 code writing:

1. header file

#ifndef __keysquare_h#define__keysquare_h/*********** macro Definition ************/#defineKeysquare_k1 PTD7#defineKeysquare_k2 PTD6#defineKeysquare_k3 PTD5#defineKeysquare_k4 PTD4#defineKeysquare_k5 PTD3#defineKeysquare_k6 PTD2#defineKeysquare_k7 PTD1#defineKEYNUM1 1#defineKeynum2 2#defineKEYNUM3 3#defineKEYNUM4 4#defineKEYNUM5 5#defineKEYNUM6 6#defineKEYNUM7 7#defineKEYNUM8 8#defineKEYNUM9 9#defineKEYNUM10 10#defineKEYNUM11 11#defineKEYNUM12 12/*****************************//*********** function Nerve ***********/intKeyboard_get (void);/*****************************/#endif

2. Source Files

#include"include.h"#include"Common.h"#include"OLED.h"#include"gpio.h"#include"KeySquare.h"#include"Lptmr.h"intKeyboard_get (void) {unsignedCharTempdata=0x00; unsignedCharBdata=0x00; intkeynum=0; //set as input to detectGpio_init (KEYSQUARE_K1, GPI,1); Gpio_init (Keysquare_k2, GPI,1); Gpio_init (Keysquare_k3, GPI,1); Gpio_init (Keysquare_k4, GPI,1); //set to pull-upPort_init_noalt (Keysquare_k1,pullup);   Port_init_noalt (Keysquare_k2,pullup);   Port_init_noalt (Keysquare_k3,pullup);      Port_init_noalt (Keysquare_k4,pullup); //set to OutputGpio_init (Keysquare_k5,gpo,0); Gpio_init (Keysquare_k6,gpo,0); Gpio_init (Keysquare_k7,gpo,0); Lptmr_delay_us (4); TempData=Gpio_get (KEYSQUARE_K1); TempData+ = Gpio_get (KEYSQUARE_K2) <<1; TempData+ = Gpio_get (KEYSQUARE_K3) <<2; TempData+ = Gpio_get (KEYSQUARE_K4) <<3; if(TempData! =0xFF) {bdata= (TempData <<4) |0x00; Gpio_init (Keysquare_k1, GPO,0); Gpio_init (Keysquare_k2, GPO,0); Gpio_init (Keysquare_k3, GPO,0); Gpio_init (Keysquare_k4, GPO,0); Gpio_init (KEYSQUARE_K5,GPI,1); Gpio_init (KEYSQUARE_K6,GPI,1); Gpio_init (KEYSQUARE_K7,GPI,1); //Pull UpPort_init_noalt (Keysquare_k5,pullup);       Port_init_noalt (Keysquare_k6,pullup);       Port_init_noalt (Keysquare_k7,pullup); Lptmr_delay_us (4); TempData=Gpio_get (KEYSQUARE_K5); TempData+ = Gpio_get (keysquare_k6) <<1; TempData+ = Gpio_get (keysquare_k7) <<2; Bdata+=TempData; Switch(bdata) { Case  the: KEYNUM=KEYNUM1; Break;  Case 214: keynum=keynum2; Break;  Case 182: keynum=keynum3; Break;  Case 118: KEYNUM=KEYNUM4; Break;  Case 229: KEYNUM=KEYNUM5; Break;  Case 213: KEYNUM=KEYNUM6; Break;  Case 181: KEYNUM=KEYNUM7; Break;  Case 117: KEYNUM=KEYNUM8; Break;  Case 227: KEYNUM=KEYNUM9; Break;  Case 211: KEYNUM=KEYNUM10; Break;  Case 179: KEYNUM=KEYNUM11; Break;  Case  the: KEYNUM=KEYNUM12; Break; default: Break; }    }    returnKeynum;}

Third, the use of instructions

When used, a call is made in the main function, and each time a value is returned, 0 means that it is not pressed, or the corresponding key value is returned.

Intelligent Vehicle Learning (vii)--implementation of key matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.