ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

按键无中断

2021-07-06 18:30:37  阅读:167  来源: 互联网

标签:Pin temp 中断 InitStructure 按键 GPIO include define


Main.c:

#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "led.h"
#include "key.h"

int main()
{
	u8 temp;
	Led_Init();
	Key_Init();
	while(1)
	{
		temp=Key_Scan(temp);
		switch(temp)
		{
			case 0:
				LED1=LED2=LED3=LED4=1;
				break;
			case 1:
				LED1=0;
				LED2=LED3=LED4=1;
				break;
			case 2:
				LED2=0;
				LED1=LED3=LED4=1;
				break;
			case 3:
				LED3=0;
				LED2=LED1=LED4=1;
				break;
			case 4:
				LED4=0;
				LED2=LED3=LED1=1;
				break;
		}
	}
}

key.h:

#ifndef __KEY_H
#define __KEY_H

#include "sys.h"

#define KEY1 PAin(0)
#define KEY2 PAin(1)
#define KEY3 PAin(2)
#define KEY4 PAin(3)

void Key_Init(void);
u8 Key_Scan(u8 temp);


#endif

key.c:

#include "key.h"
#include "stm32f10x.h"
#include "delay.h"

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 

	GPIO_InitTypeDef GPIO_InitStructure;
	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_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
	
}

u8 Key_Scan(u8 temp)
{
	u8 value=temp; //0表示没有按键被按下
	if(KEY1==0 || KEY2==0 || KEY3==0 || KEY4==0)
	{
		delay(20);
		if(KEY1==0)
		{
			if(temp==1)value=0;
			else value=1;
		}
		if(KEY2==0)
		{
			if(temp==2)value=0;
			else value=2;
		}
		if(KEY3==0)
		{
			if(temp==3)value=0;
			else value=3;
		}
		if(KEY4==0)
		{
			if(temp==4)value=0;
			else value=4;
		}
	}
	while(KEY1==0 || KEY2==0 || KEY3==0 || KEY4==0);
	
	return value;
}

led.h:

#ifndef __LED_H
#define __LED_H

#include "sys.h"

#define LED1 PBout(8)
#define LED2 PBout(9)
#define LED3 PBout(10)
#define LED4 PBout(11)

void Led_Init(void);



#endif

led.c:

#include "led.h"
#include "stm32f10x.h"

void Led_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 

	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
	
	LED1=LED2=LED3=LED4=1;
}

标签:Pin,temp,中断,InitStructure,按键,GPIO,include,define
来源: https://blog.csdn.net/weixin_45705007/article/details/118526741

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有