STM32 key scan [operation register + library function]

Publisher:BlossomSunriseLatest update time:2017-02-06 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This example will implement the key scanning function of stm32.

 

Operation Register

 

When the I/O port of stm32 is used as input, the status of the I/O port is read by reading the contents of the GPIOx -> IDR register.

 

The description of each bit of the IDR register is as follows:

gpio_idr.png

 

Since systick cannot generate interrupts as easily as library functions, it is inconvenient to query the status of each pin after querying the systick status bit. Therefore, unlike the library function method, the pin status is directly queried to detect key presses.

 

The PAout(x), PAin(x) and other functions are called in the sys.h file. See: (The sys.h code refers to the  stm32 direct operation register development environment configuration )

 

Direct operation register code:

#include 	   
#include "system.h"


//Key button port definition
#define key0 PAin(0) // PA0
#define key1 PAin(1) // PA1
#define key2 PAin(2) // PA2
#define key3 PAin(3) // PA3

//LED button port definition
#define LED0 PAout(4) // PA4
#define LED1 PAout(5) // PA5
#define LED2 PAout(6) // PA6
#define LED3 PAout(7) // PA7

void Gpio_Init(void);//initialization function
void Key_Scan(void);

int main(void)
{	
				  
	Rcc_Init(9); //System clock settings
	Gpio_Init(); //Initialize the hardware interface connected to the LED 

	while(1)
	{		
		Key_Scan();
	}
}

void Key_Scan(void)
{	 
	if(key0 == 0 || key1 == 0 || key2 == 0 || key3 == 0)
	//if(GPIOA->IDR != 0x000F)
	{
		 delay(10000); //debounce
		 if(key0 == 0)
		 {
		 	while(key0 == 0); //Detect key release
		 	LED0 = !LED0;
		 }
		 if(key1 == 0)
		 {
		    while(key1 == 0);
		 	LED1 = !LED1;
		 }
		 if(key2 == 0)
		 {
		 	while(key2 == 0);
		 	LED2 = !LED2;
		 }
		 if(key3 == 0)
		 {
		 	while(key3 == 0);
		 	LED3 = !LED3;
		 }
	 }
}

void Gpio_Init(void)
{
	RCC->APB2ENR|=1<<2; //Enable PORTA clock	   	 

	GPIOA->CRL&=0X0000FFFF; // PA0~3 are set as floating input, PA4~7 are set as push-pull output
	GPIOA->CRL|=0X33334444; 
	  
}


 

 

Library function operation

 

Anyone who has studied EDA should know a concept called a state machine. After a certain condition is triggered, it enters another state. If another condition is triggered, it enters the next state. If the condition is not met, it enters the initial state, or does not change the state. This is roughly the idea of ​​implementing key scanning.

  1. Systick generates a 20ms timing and checks in the interruption whether the buttons of each pin are pressed. If a button is pressed, it enters state 1.

  2. If it is pressed, determine whether it is jittering. If it is, return to state 0. If not, determine which pin button is pressed, and enter state 2 after implementing the corresponding function.

  3. In state 2, check whether the key is released. If it is released, return to state 0, otherwise the state does not change.

The code is as follows: main.c


#include "stm32f10x.h"

#define KEYPORT GPIOA
#define KEY0 GPIO_Pin_3
#define KEY1 GPIO_Pin_1
#define KEY2 GPIO_Pin_2
#define KEY3 GPIO_Pin_0

typedef enum
{
	KeyScanState_0 = 0x00,
	KeyScanState_1 = 0x01,
	KeyScanState_2 = 0x02,

}KeyScanState_Typedef;

KeyScanState_Typedef KeyScanState;

void RCC_Configuration(void);
void GPIO_Configuration(void);
void SysTick_Set(vu32 x);


int main(void)
{
  	RCC_Configuration();
  	GPIO_Configuration();
	SysTick_Set(20000);
	while(1);
}

void SysTick_Handler(void)
{
	vu16 keyState;
 
	keyState = GPIO_ReadInputData(KEYPORT) & 0x000f;
	switch(KeyScanState)
	{
	 	case KeyScanState_0:
		{
			if(keyState != 0x000f)
			{
				KeyScanState = KeyScanState_1;
			}
			break;
		}
		case KeyScanState_1:
		{
			if(keyState != 0x000f)
			{
				if(GPIO_ReadInputDataBit(KEYPORT,KEY0) == 0)
				{
					GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
				}else if(GPIO_ReadInputDataBit(KEYPORT,KEY1) == 0)
				{
					GPIO_WriteBit(GPIOA,GPIO_Pin_5,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_5)));
				}else if(GPIO_ReadInputDataBit(KEYPORT,KEY2) == 0)
				{
					GPIO_WriteBit(GPIOA,GPIO_Pin_6,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_6)));
				}else if(GPIO_ReadInputDataBit(KEYPORT,KEY3) == 0)
				{
					GPIO_WriteBit(GPIOA,GPIO_Pin_7,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_7)));
				}

				KeyScanState = KeyScanState_2;
			}else{
			 	KeyScanState = KeyScanState_0;
			}
			break;
		}
		case KeyScanState_2:
		{
		 	if(keyState == 0x000f)
			{
			 	KeyScanState = KeyScanState_0;
			}
			break;
		} 
	}
}


void SysTick_Set(vu32 x)
{
	if(SysTick_Config(x*72)) //Configuration error returns 1, max 16777216 
	{							
		GPIO_SetBits(GPIOA, GPIO_Pin_7); //Error handling 								
	}
}

  
void GPIO_Configuration(void)
{
  	GPIO_InitTypeDef GPIO_InitStructure;

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;			
  	GPIO_Init(GPIOA, &GPIO_InitStructure); 

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;			
  	GPIO_Init(GPIOA, &GPIO_InitStructure); 
}


void RCC_Configuration(void)
{
	/* Define enumeration type variable HSEStartUpStatus */
	ErrorStatus HSEStartUpStatus;

  	/* Reset system clock settings */
  	RCC_DeInit();
  	/* Enable HSE*/
  	RCC_HSEConfig(RCC_HSE_ON);
  	/* Wait for HSE to start oscillating and stabilize*/
  	HSEStartUpStatus = RCC_WaitForHSEStartUp();
	/* Determine whether the HSE is successfully started, if yes, enter if() */
  	if(HSESTartUpStatus == SUCCESS)
  	{
    	/* Select HCLK (AHB) clock source as SYSCLK divided by 1 */
    	RCC_HCLKConfig(RCC_SYSCLK_Div1); 
    	/* Select PCLK2 clock source as HCLK (AHB) divided by 1 */
    	RCC_PCLK2Config(RCC_HCLK_Div1); 
    	/* Select PCLK1 clock source as HCLK (AHB) divided by 2 */
    	RCC_PCLK1Config(RCC_HCLK_Div2);
    	/* Set the FLASH delay cycle number to 2 */
    	FLASH_SetLatency(FLASH_Latency_2);
    	/* Enable FLASH prefetch cache */
    	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    	/* Select the phase-locked loop (PLL) clock source as HSE 1 division, the multiplier is 9, then the PLL output frequency is 8MHz * 9 = 72MHz */
    	RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    	/* Enable PLL */ 
    	RCC_PLLCmd(ENABLE);
    	/* Wait for PLL output to stabilize */
    	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    	/* Select SYSCLK clock source as PLL */
    	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    	/* Wait for PLL to become the SYSCLK clock source */
    	while(RCC_GetSYSCLKSource() != 0x08);
  	} 
  	/* Turn on the GPIOA clock on the APB2 bus */
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);		
}

In this example, the Systick interrupt processing function is moved from stm32f10x_it.c to main.c, avoiding the need to declare external variables in stm32f10x_it.c.


Keywords:stm32 Reference address:STM32 key scan [operation register + library function]

Previous article:stm32 i2c communication [operation register + library function]
Next article:The most convenient timer Systick on stm32 [operation register + library function]

Recommended ReadingLatest update time:2024-11-16 15:49

Learning STM32 serial port (1)
The operation steps of the serial port of stm32: 1) Configure some parameters related to the serial port, such as the baud rate, number of bits, stop bits, flow control, and specify whether to enable or disable the receive or send mode. 2) Initialize the IO pins of the serial port used 3) Enable IO pin 4) Enable
[Microcontroller]
Function explanation of stm32 FSMC
Since there is no Chinese version of FSMC in the library function found on the Internet, I can only reprint the function analysis of FSMC here for rookies like me to learn. typedef struct { uint32_t FSMC_Bank; //nor is divided into four blocks, and this parameter indicates which block to program uint32_t FSMC_D
[Microcontroller]
Watchdog in STM32
In a microcomputer system composed of a single-chip microcomputer, the operation of the single-chip microcomputer is often interfered by the external electromagnetic field, causing the program to run away and fall into an infinite loop. The normal operation of the program is interrupted, and the system controlled by t
[Microcontroller]
STM32 Getting Started with USART (STM32F030F4P6 based on CooCox IDE)
#include "stm32f0xx.h" #include "stm32_lib/inc/stm32f0xx_rcc.h" #include "stm32_lib/inc/stm32f0xx_gpio.h" #include "stm32_lib/inc/stm32f0xx_usart.h"   int main(void) { //1. Enable the clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Special note: Because the serial port uses GPIO, the corresponding GP
[Microcontroller]
Summary of STM32 peripheral DMA usage
Summary of STM32 peripheral DMA usage: 1. Select DAM mode according to your needs: (1) Circular mode—DMA_Mode = DMA_Mode_Circular (2) Normal mode—DMA_Mode = DMA_Mode_Normal 2. For Chanel3 of DMA1, the corresponding peripheral is RX of USART3 . Imagine: if the serial port receive interrupt and DAM interrupt are turned
[Microcontroller]
STM32 open-drain mode and external pull-up resistor issues
First, let's introduce the meaning of open-drain mode. (1) Floating input_IN_FLOATING——Floating input, can be used for KEY identification, RX1 (2) Input with pull-up_IPU——IO internal pull-up resistor input (3) Input with pull-down_IPD——IO internal pull-down resistor input (4) Analog input_AIN——Apply ADC analog inpu
[Microcontroller]
stm32 data storage
Big-endian mode: the high byte of data is stored in the low address of memory, and the low byte of data is stored in the high address of memory; Little-endian mode: the high byte of data is stored in the high address of memory, and the low byte of data is stored in the low address of memory; stm32 defaults to little
[Microcontroller]
STM32-Serial Port Experiment Study Notes
USART1_IRQHandler(void) function: When a corresponding interrupt occurs on serial port 1, it will jump to the function execution. A small receiving protocol is designed here (not defined by the system): through this function, an array USART_RX_BUF and a receiving status register USART_RX_STA are used to manage the rec
[Microcontroller]
STM32-Serial Port Experiment Study Notes
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号