stm32f030 development board.
Use HAL library.
The state machine performs debounce.
col column, Pin is configured as PP push-pull output mode;
row, the Pin is configured as Input mode and the internal pull-up resistor is enabled.
code show as below:
.h files
/*
*
* Name: keypad.h
* Faq: www.mazclub.com
*/
#ifndef KEYPAD_H
#define KEYPAD_H
#include "stm32f0xx_hal.h"
//#include "pinname.h"
#define COLS (GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6)
#define PORT_COL GPIOB
#define KEYy0 GPIO_PIN_6
#define KEYy1 GPIO_PIN_5
#define KEYy2 GPIO_PIN_4
#define KEYy3 GPIO_PIN_3
#define PORT_ROW GPIOA
#define KEYx0 GPIO_PIN_12
#define KEYx1 GPIO_PIN_11
#define KEYx2 GPIO_PIN_10
#define KEYx3 GPIO_PIN_9
// Read pin
//#define In(GPIO_Pin) (PORT_KEY->IDR & GPIO_Pin)
#define In(GPIO_Pin) HAL_GPIO_ReadPin(PORT_ROW, GPIO_Pin)
// Write 1 to Pin
//#define High(GPIO_Pin) PORT_KEY->BSRR = GPIO_Pin
#define High(GPIO_Pin) HAL_GPIO_WritePin(PORT_COL, GPIO_Pin, GPIO_PIN_SET)
// Write 0 to Pin
//#define Low(GPIO_Pin) PORT_KEY->BSRR = (uint32_t)GPIO_Pin << 16
#define Low(GPIO_Pin) HAL_GPIO_WritePin(PORT_COL, GPIO_Pin, GPIO_PIN_RESET)
/*
* 0 1 2 3
* 4 5 6 7
* 8 9 10 11
* 12 13 14 15
*/
typedef enum {
Key_Up = 0x02,
Key_Left = 0x03,
Key_Right = 0x04,
Key_Down = 0x08,
Key_Power = 0x09,
Key_Mode = 0x0a,
Key_None = 0xFF
} KeyPressed;
static const int row_count = 4;
static const int col_count = 4;
uint16_t bus_out(void);
void Keypad(void);
char AnyKey(void);
char SameKey(void);
char ScanKey(void);
void FindKey(void);
void ClearKey(void);
void Read(void);
/** Start the keypad interrupt routines */
void Start(void);
/** Stop the keypad interrupt routines */
void Stop(void);
void Cols_out(uint16_t v);
void Scan_Keyboard(void);
KeyPressed getKey(void);
#endif // KEYPAD_H
.c files
/*
*
* Name: keypad.cpp
* Faq: www.mazclub.com
*
*/
#include "keypad.h"
// State:
char KeyState;
// Bit pattern after each scan:
char KeyCode;
// Output value from the virtual 74HC922:
KeyPressed KeyValue;
// KeyDown is set if key is down:
char KeyDown;
// KeyNew is set every time a new key is down:
char KeyNew;
// Mapping table
char KeyTable[12][2];
// Pin of Row
uint16_t _rows[] = {KEYx0, KEYx1, KEYx2, KEYx3};
uint16_t _cols[] = {KEYy0, KEYy1, KEYy2, KEYy3};
//Constructor
void Keypad(void)
{
Stop();
KeyState = 0; // The initial key state is 0
}
//Scan the keyboard
void Scan_Keyboard(void){
switch (KeyState) {
case 0: {
if (AnyKey()) {
char scankey = ScanKey();
if (scankey != 0xff)
KeyCode = scankey;
KeyState = 1;
}
break;
}
case 1: {
if (SameKey()) {
FindKey();
KeyState = 2;
}
else
KeyState = 0;
break;
}
case 2: {
if (SameKey()) {
}
else
KeyState = 3;
break;
}
case 3: {
if (SameKey()) {
KeyState = 2;
}
else {
ClearKey();
KeyState = 0;
}
break;
}
}
// func end
}
// A key is pressed
char AnyKey(void) {
//Start(); //Pull down
int r = -1;
for (r = 0; r < row_count; r++) {
if (In(_rows[r]) == 0) // In macro
break;
}
//Stop(); //Resume
if (!(0 <= r && r < row_count))
return 0;
else
return 1;
}
//Key pressed, key value is the same
char SameKey(void) {
// char KeyCode_new = KeyCode;
char KeyCode_new = ScanKey();
if (KeyCode == KeyCode_new)
return 1;
else
return 0;
}
// Scan key
char ScanKey(void) {
/* Line scan */
int r = -1;
for (r = 0; r < row_count; r++) {
if (In(_rows[r]) == 0) // In macro
break;
}
/* If no valid row is found, return */
if (!(0 <= r && r < row_count)) {
return 0xff;
}
/* Scan the columns to find out which row is pulled low */
int c = -1;
for (c = 0; c < col_count; c++) {
// Output column lines in turn
Cols_out(~(1 << c));
if (In(_rows[r]) == 0) // In macro
break;
}
/* Recharge all columns */
Start();
/* If no valid column is found, return */
if (!(0 <= c && c < col_count))
return 0xff;
return r * col_count + c;
}
// FindKey compares KeyCode to values in KeyTable.
// If match, KeyValue, KeyDown and KeyNew are updated.
void FindKey(void) {
KeyValue = (KeyPressed)KeyCode;
KeyDown = 1;
KeyNew = 1;
}
void ClearKey(void) {
KeyDown = 0;
}
KeyPressed getKey(void) {
if(KeyNew)
{
KeyNew = 0;
return KeyValue;
}
else
return Key_None;
}
void Start(void)
{
/* Output 0 for the column, pull the row down */
PORT_COL->BRR = COLS;
}
void Stop(void)
{
/* Column output 1 so that the row is not pulled low */
PORT_COL->BSRR = COLS;
}
// cols bus output
void Cols_out(uint16_t v)
{
if ((v & 0x01) > 0) //0b001
High(_cols[0]);
else
Low(_cols[0]);
if ((v & 0x02) > 0) //0b010
High(_cols[1]);
else
Low(_cols[1]);
if ((v & 0x04) > 0) //0b100
High(_cols[2]);
else
Low(_cols[2]);
if ((v & 0x08) > 0) //0b100
High(_cols[3]);
else
Low(_cols[3]);
}
Previous article:Output Compare of stm32 timer
Next article:Understanding STM32 open-drain output and push-pull output
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief discussion: Electromagnetic compatibility (EMC) radio frequency electromagnetic field radiation immunity test plan
- The biggest company in the Metaverse is also laying off employees. It is said to be bigger than Twitter.
- EEWORLD University Hall----Live Replay: ADI Multi-parameter Optical Water Quality Analysis Platform
- How to improve the frequency accuracy of the resonant circuit?
- [FS-IR02 + D1CS-D54] - 3: Electrical performance index detection (D1CS-D54)
- RF application scenarios——Q value
- Last three days! Apply for the free Zhongke Yihai Micro-Shenzhen series FPGA development board EQ6HL45
- http get weather + cjson print output
- Evaluate the TMDSCNCD280025C + TMDS HSECDOCK Kit for Free
- EEWORLD University Hall----Live Replay: ADI TOF (Time of Flight) Technology Introduction and Product Application