Based on stm32, the 4x4 matrix keyboard timer scan and shake method completely removes the delay function!

Source: Internet
Author: User

After my painstaking research, I transplanted the matrix keyboard scanning program on the 51 Single-Chip Microcomputer to the stm32. At the same time, I also made great improvements. The biggest improvement is to shake the image. In the past, the delay latency was always about 10 to 20 ms, but this would cause the MCU to run falsely in the delay and cause the main program to be shelved. Obviously, the real-time performance of the program is greatly reduced, today, let new users stay away from delay and enter a brand new scan to shake the world! (I am also a newbie-a newbie who loves to study! ^_^)

// Stm32 matrix keyboard flip scanning method (8-bit low for Pb ports)
// Place the matrix keyboard scanner in the timer interrupt service program
// Interrupt the service every 10 ms
S8 scan_matrixkey (void)
{
# Define Port gpiob-> IDR
U8 column; // Column
U8 row; // row
U8 TMP; // Temporary Variable
S8 matrixkey_value = 20; // The initial value cannot be 0 ~ 15
Static u8 key_count = 0; // number of times the scan is interrupted

///////// IO port configuration /////////////
// 8-bit lower is the push-pull output
Gpiob-> CRL & = 0x00000000;
Gpiob-> CRL | = 0x33333333;
// Initial value: the minimum value is 4 bits, and the minimum value is 4 bits.
Gpiob-> ODR & = 0xff00;
Gpiob-> ODR | = 0x00f0;
// When the number of times is low, the 4-digit value indicates the pull-up input.
Gpiob-> CRL & = 0x0000ffff;
Gpiob-> CRL | = 0x88880000;

TMP = port; // required
If (TMP! = 0xf0) // If a key is pressed
{
// Prevent variable overflow caused by long time and continuous auto-Increment
If (key_count <= 2)
{
Key_count ++;
}
}
// If the jitter button is raised, the count is cleared.
Else
{
Key_count = 0;
}
// If the scan button is pressed for two consecutive times
// The Key is indeed pressed.
If (key_count = 2)
{
Column = TMP & 0x00f0; // obtain the column number

///////// IO port configuration /////////////
// 8-bit lower is the push-pull output
Gpiob-> CRL & = 0x00000000;
Gpiob-> CRL | = 0x33333333;
// Flip: 4 low bits are high, and 4 low bits are low
Gpiob-> ODR & = 0xff00;
Gpiob-> ODR | = 0x000f; // four low bits are high, and four low bits are low
// The 4-bit lower is the pull-up input.
Gpiob-> CRL & = 0xffff0000;
Gpiob-> CRL | = 0x00008888;

Row = Port & 0x000f; // get the row number
Switch (column | row) // column | row indicates the encoding of the port on which the key is pressed.
{
// The code table corresponding to the buttons (you can adjust the key value to be returned as needed)
Case 0xEE: matrixkey_value = 12; break;
Case 0xde: matrixkey_value = 10; break;
Case 0xbe: matrixkey_value = 11; break;
Case 0x7e: matrixkey_value = 0; break;
Case 0xed: matrixkey_value = 13; break;
Case 0xdd: matrixkey_value = 3; break;
Case 0xbd: matrixkey_value = 2; break;
Case 0x7d: matrixkey_value = 1; break;
Case 0xeb: matrixkey_value = 14; break;
Case 0xdb: matrixkey_value = 6; break;
Case 0xbb: matrixkey_value = 5; break;
Case 0x7b: matrixkey_value = 4; break;
Case 0xe7: matrixkey_value = 15; break;
Case 0xd7: matrixkey_value = 9; break;
Case 0xb7: matrixkey_value = 8; break;
Case 0x77: matrixkey_value = 7; break;
Default: break;
}
}
// If no buttons are pressed (released), the number of scans is cleared by 0.
// Facilitate the next scan count
If (Port & 0x00ff) = 0x00f0)
{
Key_count = 0;
}

Return matrixkey_value;
}

If you have any questions during use, please leave a message to me to enhance communication, learn from each other and make progress together !~

The next stm32 matrix Keyboard Scan timer anti-shake code link is:

Http://blog.csdn.net/dcx1205/article/details/8884089

 

 

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.