zoukankan      html  css  js  c++  java
  • STM32F4 3.GPIO按键输入,实现开关灯

    void LED_Init(void)
    {
        //GPIOF9初始化设置
        GPIO_InitTypeDef  GPIO_InitStructure;
    
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//LED对应IO口
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
        
        GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIO
        
        GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9设置高,灯灭
        //GPIO_ResetBits(GPIOF,GPIO_Pin_9);//输出低电平,灯亮
    }
    
    void Key_InIt(void)
    {
            //GPIOF9初始化设置
        GPIO_InitTypeDef  GPIO_InitStructure;
    
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF时钟
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//LED对应IO口
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//输出模式
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
        
        GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIO
    }
    
    u8 KEY_Scan(u8 mode)
    {     
    static u8 key_up=1;//按键按松开标志
        if(mode)key_up=1;  //支持连按          
        if(key_up&&(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0))
        {
            delay_ms(10);//去抖动 
            
            key_up=0;
            if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==0)return 1;
        }else if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)==1)key_up=1;         
         return 0;// 无按键按下
    }
    
    int main(void)
    {
        u8 key;
        u8 sw=0;
        LED_Init();
        Key_InIt();        //保存键值
        delay_init(168);  //初始化延时函数
        while(1)    
        {
            key=KEY_Scan(0);        //得到键值
               if(key)
            {                           
                if(sw==0)
                {
                    GPIO_ResetBits(GPIOF,GPIO_Pin_9);//输出低电平,灯亮
                    sw=1;
                }
                else
                {
                    GPIO_SetBits(GPIOF,GPIO_Pin_9);//GPIOF9设置高,灯灭
                    sw=0;
                }
            }else delay_ms(10); 
        }
    }
  • 相关阅读:
    【MM 】采购合同成批维护
    【FICO 汇率】汇率
    【S4 MM】S4中继续使用MB系统事务代码(转)
    【MM 交货成本】Unplanned Delivery Cost
    Tracer Deployment UVALive
    The Best Path HDU
    Artwork Gym
    PTA 银行排队问题之单队列多窗口加VIP服务 队列+模拟
    Counting Cliques HDU
    Do not pour out HDU
  • 原文地址:https://www.cnblogs.com/xwcs/p/13192077.html
Copyright © 2011-2022 走看看