STM32实战 2.矩阵键盘通过串口3输出

匿名 (未验证) 提交于 2019-12-03 00:21:02
#include "KEY44.h" #include "sys.h" #include "delay.h" #include "usart.h"  u8 key_num = 0;  int main(void) {	 		uart_init(115200); 		delay_init(); 		KEY44_Init(); 	 	while(1) 	{ 		key_num = key44_Scan(); 		if(key_num != 0) 		{ 		printf("KEY is %d\r\n",key_num); 		} 	} 	 } 

#include "KEY44.h" #include "sys.h" #include "delay.h"   void  KEY44_Init(void) { 		GPIO_InitTypeDef GPIO_InitStructure; 		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3; 		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_4 |GPIO_Pin_5 |GPIO_Pin_6 |GPIO_Pin_7; 		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 		GPIO_Init(GPIOA, &GPIO_InitStructure); }  unsigned char key44_Scan(void) { 			H_1 = 0; 			H_2 = 1; 			H_3 = 1; 			H_4 = 1;  	if( L_1 == 0) 	{ 		delay_ms(10); 		if(L_1 == 0) 		{ 			while(!L_1); 			return 1; 		} 	} 	if(L_2 == 0) 	{ 		delay_ms(10); 		if(L_2 == 0) 		{ 			while(!L_2); 			return 2; 		} 	} 	if(L_3 == 0) 	{ 		delay_ms(10); 		if(L_3 == 0) 		{ 			while(!L_3); 			return 3; 		} 	} 	if(L_4 == 0) 	{ 		delay_ms(10); 		if(L_4 == 0) 		{ 			while(!L_4); 			return 4; 		} 	}  	H_1 = 1; 	H_2 = 0; 	H_3 = 1; 	H_4 = 1;  	if(L_1 == 0) 	{ 		delay_ms(10); 		if(L_1 == 0) 		{ 			while(!L_1); 			return 5; 		} 	} 	if(L_2 == 0) 	{ 		delay_ms(10); 		if(L_2 == 0) 		{ 			while(!L_2); 			return 6; 		} 	} 	if(L_3 == 0) 	{ 		delay_ms(10); 		if(L_3 == 0) 		{ 			while(!L_3); 			return 7; 		} 	} 	if(L_4 == 0) 	{ 		delay_ms(10); 		if(L_4 == 0) 		{ 			while(!L_4); 			return 8; 		} 	}  	H_1 = 1; 	H_2 = 1; 	H_3 = 0; 	H_4 = 1;  	if(L_1 == 0) 	{ 		delay_ms(10); 		if(L_1 == 0) 		{ 			while(!L_1); 			return 9; 		} 	} 	if(L_2 == 0) 	{ 		delay_ms(10); 		if(L_2 == 0) 		{ 			while(!L_2); 			return 10; 		} 	} 	if(L_3 == 0) 	{ 		delay_ms(10); 		if(L_3 == 0) 		{ 			while(!L_3); 			return 11; 		} 	} 	if(L_4 == 0) 	{ 		delay_ms(10); 		if(L_4 == 0) 		{ 			while(!L_4); 			return 12; 		} 	}  	H_1 = 1; 	H_2 = 1; 	H_3 = 1; 	H_4 = 0;  	if(L_1 == 0) 	{ 		delay_ms(10); 		if(L_1 == 0) 		{ 			while(!L_1); 			return 13; 		} 	} 	if(L_2 == 0) 	{ 		delay_ms(10); 		if(L_2 == 0) 		{ 			while(!L_2); 			return 14; 		} 	} 	if(L_3 == 0) 	{ 		delay_ms(10); 		if(L_3 == 0) 		{ 			while(!L_3); 			return 15; 		} 	} 	if(L_4 == 0) 	{ 		delay_ms(10); 		if(L_4 == 0) 		{ 			while(!L_4); 			return 16; 		} 	} 	return 0; }

#ifndef __KEY44_H #define __KEY44_H	   	//PA0~PA3为推挽式输出 //PA4~PA7为上拉式输入	  #define H_1 PAout(0) #define H_2 PAout(1) #define H_3 PAout(2) #define H_4 PAout(3)  #define L_1 PAin(4) #define L_2 PAin(5) #define L_3 PAin(6) #define L_4 PAin(7)  void  KEY44_Init(void); unsigned char key44_Scan(void);		     #endif 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!