矩阵键盘通过串口3输出

匿名 (未验证) 提交于 2019-12-03 00:21:02
主程序如下:
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 Num is %d\r\n",key_num); 		} 	} } 
void  KEY44_Init(void) { 	GPIO_InitTypeDef GPIO_InitStructure;//结构体声明 	 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能APB2外设时钟 	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);//选用管脚1,2,3 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置管脚的速率 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//设置管脚为推挽式输出 	GPIO_Init(GPIOA, &GPIO_InitStructure); 	 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 	GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);//选用管脚5,6,7 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//设置管脚为上拉式输入
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; }

void uart_init(u32 bound) 	{     //GPIO端口设置     GPIO_InitTypeDef GPIO_InitStructure; 	USART_InitTypeDef USART_InitStructure; 	NVIC_InitTypeDef NVIC_InitStructure; 	  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART3|RCC_APB2Periph_GPIOA, ENABLE);	//使能USART3,GPIOA时钟  	USART_DeInit(USART3);  //复位串口3 	 //USART1_TX   PA.9     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//复用推挽输出     GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9         //USART1_RX	  PA.10     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入     GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA10     //Usart1 NVIC 配置      NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3 	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能 	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器       //USART 初始化设置  	USART_InitStructure.USART_BaudRate = bound;//一般设置为115200; 	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 	USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 	USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//收发模式      USART_Init(USART1, &USART_InitStructure); //初始化串口     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断     USART_Cmd(USART1, ENABLE);                    //使能串口   } #if EN_USART1_RX   //如果使能了接收 void USART1_IRQHandler(void)                	//串口3中断服务程序 	{ 	u8 Res; #ifdef OS_TICKS_PER_SEC	 	//如果时钟节拍数定义了,说明要使用ucosII了. 	OSIntEnter();     #endif 	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾) 		{ 		Res =USART_ReceiveData(USART3);//(USART3->DR);	//读取接收到的数据 		 		if((USART_RX_STA&0x8000)==0)//接收未完成 			{ 			if(USART_RX_STA&0x4000)//接收到了0x0d 				{ 				if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始 				else USART_RX_STA|=0x8000;	//接收完成了  				} 			else //还没收到0X0D 				{	 				if(Res==0x0d)USART_RX_STA|=0x4000; 				else 					{ 					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ; 					USART_RX_STA++; 					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收	   					}		  				} 			}   		       } 


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