• STM32 驱动


    led 驱动

    led头文件

    #ifndef _LED_H
    #define _LED_H
    
    void LED_Init(void);
    void LED_ON(void);
    void LED_OFF(void);
    void LED_Turn(void);
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    led代码

    #include "stm32f10x.h" 
    
    void   LED_Init(void)
    {
        //START CLOCK
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 ;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    
        GPIO_Init(GPIOA,&GPIO_InitStructure);
    
        GPIO_SetBits(GPIOA,GPIO_Pin_1 );
    }
    void LED_ON(void){
        GPIO_ResetBits(GPIOA,GPIO_Pin_1);
    }
    void LED_OFF(void){
        GPIO_SetBits(GPIOA,GPIO_Pin_1);
    }
    void LED_Turn(void)
    {
        if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==0)
        {
            GPIO_SetBits(GPIOA,GPIO_Pin_1);//关灯
        }else{
            GPIO_ResetBits(GPIOA,GPIO_Pin_1);//开灯
        }
        
    }
    
    • 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

    delay 延迟

    delay头文件

    #ifndef __DELAY_H
    #define __DELAY_H
    
    void Delay_us(uint32_t us);
    void Delay_ms(uint32_t ms);
    void Delay_s(uint32_t s);
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    delay代码

    #include "stm32f10x.h"
    
    /**
      * @brief  微秒级延时
      * @param  xus 延时时长,范围:0~233015
      * @retval 无
      */
    void Delay_us(uint32_t xus)
    {
    	SysTick->LOAD = 72 * xus;				//设置定时器重装值
    	SysTick->VAL = 0x00;					//清空当前计数值
    	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
    	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
    	SysTick->CTRL = 0x00000004;				//关闭定时器
    }
    
    /**
      * @brief  毫秒级延时
      * @param  xms 延时时长,范围:0~4294967295
      * @retval 无
      */
    void Delay_ms(uint32_t xms)
    {
    	while(xms--)
    	{
    		Delay_us(1000);
    	}
    }
     
    /**
      * @brief  秒级延时
      * @param  xs 延时时长,范围:0~4294967295
      * @retval 无
      */
    void Delay_s(uint32_t xs)
    {
    	while(xs--)
    	{
    		Delay_ms(1000);
    	}
    } 
    
    
    • 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

    光敏传感器

    头文件

    #ifndef _LightSensor_H
    #define _LightSensor_H
    
    void LightSensor_Init(void);
    uint8_t LightSensor_Get(void);
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    c文件

    #include "stm32f10x.h" 
    
    void LightSensor_Init(void)
    {
         //START CLOCK
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        
        GPIO_InitTypeDef GPIO_InitStructure;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13 ;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    
        GPIO_Init(GPIOB,&GPIO_InitStructure);
    }
    
    uint8_t LightSensor_Get(void)
    {
        return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    对射式红外传感器驱动

    c文件

    #include "stm32f10x.h"                  // Device header
    
    uint16_t CountSensor_Count;
    
    void CountSensor_Init(void)
    {
    	//开启GPIOB时钟
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    
    	// 开启AFIO时钟
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    
    	//初始化GPIO_InitTypeDef
    	GPIO_InitTypeDef GPIO_InitStructure;
    	//高电平输入
    	GPIO_InitStructure.GPIO_Mode=  GPIO_Mode_IPU;
    	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
    	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
    
    	GPIO_Init(GPIOB,&GPIO_InitStructure);
    
    	//配置AFIO
    	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
    
    	// EXTI
    	EXTI_InitTypeDef EXTI_InitStruct;
    	EXTI_InitStruct.EXTI_Line=EXTI_Line14 ;
    	EXTI_InitStruct.EXTI_LineCmd=ENABLE;
    	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
    	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
    
    
    	EXTI_Init(&EXTI_InitStruct);
    
    	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    	NVIC_InitTypeDef NVIC_InitStruct;
    	NVIC_InitStruct.NVIC_IRQChannel=EXTI15_10_IRQn;
    	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
    	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
    	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
    	NVIC_Init(&NVIC_InitStruct);
    
    
    }
    
    void EXTI15_10_IRQHandler (void){
    	if (EXTI_GetITStatus(EXTI_Line14)==SET)
    	{
    		CountSensor_Count ++;
    		EXTI_ClearITPendingBit(EXTI_Line14);
    	}
    	
    }
    
    uint16_t CountSensor_Get(void)
    {
    	return CountSensor_Count;
    }
    
    // void EXTI15_10_IRQHandler(void)
    // {
    // 	if (EXTI_GetITStatus(EXTI_Line14) == SET)
    // 	{
    // 		/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
    // 		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
    // 		{
    // 			CountSensor_Count ++;
    // 		}
    // 		EXTI_ClearITPendingBit(EXTI_Line14);
    // 	}
    // }
    
    
    • 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

    .h文件

    #ifndef __COUNT_SENSOR_H
    #define __COUNT_SENSOR_H
    
    void CountSensor_Init(void);
    uint16_t CountSensor_Get(void);
    
    #endif
    
    ```c
    #ifndef __COUNT_SENSOR_H
    #define __COUNT_SENSOR_H
    
    void CountSensor_Init(void);
    uint16_t CountSensor_Get(void);
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5-2 旋转编码器计次

    #ifndef __ENCODER_H
    #define __ENCODER_H
    
    void Encoder_Init(void);
    int16_t Encoder_Get(void);
    
    #endif
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    #include "stm32f10x.h"                  // Device header
    
    int16_t Encoder_Count;
    
    void Encoder_Init(void)
    {
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    	
    	GPIO_InitTypeDef GPIO_InitStructure;
    	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOB, &GPIO_InitStructure);
    	
    	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
    	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
    	
    	EXTI_InitTypeDef EXTI_InitStructure;
    	EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;
    	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    	EXTI_Init(&EXTI_InitStructure);
    	
    	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    	
    	NVIC_InitTypeDef NVIC_InitStructure;
    	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    	NVIC_Init(&NVIC_InitStructure);
    
    	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    	NVIC_Init(&NVIC_InitStructure);
    }
    
    int16_t Encoder_Get(void)
    {
    	int16_t Temp;
    	Temp = Encoder_Count;
    	Encoder_Count = 0;
    	return Temp;
    }
    
    void EXTI0_IRQHandler(void)
    {
    	if (EXTI_GetITStatus(EXTI_Line0) == SET)
    	{
    		/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
    		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
    		{
    			if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
    			{
    				Encoder_Count++;
    			}
    		}
    		EXTI_ClearITPendingBit(EXTI_Line0);
    	}
    }
    
    void EXTI1_IRQHandler(void)
    {
    	if (EXTI_GetITStatus(EXTI_Line1) == SET)
    	{
    		/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
    		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
    		{
    			if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0)
    			{
    				Encoder_Count --;
    			}
    		}
    		EXTI_ClearITPendingBit(EXTI_Line1);
    	}
    }
    
    
    • 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

    TIM定时器驱动

    #include "stm32f10x.h"                  // Device header
    
    void Timer_Init(void)
    {
    	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    
    	TIM_InternalClockConfig(TIM2);
    	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
    	TIM_TimeBaseInitStructure.TIM_Period=10000-1;
    	TIM_TimeBaseInitStructure.TIM_Prescaler=7200-1;
    	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
    
    	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
    	//使能中断
    
    	TIM_ClearFlag(TIM2,TIM_FLAG_Update);
    	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    
    	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    	NVIC_InitTypeDef NVIC_InitStructure;
    	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    	NVIC_Init(&NVIC_InitStructure);
    
    		
    	TIM_Cmd(TIM2, ENABLE);
    }
    // void TIM2_IRQHandler(void)
    // {
    // 	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
    // 	{
    // 		num++;
    // 		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    // 	}
    // }
    
    
    
    
    • 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
  • 相关阅读:
    unity tolua热更新框架教程(2)
    SaaS 电商设计 (二) 全链路解决方案概述和核心业务流程梳理
    加拿大便携式火炉ASTM F3363-19(适用于燃烧液体或凝胶燃料的便携式无排气口装置的标准规范)标准解答
    LNMP配置(Linux、Nginx、MySQL、PHP)
    软件测试这些基本类型你知道吗?
    大数据面试题(三):MapReduce核心高频面试题
    宝塔自建bitwarden密码管理器
    以太坊 CALL 数据解析【ETH】
    Seata
    最新冠军方案开源 | MOTRv2:YOLOX与MOTR合力打造最强多目标跟踪!(旷视&上交)...
  • 原文地址:https://blog.csdn.net/weixin_45079974/article/details/132991893