蓝桥嵌入式之 KEY_Again

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ReCclay/article/details/87688744

工程可见Github<传送门>


一、主要代码

main.c

/*******************************************************************************
* 文件名:main.c
* 描  述:
* 作  者:CLAY
* 版本号:v1.0.0
* 日  期: 2019年2月19日
* 备  注:修改后的LCD例程
*         定时器实现2ms定时中断;按下B1,LED1状态改变
*******************************************************************************
*/

#include "stm32f10x.h"
#include "lcd.h"
#include "e2prom.h"
#include "stdio.h"
#include "i2c.h"
#include "adc.h"
#include "rtc.h"
#include "usart2.h"
#include "pwm.h"
#include "pwm_oc.h"
#include "pwm_ic.h"
#include "timer.h"
#include "led.h"
#include "key.h"

u32 TimingDelay = 0;
u8 RxdCnt = 0;
u8 RxdOver = 0;
u8 RxdBuf[20];
void Delay_Ms(u32 nTime);
u8 RTC_Flag = 0;

//Main Body
int main(void)
{
	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	SysTick_Config(SystemCoreClock/1000);
	
	//PWM_Init(500, 60);//500Hz 60%方波//PA1
	PWM_OC_Init(500, 60);//500Hz 60%方波 PA1
	PWM_IC_Init();//PA7
	
	TIM4_Init(2000, 72);//定时2ms
	LED_Init();//LED配置
	KeyInit();
	
	while(1)
	{
		KeyDriver();
	}
}

void KeyAction(int code)
{
	if(code == 1)//按下B1,LED1状态改变
	{
		GPIOD->ODR |= (1<<2);//使能573
		GPIOC->ODR ^= (1<<8);//LED1改变
		GPIOD->ODR &= (1<<2);//失能573
	}
}

//
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

key.c

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

extern void KeyAction(int code);

u8 KeySta[4] = {1, 1, 1, 1};

void KeyInit(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void KeyDriver(void)
{
	u8 i;
	static u8 backup[4] = {1, 1, 1, 1};
	
	for(i=0; i<4; i++)
	{
		if(backup[i] != KeySta[i])
		{
			if(backup[i] != 0)
			{
				KeyAction(i+1);
			}
			backup[i] = KeySta[i];
		}
	}
}

void KeyScan(void)
{
	u8 i;
	static u8 keybuf[4] = {0xFF, 0xFF, 0xFF, 0xFF};
	
	keybuf[0] = (keybuf[0] << 1) | KEY1;
	keybuf[1] = (keybuf[1] << 1) | KEY2;
	keybuf[2] = (keybuf[2] << 1) | KEY3;
	keybuf[3] = (keybuf[3] << 1) | KEY4;
	
	for(i=0; i<4; i++)
	{
		if(keybuf[i] == 0xFF)
		{
			KeySta[i] = 1;
		}
		else if(keybuf[i] == 0x00)
		{
			KeySta[i] = 0;
		}
		else
		{}
			
	}
}

key.h

#ifndef _KEY_H
#define _KEY_H


#define KEY1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)
#define KEY3 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1)
#define KEY4 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2)

void KeyInit(void);
void KeyDriver(void);
void KeyScan(void);


#endif

stm32f10x_it.c

void TIM4_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET) 
	{
		TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
		KeyScan();
	}
}

二、注意事项

1、按键分别再PA0/PA8/PB1/PB2
2、宏定义按键值得在品味下
#define KEY1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)
3、别忘了在主函数中KeyInit
展开阅读全文

没有更多推荐了,返回首页