基本定时器TIM6的使用 1、设置一个定时器,100ms一次去开关LED灯
2、tim6的初始化代码:
大部分代码来自官方demo /*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
*\*\file timer_common.c
*\*\author Nations
*\*\version v1.0.0
*\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
**/
#include "timer.h"
#include "main.h"
#include "type.h"
static TIM_TimeBaseInitType TIM_TimeBaseStructure;
/**
*\*\name Common_TIM_Base_Initialize.
*\*\param TIMx :
*\*\ - TIM1
*\*\ - TIM2
*\*\ - TIM3
*\*\ - TIM4
*\*\ - TIM5
*\*\ - TIM6
*\*\param period
*\*\ - [1, 0xffff]
*\*\param prescaler
*\*\ - [0, 0xffff]
*\*\return none
**/
static void Common_TIM_Base_Initialize(TIM_Module *TIMx, uint16_t period, uint16_t prescaler)
{
TIM_Base_Struct_Initialize(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.Period = period;
TIM_TimeBaseStructure.Prescaler = prescaler;
TIM_TimeBaseStructure.ClkDiv = 0;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_Base_Initialize(TIMx, &TIM_TimeBaseStructure);
}
/**
*\*\name Common_TIM_NVIC_Initialize.
*\*\param IRQ_number :
*\*\ - TIM1_BRK_IRQn
*\*\ - TIM1_UP_IRQn
*\*\ - TIM1_TRG_COM_IRQn
*\*\ - TIM1_CC_IRQn
*\*\ - TIM2_IRQn
*\*\ - TIM3_IRQn
*\*\ - TIM4_IRQn
*\*\ - TIM8_BRK_IRQn
*\*\ - TIM8_UP_IRQn
*\*\ - TIM8_TRG_COM_IRQn
*\*\ - TIM8_CC_IRQn
*\*\ - TIM5_IRQn
*\*\ - TIM6_IRQ
*\*\param command
*\*\ - ENABLE
*\*\ - DISABLE
*\*\return none
**/
static void Common_TIM_NVIC_Initialize(IRQn_Type IRQ_number, FunctionalState command)
{
NVIC_InitType NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = IRQ_number;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
if(ENABLE == command)
{
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
}
else
{
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
}
NVIC_Initializes(&NVIC_InitStructure);
}
/**
*\*\name Common_TIM_DMA_Config.
*\*\param chan_handle
*\*\param dma_param
*\*\param req_remap
*\*\ - DMA_REMAP_TIM1_CH1
*\*\ - DMA_REMAP_TIM1_CH2
*\*\ - DMA_REMAP_TIM1_CH3
*\*\ - DMA_REMAP_TIM1_CH4
*\*\ - DMA_REMAP_TIM1_COM
*\*\ - DMA_REMAP_TIM1_UP
*\*\ - DMA_REMAP_TIM1_TRIG
*\*\ - DMA_REMAP_TIM2_CH1
*\*\ - DMA_REMAP_TIM2_CH2
*\*\ - DMA_REMAP_TIM2_CH3
*\*\ - DMA_REMAP_TIM2_CH4
*\*\ - DMA_REMAP_TIM2_UP
*\*\ - DMA_REMAP_TIM3_CH1
*\*\ - DMA_REMAP_TIM3_CH3
*\*\ - DMA_REMAP_TIM3_CH4
*\*\ - DMA_REMAP_TIM3_UP
*\*\ - DMA_REMAP_TIM3_TRIG
*\*\ - DMA_REMAP_TIM4_CH1
*\*\ - DMA_REMAP_TIM4_CH2
*\*\ - DMA_REMAP_TIM4_CH3
*\*\ - DMA_REMAP_TIM4_UP
*\*\ - DMA_REMAP_TIM5_CH1
*\*\ - DMA_REMAP_TIM5_CH2
*\*\ - DMA_REMAP_TIM5_CH3
*\*\ - DMA_REMAP_TIM5_CH4
*\*\ - DMA_REMAP_TIM5_UP
*\*\ - DMA_REMAP_TIM5_TRIG
*\*\ - DMA_REMAP_TIM6_UP
*\*\ - DMA_REMAP_TIM8_CH1
*\*\ - DMA_REMAP_TIM8_CH2
*\*\ - DMA_REMAP_TIM8_CH3
*\*\ - DMA_REMAP_TIM8_CH4
*\*\ - DMA_REMAP_TIM8_COM
*\*\ - DMA_REMAP_TIM8_UP
*\*\ - DMA_REMAP_TIM8_TRIG
*\*\return none
**/
static void Common_TIM_DMA_Config(DMA_ChannelType *chan_handle, DMA_InitType *dma_param, uint32_t req_remap)
{
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_DMA);
DMA_Reset(chan_handle);
DMA_Initializes(chan_handle, dma_param);
DMA_Channel_Request_Remap(chan_handle, req_remap);
}
/**
*\*\name Common_TIM_DMA_Config.
*\*\param chan_handle
*\*\param src_addr
*\*\param dst_addr
*\*\param len
*\*\return none
**/
static void Common_TIM_DMA_Start(DMA_ChannelType *chan_handle, uint32_t src_addr, uint32_t dst_addr, uint32_t len)
{
DMA_Channel_Disable(chan_handle);
chan_handle->TXNUM = len;
if(chan_handle->CHCFG & DMA_DIR_PERIPH_DST)
{
chan_handle->PADDR = dst_addr;
chan_handle->MADDR = src_addr;
}
else
{
chan_handle->PADDR = src_addr;
chan_handle->MADDR = dst_addr;
}
DMA_Channel_Enable(chan_handle);
}
/**
*\*\name Common_BSTIM_RCC_Initialize.
*\*\param TIMx :
*\*\ - TIM6
*\*\param hclk_division
*\*\ - RCC_HCLK_DIV1
*\*\ - RCC_HCLK_DIV2
*\*\ - RCC_HCLK_DIV4
*\*\ - RCC_HCLK_DIV8
*\*\ - RCC_HCLK_DIV16
*\*\return uint32_t
**/
static MI_BOOL Common_BSTIM_RCC_Initialize(TIM_Module *TIMx, uint32_t hclk_division)
{
uint32_t BSTIM_clock;
RCC_ClocksType RCC_Clocks;
// RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA | RCC_AHB_PERIPH_GPIOB| RCC_AHB_PERIPH_GPIOC
// | RCC_AHB_PERIPH_GPIOD);
// RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
// RCC 配置 RCC_HCLK_DIV1 频率为128M RCC_HCLK_DIV4 为64M
// 多个预分频器可用于配置 AHB、高速 APB(APB2)和低速 APB(APB1)的频率。AHB、APB2 和 APB1 的最大 频率分别为 128MHz、64MHz 和 32MHz。
// APB 低速(APB1)预分频器
// 由软件置位和清零,配置 APB1 时钟(PCLK1)的分频系数。需确保 PCLK1 不 超过 32MHz。
// 0xx:HCLK 不分频
// 100:HCLK 2 分频
// 101:HCLK 4 分频
// 110:HCLK 8 分频
// 111:HCLK 16 分频
//RCC_Pclk1_Config(hclk_division);
RCC_APB1_Peripheral_Clock_Enable(RCC_APB1_PERIPH_TIM6);
RCC_Clocks_Frequencies_Value_Get(&RCC_Clocks);
return MI_TRUE;
}
// 基本定时器的使用
void timer6_init()
{
Common_BSTIM_RCC_Initialize(TIM6, RCC_HCLK_DIV1);
/* NVIC Configuration */
Common_TIM_NVIC_Initialize(TIM6_IRQn, ENABLE);
/* Time base configuration, period = 1000-1, prescaler = 6400-1 */
/* 100ms 触发中断一次 64000000 /(1000-1+1)* (6400-1+1) = 10HZ */
Common_TIM_Base_Initialize(TIM6, 1000-1, 6400-1);
TIM_Base_Reload_Mode_Set(TIM6, TIM_PSC_RELOAD_MODE_IMMEDIATE);
TIM_Interrupt_Enable(TIM6, TIM_INT_UPDATE);
TIM_On(TIM6);
}
// timer1 当作通用定时器的使用
void timer1_common_init()
{
//RCC配置
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_TIM1);
// 中断配置
Common_TIM_NVIC_Initialize(TIM1_UP_IRQn, ENABLE);
/* Time base configuration, period = 65535, prescaler = prescaler */
Common_TIM_Base_Initialize(TIM1, 1000-1, 12800-1);
TIM_Base_Reload_Mode_Set(TIM1, TIM_PSC_RELOAD_MODE_IMMEDIATE);
TIM_Interrupt_Enable(TIM1, TIM_INT_UPDATE);
TIM_On(TIM1);
}
3、中断处理函数 void TIM6_IRQHandler(void)
{
if (TIM_Interrupt_Status_Get(TIM6, TIM_INT_UPDATE) != RESET)
{
TIM_Interrupt_Status_Clear(TIM6, TIM_INT_UPDATE);
/* Pin PC.06 toggling */
GPIO_Pin_Toggle(LED2_GPIO_PORT, LED1_GPIO_PIN);
}
}
4、main.cpp引用time6_init()后,重新烧录,即可看到led1 100ms闪烁。 注意:
/* Time base configuration, period = 1000-1, prescaler = 6400-1 / / 100ms 触发中断一次 64000000 /(1000-1+1)* (6400-1+1) = 10HZ */
N32G430C8L7的时钟配置为 HCLK = 128M APB2 = 64M APB1 = 32M
|