4 × 4 keyboard of STM32F103RCT6 experiment code

STM32 experiment——4 × 4 buttons
Experiment requirements: use a 4 × 4 keyboard to input to the single-chip microcomputer and output to the host computer through the USART port.
The first part: the detection of keys, the principle can be known through Baidu. The general principle is to output a low level through 4 rows, respectively detect the levels of four columns, and determine which column the key is on. Then, by outputting a high level to that column, it is detected on which row.

Code section:

#include "4X4.h"
#include "delay.h"
#include "usart.h"
	
	
	
void LED_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);	 //使能PB,PE端口时钟
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;				 //LED0-->PB.5 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //IO口速度为50MHz
 GPIO_Init(GPIOA, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB.5
 GPIO_SetBits(GPIOA,GPIO_Pin_8);						 //PB.5 输出高

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;	    		 //LED1-->PE.5 端口配置, 推挽输出
 GPIO_Init(GPIOD, &GPIO_InitStructure);	  				 //推挽输出 ,IO口速度为50MHz
 GPIO_SetBits(GPIOD,GPIO_Pin_2); 						 //PE.5 输出高 
}	
	
	
void LED_R_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);	 //使能PB,PC端口时钟
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;				 //PB0
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //上拉输入

 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB0


	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;				 //PB1
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;		 //上拉输入
 
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB1
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 //PB5
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //上拉输入
 
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB5
	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				 //PB6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		 //上拉输入
 
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB6
 
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;				 //PC9
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC9
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;				 //PC8
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC8
 
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;				 //PC7
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC7
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				 //PC6
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC6
}
 
void LED_C_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB, ENABLE);	 //使能PB,PC端口时钟
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;				 //PC9
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 		 //下拉输入

 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC9


	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;				 //PC8
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;		 //下拉输入
 
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC8
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;				 //PC7
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 		 //下拉输入

 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC7
	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				 //PC6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; 		 //下拉输入
 
 GPIO_Init(GPIOC, &GPIO_InitStructure);					 //根据设定参数初始化GPIOC6
 
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;				 //PB0
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB0
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;				 //PB1
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB1
 
 
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 //PB5
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB5
 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;				 //PB6
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //根据设定参数初始化GPIOB6
}

char key;
u8 KEY_Scan(void)
{	 
	uart_init(9600);
		LED_R_Init();
		GPIO_ResetBits(GPIOC,GPIO_Pin_9);
		GPIO_ResetBits(GPIOC,GPIO_Pin_8);
		GPIO_ResetBits(GPIOC,GPIO_Pin_7);
		GPIO_ResetBits(GPIOC,GPIO_Pin_6);
		delay_ms(10);
		if(C1==0)
		{
			LED_C_Init();
			
			GPIO_SetBits(GPIOB,GPIO_Pin_0);
			delay_ms(10);
			if(R2==1)
				key= '4' ;
			else if(R1==1)
				key='1';
			else if(R3==1)
				key='7';
			else if(R4==1)
				key= '*';
			else 
				key='z';
		
		}
		
		
		else if(C2==0)
		{
			LED_C_Init();
			
			GPIO_SetBits(GPIOB,GPIO_Pin_1);
			delay_ms(10);
			if(R1==1)
				key= '2';
			else if(R2==1)
				key= '5';
			else if(R3==1)
				key= '8';
			else if(R4==1)
				key= '0';
				else 
				key='z';
	
		}
		
		
		else if(C3==0)
		{
			LED_C_Init();
			
			GPIO_SetBits(GPIOB,GPIO_Pin_5);
			delay_ms(10);
			if(R1==1)
				key= '3';
			else if(R2==1)
				key= '6';
			else if(R3==1)
				key= '9';
			else if(R4==1)
				key= '#';
				else 
				key='z';

		}
		
		
		else if(C4==0)
		{
			LED_C_Init();
			
			GPIO_SetBits(GPIOB,GPIO_Pin_6);
			delay_ms(10);
			if(R1==1)
				key= 'A';
			else if(R2==1)
				key= 'B';
			else if(R3==1)
				key= 'C';
			else if(R4==1)
				key= 'D';
				else 
				key='z';

		}
		else key='z';
		return key;
	}	

The comments in this section may be problematic and have not been carefully checked.

Attach the .h file

#ifndef __4X4_H
#define __4X4_H	 
#include "sys.h"

#define R1  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)
#define R2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8)
#define R3  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7)
#define R4  GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6)
#define C1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)
#define C2  GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
#define C3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5)
#define C4  GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)

void LED_R_Init(void);//初始化
void LED_C_Init(void);//初始化
void LED_Init(void);
u8 KEY_Scan(void);  	 				    
#endif

The second part: main function The
main function mainly realizes reading numeric characters in the keyboard and then converting them into numbers, and can input a certain length of numbers, in which (R4, C4) key is the OK key. For example, input three numbers '1', '1', '3', and then press the OK key to output 113 numbers to the PC. When pressing the button, there is LED light prompt. There is also a LED light prompt on the output to the PC.

/************************
名称:用4X4键盘来输入数据显示到PC

时间:2020.3.22

作者:L.J.W

变量作用:arr数组用来储存键盘输入的数据
					D代表输入结束
					i计数来统计一共输入多少个数据
					add是最后显示的数据
				  按下按键led亮 输出数据另一个led亮
************************/


#include "4X4.h"
#include "usart.h"
#include "delay.h"
#include "math.h"
char t;
int main()
{
	 
	LED_Init();		  	//初始化与LED连接的硬件接口
	uart_init(9600);
	delay_init();
int arr[9];
int i;
int count=0;
int	add=0;
int z;

	while(1)
	{	
	t=KEY_Scan();
		if(t=='z')
			t=t;
		else
		{
			if(t=='D')
			{		
				
				
				//用来把arr数组里的数转换到add
				for(count=0;count<i;count++)
			{
				
				add+=arr[count]*(int)pow((float)10,(i-count-1));
			}
			printf("%d\n\r",add);
					
					
			GPIO_ResetBits(GPIOD,GPIO_Pin_2);//LED1输出低
			delay_ms(300);
			GPIO_SetBits(GPIOD,GPIO_Pin_2);//LED0输出高

			add=0;//add清零
			for(z=0;z<9;z++)
			{
			arr[z]=0;//数组清零
			}
			count=0;		
			i=0;//统计数据清零
			}
			else
			{
			//用来i计数并且写入arr数组
			arr[i]=t-'0';
			GPIO_ResetBits(GPIOA,GPIO_Pin_8); //LED0输出低
			delay_ms(300);
			GPIO_SetBits(GPIOA,GPIO_Pin_8);//LED1输出高	
			i++;
				
			}
		
		}
	
	delay_ms(300);

	}
	

	}



This time the key code has 5 keys in addition to the 0-9 numeric keys and a confirmation key, but the code part is not processed. Because the new handwritten code has many omissions, it is more forgiving.

This is the 4X4 code compression package
https://download.csdn.net/download/ljw__/12265113

Published 3 original articles · praised 4 · visits 145

Guess you like

Origin blog.csdn.net/ljw__/article/details/105053068