【STM32单片机_(HAL库)】4-3-4【定时器TIM】测量按键按下时间实现3

在这里插入图片描述

1.硬件

2.软件

#include "sys.h"
#include "delay.h"
#include "led.h"
#include "uart1.h"
#include "ic.h"

int main(void)
{
    HAL_Init();                         /* 初始化HAL库 */
    stm32_clock_init(RCC_PLL_MUL9);     /* 设置时钟, 72Mhz */
    led_init();                         /* 初始化LED灯 */
    uart1_init(115200);
    printf("hello world!\r\n");
    ic_init(65536 - 1, 72 - 1);

    while(1)
    { 
        pressed_time_get();
        delay_ms(10);
//        led1_on();
//        led2_off();
//        delay_ms(500);
//        led1_off();
//        led2_on();
//        delay_ms(500);
    }
}


  • **ic_init(65536 - 1, 72 - 1);//计时1us**语句定时参考

在这里插入图片描述

  • ic.c程序
#include "ic.h"
#include "stdio.h"
#include "string.h"

struct
{
    uint8_t succeed_flag;
    uint8_t rising_flag;
    uint8_t falling_flag;
    uint16_t timout_cnt;
} capture_status = {0};

uint16_t last_cnt = 0;

TIM_HandleTypeDef ic_handle = {0};
void ic_init(uint16_t arr, uint16_t psc)
{
    TIM_IC_InitTypeDef ic_config = {0};
    
    ic_handle.Instance = TIM2;
    ic_handle.Init.Prescaler = psc;
    ic_handle.Init.Period = arr;
    ic_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_IC_Init(&ic_handle);
    
    ic_config.ICPolarity = TIM_ICPOLARITY_FALLING;
    ic_config.ICSelection = TIM_ICSELECTION_DIRECTTI;
    ic_config.ICPrescaler = TIM_ICPSC_DIV1;
    ic_config.ICFilter = 0;
    HAL_TIM_IC_ConfigChannel(&ic_handle, &ic_config, TIM_CHANNEL_2);
    __HAL_TIM_ENABLE_IT(&ic_handle, TIM_IT_UPDATE);
    HAL_TIM_IC_Start_IT(&ic_handle, TIM_CHANNEL_2);
}

void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM2)
    {
        GPIO_InitTypeDef gpio_initstruct;
        //打开时钟
        __HAL_RCC_GPIOA_CLK_ENABLE();                           // 使能GPIOB时钟
        __HAL_RCC_TIM2_CLK_ENABLE();
        
        //调用GPIO初始化函数
        gpio_initstruct.Pin = GPIO_PIN_0;                    // 两个LED对应的引脚
        gpio_initstruct.Mode = GPIO_MODE_AF_PP;             // 推挽输出
        gpio_initstruct.Pull = GPIO_PULLUP;                     // 上拉
        gpio_initstruct.Speed = GPIO_SPEED_FREQ_HIGH;           // 高速
        HAL_GPIO_Init(GPIOB, &gpio_initstruct);
        
        HAL_NVIC_SetPriority(TIM2_IRQn, 2, 2);
        HAL_NVIC_EnableIRQ(TIM2_IRQn);
    }
}

void TIM2_IRQHandler(void)
{
    HAL_TIM_IRQHandler(&ic_handle);
}

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    //printf("捕获到下降沿\r\n");
    if(htim->Instance == TIM2)
    {
        if(capture_status.succeed_flag == 0)
        {
            if(capture_status.falling_flag == 1)
            {
                printf("捕获到上升沿\r\n");
                capture_status.succeed_flag = 1;
                last_cnt = HAL_TIM_ReadCapturedValue(&ic_handle, TIM_CHANNEL_2);
                TIM_RESET_CAPTUREPOLARITY(&ic_handle, TIM_CHANNEL_2);
                TIM_SET_CAPTUREPOLARITY(&ic_handle, TIM_CHANNEL_2, TIM_ICPOLARITY_FALLING);
                //memset(&capture_status, 0, sizeof(capture_status));
            }
            else
            {
                printf("捕获到下降沿\r\n");
                memset(&capture_status, 0, sizeof(capture_status));
                capture_status.falling_flag = 1;
                __HAL_TIM_DISABLE(&ic_handle);
                __HAL_TIM_SET_COUNTER(&ic_handle, 0);//定时器清零
                TIM_RESET_CAPTUREPOLARITY(&ic_handle, TIM_CHANNEL_2);
                TIM_SET_CAPTUREPOLARITY(&ic_handle, TIM_CHANNEL_2, TIM_ICPOLARITY_RISING);
                __HAL_TIM_ENABLE(&ic_handle);
            }
        }
    }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM2)
    {
        if(capture_status.succeed_flag == 0)
        {
            if(capture_status.falling_flag == 1)
                capture_status.timout_cnt++;
        }
    }
}

void pressed_time_get(void)
{
    if(capture_status.succeed_flag == 1)
    {
        printf("按下时间:%d us\r\n", capture_status.timout_cnt * 65536 + last_cnt);
        memset(&capture_status, 0, sizeof(capture_status));
    }
}

  • ic.h程序
#ifndef __IC_H__
#define __IC_H__

#include "sys.h"
void ic_init(uint16_t arr, uint16_t psc);
void pressed_time_get(void);

#endif

3.实物效果

  • 硬件模块接线
    KEY一端—>PA0
    KEY另一端—>GND
    ST-Link下载方式
    打开串口软件,当按键按下时,串口打印输出“捕获到下降沿”;当按键松开时,串口打印输出“捕获到上升沿”;并打印“按下时间:”

测量按键按下时间keil源文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值