码迷,mamicode.com
首页 > 其他好文 > 详细

定时器TIM,pwm

时间:2019-06-02 00:58:54      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:性能   reset   config   更新   com   lock   int   def   bsp   

一、定时器

1.     定义

设置等待时间,到达后则执行指定操作的硬件。

 

2.    STM32F407的定时器有以下特征

具有基本的定时功能,也有PWM输出(灯光控制、电机的转速)、脉冲捕获功能(红外捕捉)

 

2个高级控制定时器、10个通用定时器和2个基本定时器

 

高级控制定时器(TIM1和TIM8)

  具有16位定时器功能,也具有PWM输出高级控制功能

通用定时器(TIM2到TIM5)

  具有16位定时功能,也具有PWM输出控制功能

通用定时器(TIM9到TIM14)

  具有16位定时功能,也具有PWM输出控制功能

基本定时器(TIM6和TIM7)

  具有16位定时功能

 

二、定时器初始化

 

1.   选择时钟源,并初始化定时器分频值与定时时间

 

/* TIM3 clock enable ,定时器3时钟使能*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
/* Time base configuration ,定时器基本配置*/
TIM_TimeBaseStructure.TIM_Period = (10000/1000)-1;                           //定时时间的设置[非常重要],中断频率为1000Hz,也就是定时时间为1ms
TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;                             //预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;              //时钟分频,也称之为二次分频,当前是1分频,说白了不分频,不降低频率
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;        //向上计数的方法
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                               //初始化

 

涉及部分寄存器参考如下

技术图片

 

技术图片

 

技术图片

 

 

1.         中断的配置

 

/* TIM Interrupts enable,使能定时器3更新中断事件,也代表说定时已经到达的事件 */
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
 
/* TIM3 enable counter,使能定时器3工作 */
TIM_Cmd(TIM3, ENABLE);

 

2.         中断服务函数的编写

 

void TIM3_IRQHandler(void)
{
         static uint32_t cnt=0;
         
         //是否已经有更新中断事件
         if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
         {
                   //添加用户代码
                   cnt++;
 
                   if(cnt>=500)
                   {
                            cnt =0;
                            PFout(9)^=1;
                   
                   }
                   //清空标志位,告诉CPU我已经完成中断处理
                   TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
         }

}

 

 TIM3

#include <stdio.h>
#include "stm32f4xx.h"
#include "sys.h"

GPIO_InitTypeDef            GPIO_InitStructure;

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
NVIC_InitTypeDef         NVIC_InitStructure;


void tim3_init(void)
{
    /* TIM3 clock enable ,定时器3时钟使能*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* Enable the TIM3 gloabal Interrupt ,使能定时器3全局中断*/
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


    /* Time base configuration ,定时器基本配置*/
    TIM_TimeBaseStructure.TIM_Period = (10000/1000)-1;            //定时时间的设置[非常重要],中断频率为1000Hz,也就是定时时间为1ms
    TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;                //预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;        //时钟分频,也称之为二次分频,当前是1分频,说白了不分频,不降低频率
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    //向上计数的方法
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                //初始化

    /* TIM Interrupts enable,使能定时器3更新中断事件,也代表说定时已经到达的事件 */
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    /* TIM3 enable counter,使能定时器3工作 */
    TIM_Cmd(TIM3, ENABLE);

}


int main(void)
{

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

    /* 配置PF9引脚为输出模式 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                    //第9根引脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                //设置输出模式
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                //推挽模式,增加驱动电流
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;            //设置IO的速度为100MHz,频率越高性能越好,频率越低,功耗越低
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;            //不需要上拉电阻
    GPIO_Init(GPIOF, &GPIO_InitStructure);


    PFout(9)=1;
    
    //定时器3初始化
    tim3_init();
    
    while(1)
    {


    }

}

void TIM3_IRQHandler(void)
{
    static uint32_t cnt=0;
    
    //是否已经有更新中断事件
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
    {
        //添加用户代码
        cnt++;
        
        if(cnt>=500)
        {
            cnt =0;
            
            PFout(9)^=1;
        
        }
        
        //清空标志位,告诉CPU我已经完成中断处理
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }

}

 

tim1238_irq

#include "stm32f4xx.h"
#include "stdio.h"
#include "bitband.h"

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;

void tim1_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    //定时时间为100ms  168000000/16800=10000Hz 
    TIM_TimeBaseStructure.TIM_Period = (10000/10)-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 16800-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM1, ENABLE);

}

void tim2_init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    //定时时间为200ms 84000000/84000=10000Hz
    TIM_TimeBaseStructure.TIM_Period = (10000/5)-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM2, ENABLE);

}

void tim3_init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    //定时时间为500ms 84000000/84000=10000Hz
    TIM_TimeBaseStructure.TIM_Period = (10000/2)-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

}

void tim8_init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
    
    NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_TIM13_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    //定时时间为2秒  168000000/168000=10000Hz
    TIM_TimeBaseStructure.TIM_Period = (10000*2)-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 16800-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM8, ENABLE);

}

int main(void)
{
    
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOE, ENABLE);
    
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9|GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOF, &GPIO_InitStructure);
        
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13|GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    PFout(9) = 1;
    PFout(10) = 1;
    PEout(13) = 1;
    PEout(14) = 1;
    
    tim1_init();
    tim2_init();
    tim3_init();
    tim8_init();

    while(1)
    {    
            
    }
}

void TIM1_UP_TIM10_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
    {

        PFout(9) ^= 1;
        
        TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
    }
}

void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {

        PFout(10) ^= 1;
        
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}

void TIM3_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {

        PEout(13) ^= 1;
        
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}

void TIM8_UP_TIM13_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)
    {

        PEout(14) ^= 1;
        
        TIM_ClearITPendingBit(TIM8, TIM_IT_Update);
    }
}

 

定时器TIM,pwm

标签:性能   reset   config   更新   com   lock   int   def   bsp   

原文地址:https://www.cnblogs.com/xiangtingshen/p/10961427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!