STM32 - 키 제어 LED 램프

2869 단어 단편기
main.c
/*********************************************************
**  FileName: 		      

**  Description: 
                 ,     
      CPU            
 
**  Author:		        
**  Date:   		  2019/9/30
**  Others:	  
*********************************************************/
#include "stm32f10x.h" //STM32   
#include "stm32f10x_gpio.h"
#include "sys.h"
#define KEYPORT	GPIOA	
#define KEY1	GPIO_Pin_0	
#define KEY2	GPIO_Pin_1
typedef enum {
	KeyScanState_0=0x00,
	KeyScanState_1=0x01,
	KeyScanState_2=0x02,
}KeyScanState_Typedef;
KeyScanState_Typedef KeyScanState=KeyScanState_0;
vu32 flag=0;
void GPIO_Configuration(void);
void Systick_Configuration(void);
void keyscan(void);
int main (void){//   
	RCC_Configuration(); //    
	GPIO_Configuration();//  GPIO  
	Systick_Configuration();
	while(1)
	{
		keyscan();
	}
}
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin=KEY1|KEY2;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_Init(KEYPORT,&GPIO_InitStructure);	
}
void Systick_Configuration(void)
{
	SysTick_Config(9000000/1000*20);//  20ms        
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
}
void keyscan()
{
 		vu16 KeyPortStatus=0;
    if(flag==1)
    {
        flag=0;
        KeyPortStatus=GPIO_ReadInputData(KEYPORT)&0x0003;
        switch(KeyScanState)
        {
            case KeyScanState_0:
            {
                if(KeyPortStatus!=0x0003)//    
                {
                    KeyScanState=KeyScanState_1;
                }
                break;
            }
            case KeyScanState_1:
            {
                if(KeyPortStatus!=0x0003)//    
                {
                    if(GPIO_ReadInputDataBit(KEYPORT,KEY1)==0)//  1   
                    {
                        GPIO_WriteBit(GPIOB,GPIO_Pin_0,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_0)));
                    }
                    else if(GPIO_ReadInputDataBit(KEYPORT,KEY2)==0)//  2   
                    {
                        GPIO_WriteBit(GPIOB,GPIO_Pin_1,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_1)));
                    }
                    KeyScanState=KeyScanState_2;
                }
                else
                {
                    KeyScanState=KeyScanState_0;
                }
                break;
            }
            case KeyScanState_2:
            {
                if(KeyPortStatus==0x0003)//    
                {
                    KeyScanState=KeyScanState_0;
                }
                break;
            }
        }
    }
}

인터럽트 안에서 20ms씩 설정하고 flag를 한 번 부여하면 됩니다

좋은 웹페이지 즐겨찾기