블루 브리지 내장형 KEYAgain

프로젝트 가시성 Github

1. 주요 코드

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

좋은 웹페이지 즐겨찾기