• Stm32_标准库_5_呼吸灯_按键控制


    Stm32按键和输出差不多

    PA1为LED供给正电,PB5放置按键,按键一端接PB5,另一端接负极

    void Key_Init(void){
    	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //APB2总线连接着GPIOB
    	   GPIO_InitStructur.GPIO_Mode = GPIO_Mode_IPU;
    	   GPIO_InitStructur.GPIO_Pin = GPIO_Pin_5;
    	   GPIO_InitStructur.GPIO_Speed = GPIO_Speed_50MHz;
    	   GPIO_Init(GPIOB, &GPIO_InitStructur);//写入
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    初始化为上拉输入输入HZ不用管,GPIOA和GPIOB都连接在APB2总线上,开启对应时钟才能进一步使用

    这里初始化PB5为输入,PB3不知道啥原因不能作为输入端口

    读输入值

    GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)
    
    • 1

    这个函数返回的是一个uint8_t范围为[0 ~ 255),一般用它装小范围的int类型数据

    在这里插入图片描述

    按键扫描代码

    uint8_t check(void){
    	  uint8_t keynum = 0;//初始化0
    	  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0){
    			 Delay_ms(10);//消抖
    			 while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0);//等待松按键
    			 keynum = 1;
    		}
    		return keynum;//记得返回值
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    完整代码

    #include "stm32f10x.h"    // Device header
    #include "Delay.h"
    
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructuer;//结构体
    GPIO_InitTypeDef GPIO_InitStructur;//定义变量结构体
    
    
    
    void GPIOA_Init(void){
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    	GPIO_InitStructur.GPIO_Mode = GPIO_Mode_AF_PP;
    	GPIO_InitStructur.GPIO_Pin = GPIO_Pin_0;		//GPIO_Pin_15;
    	GPIO_InitStructur.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOA, &GPIO_InitStructur);
    }
    
    
    
    void PWM_Init(void){ //pwm波形配置
    	   TIM_OCStructInit(&TIM_OCInitStructuer);//给不需要配置的结构体成员赋予初始值
    	   TIM_OCInitStructuer.TIM_OCMode = TIM_OCMode_PWM1; //输出比较模式
    	   TIM_OCInitStructuer.TIM_OCPolarity = TIM_OCPolarity_High; //极性
    	   TIM_OCInitStructuer.TIM_OutputState = TIM_OutputState_Enable; //输出使能
    	   TIM_OCInitStructuer.TIM_Pulse = 0; //CCR,频率1kHZ,占空比50%的PWM波形
    	   TIM_OC1Init(TIM2, &TIM_OCInitStructuer);//配置GPIOA端口通道
    }
    
    /*定时器定时中断代码*/
    
    void Timer_Init(void)//使用TM2定时器
    {
    	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    	
    	GPIOA_Init();
    	
    	TIM_InternalClockConfig(TIM2);
    	
    	 
    	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR
    	TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC
    	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
    	
    	
    	PWM_Init();
    	 
    	
    	TIM_Cmd(TIM2, ENABLE);//启动定时器
    }
    
    /*CCR控制灯的亮度在运行过程中不断更改CCR的值来达到呼吸效果*/ //CCR[0~100]值越大灯越亮
    
    
    
    void PWM_setcompare1(uint16_t compare){
    	   TIM_SetCompare1(TIM2, compare);
    }
    
    uint8_t i;
    uint8_t j;
    
    void Led_light(void){
    	  
       for(j = 0; j < 3; j ++){     	
    	   for(i = 0; i <= 100; i ++){
    			   PWM_setcompare1(i);
    			   Delay_ms(8);
    		 }
    		 for(i = 0; i <= 100; i ++){
    			   PWM_setcompare1(100 - i);
    			   Delay_ms(8);
    		 }
    	}
    		 
    }
    
    void Key_Init(void){
    	   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //APB2总线连接着GPIOB
    	   GPIO_InitStructur.GPIO_Mode = GPIO_Mode_IPU;
    	   GPIO_InitStructur.GPIO_Pin = GPIO_Pin_5;
    	   GPIO_InitStructur.GPIO_Speed = GPIO_Speed_50MHz;
    	   GPIO_Init(GPIOB, &GPIO_InitStructur);//写入
    }
    
    uint8_t check(void){
    	  uint8_t keynum = 0;//初始化0
    	  if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0){
    			 Delay_ms(10);//消抖
    			 while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5) == 0);//等待松按键
    			 keynum = 1;
    		}
    		return keynum;//记得返回值
    }
     
    	
    int main(void){
    	Timer_Init();
      Key_Init();
    	while(1){
           if(check() == 1){
    			    Led_light();
    			 }
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107

    效果:按一下按键松开,Led呼吸三次

  • 相关阅读:
    盛元广通矿企煤炭检测实验室信息管理系统3.0
    驱动开发:内核文件读写系列函数
    Python基础之面向对象:3、继承与派生
    Oracle SQL执行计划操作(2)——索引相关操作
    Spring注解驱动之AnnotationConfigApplicationContext(二)
    【Unity | Editor强化工具】资产快速访问工具
    Matlab基本语法(一)
    植物图像识别易语言代码
    【Oauth2】四、OAuth2AuthorizationRequestRedirectFilter
    强化学习:伪代码汇总及用DQN求解MountainCar-v0问题代码
  • 原文地址:https://blog.csdn.net/xyint/article/details/133326497