矩阵键盘原理图及代码文件(Matrix Keyboard Circuit Diagram and Code Files A Comprehensive Guide)

jk 413次浏览

最佳答案Matrix Keyboard Circuit Diagram and Code Files: A Comprehensive Guide Matrix keyboard is a popular choice for electronic enthusiasts due to its cost-effective a...

Matrix Keyboard Circuit Diagram and Code Files: A Comprehensive Guide Matrix keyboard is a popular choice for electronic enthusiasts due to its cost-effective and efficient operations. It is a type of keyboard that employs a grid of intersecting lines that allow multiple keys to be pressed simultaneously. In this article, we will explore the principle behind a matrix keyboard and its corresponding code files. The Working Principle of a Matrix Keyboard A matrix keyboard operates on the concept of digital scanning. The keyboard matrix consists of a matrix of switches, each representing a key. The number of possible keys is determined by the dimensions of the matrix. When a key is pressed, a connection is established between the corresponding row and column of the matrix. The microcontroller then scans the matrix row by row and column by column to detect any active key presses. The exact position of the key press can then be determined based on the row and column address. The matrix keyboard requires a scanning process that is performed repeatedly, making use of a timer interrupt. The scanning process is done quickly, typically around 100 times per second or more, ensuring responsiveness. Code Files for Matrix Keyboard To create a matrix keyboard, one must have an understanding of the code required for it to operate. There are different coding options available depending on your preferred language. In this section, we will take a look at the two main coding options for matrix keyboards, namely, the Arduino and C Language. Arduino Code One of the most popular options for coding a matrix keyboard is using Arduino. The code for matrix keyboard using Arduino is simple and straightforward. Here is an example code that can be used: #include const rowPin = {2, 3, 4, 5}; const colPin = {6, 7, 8, 9}; const char keyMap [4][4] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; Keypad myKeypad= Keypad(makeKeymap(keyMap), rowPin, colPin, 4, 4); void setup() { Serial.begin(9600); } void loop() { char key = myKeypad.getKey(); if (key != NO_KEY){ Serial.println(key); delay(100); } } The code above creates a matrix keyboard using Keypad library for Arduino. The matrix has four rows and four columns, as specified in the rowPin and colPin arrays. The keyMap array holds the key values for each row and column intersection. The values are arranged in a 4x4 matrix. The Keypad function then maps the keys to the specific row and column pins. The loop function reads the values of the keys pressed and prints them to the serial monitor. C Language Code C language offers an efficient way to code a matrix keyboard. The code employs a polling mechanism, which looks for a key press and returns the key value when one is detected. CODE: Matrix keyboard using C Language: #include \"stm32f4xx.h\" void init_keyboard_gpio() { RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOCEN; //Enable GPIOC clock GPIOC -> MODER &= ~(GPIO_MODER_MODE0 | GPIO_MODER_MODE1 | GPIO_MODER_MODE2 | GPIO_MODER_MODE3 | GPIO_MODER_MODE4 | GPIO_MODER_MODE5 | GPIO_MODER_MODE6 | GPIO_MODER_MODE7); //set GPIOC0 - GPIOC7 as input GPIOC -> PUPDR &= ~(GPIO_PUPDR_PUPD0 | GPIO_PUPDR_PUPD1 | GPIO_PUPDR_PUPD2 | GPIO_PUPDR_PUPD3 | GPIO_PUPDR_PUPD4 | GPIO_PUPDR_PUPD5 | GPIO_PUPDR_PUPD6 | GPIO_PUPDR_PUPD7); //disable pull up and pull down for GPIOC0 - GPIOC7 } int scan_keyboard() { int row, col; for(row = 0; row<4; row++) { //scan keyboard GPIOC -> ODR &= ~(0x00FF<<8); GPIOC -> ODR |= (0x01<<8)< IDOR & (0x0100< 0) { delay(1); while(scan_keyboard() > 0); printf(\"Key Pressed: %d\ \", key); } } } The code creates a matrix keyboard with four rows and four columns, which is scanned using a for loop. The scan_keyboard function reads the state of the rows and columns. The delay function is used to avoid detecting a key press multiple times if it is held down for a long duration. The main function executes the code loop, i.e., it listens for key presses and responds accordingly. Conclusion A matrix keyboard is an essential component of many electronic projects, and its underlying principles and coding requirements are essential knowledge for any embedded systems engineer. This article provides an insight into the working principle of matrix keyboard and its corresponding code files using C Language and Arduino. With this information, we hope that you can now create your own matrix keyboard and get started on your electronic projects.