微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > TIM的配置

TIM的配置

时间:11-26 来源:互联网 点击:
TIM_DeInit(TIM1); //复位定时器1所有寄存器


TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频数为0,不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方式为顺序计数模式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //配置时钟分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循环计数次数值

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //用以上参数初始化定时器时间基础模块


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = Pulse;//127;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC1Init(TIM1, &TIM_OCInitStructure);


TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 5;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

//清中断
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ClearFlag(TIM2, TIM_FLAG_Update);

//TIM1中断源设置,开启相应通道的捕捉比较中断



TIM_Cmd(TIM1, ENABLE); //使能定时器1



TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能定时器1的PWM输出

}

void TIM2_Configuration (uint Period)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//用以上参数初始化定时器时间基础模块

TIM_DeInit(TIM2); //复位定时器1所有寄存器

TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频数为0,不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数方式为顺序计数模式,增大型
//TIM_TimeBaseStructure.TIM_Period = 5625-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_Period = Period-1;//设置计数器溢出后的重载初值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //配置时钟分隔值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //循环计数次数值

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//清中断
TIM_ClearFlag(TIM2, TIM_FLAG_Update);

//TIM1中断源设置,开启相应通道的捕捉比较中断
TIM_ITConfig(//使能或者失能指定的TIM中断
TIM2, //TIM2
TIM_IT_Update|//TIM 中断源
TIM_IT_Trigger,//TIM 触发中断源
ENABLE//使能
);

TIM_Cmd(TIM2, ENABLE); //使能定时器1

}

void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

#ifdefVECT_TAB_RAM//中断向量表存放位置选择

NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //中断向量表存放于RAM中0x00
#else

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);//中断向量表存放于FLASH中0x00
#endif

//中断NVIC设置:允许中断,设置优先级//中断向量设置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置优先级分组:先占优先级2位,从优先级2位
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//更新事件
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//允许中断
NVIC_Init(&NVIC_InitStructure);//写入设置

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top