微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32f407的TIM1的中断和PWM

stm32f407的TIM1的中断和PWM

时间:11-10 来源:互联网 点击:


Prescaler = (TIMxCLK / TIMx counter clock) - 1;
Period = (TIMx counter clock / TIM3 output clock) - 1
TIMx counter clock为你所需要的TXM的定时器时钟
*/
TIM_TimeBaseStructure.TIM_Period = 10000-1; //查数据手册可知,TIM1与TIM8为16位自动装载
/*在system_stm32f4xx.c中设置的APB2 Prescaler = 2 ,可知
*APB1时钟为168M/2*2,因为如果APB1分频不为1,则定时时钟x2
*/
TIM_TimeBaseStructure.TIM_Prescaler = 16800-1; //分频为10K
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

/*配置输出比较,产生占空比为20%的PWM方波*/
TIM_OCStructInit(&TIM_OCInitStructure); //填入缺省值,其中后几项只对TIM1和TIM8有效
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1为正常占空比模式,PWM2为反极性模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出允许
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补输出允许
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //High为占空比高极性,此时占空比为20%;Low则为反极性,占空比为80%
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//互补输出,与以上相反
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

TIM_OCInitStructure.TIM_Pulse = 2000; //输入通道1 CCR1(占空比数值)
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //Ch1初始化
TIM_OCInitStructure.TIM_Pulse = 4000;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 7000;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//CCR自动装载默认也是打开的
TIM_ARRPreloadConfig(TIM1, ENABLE); //ARR自动装载默认是打开的,可以不设置

TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1定时器
TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM1的PWM输出,TIM1与TIM8有效
}

/*************************************************
Function: void NVIC_Config(void)
Description: 嵌套中断断配置函数
Input: 无
Output: 无
*************************************************/
void NVIC_Config(void)
{
/*配置定时器TIM1中断嵌套*/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中断优先级分组为1
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; //嵌套中断通道为TIM1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断
NVIC_Init(&NVIC_InitStructure);
}

/*************************************************
Function: void Delay(__IO uint32_t nTime)
Description: 精确延时函数,时基根据前面设定,当前
为1ms
Input: 需要延时的时间,单位ms
Output:无
*************************************************/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}

/*************************************************
Function: void TimingDelay_Decrement(void)
Description: SysTick中断服务函数,加在_it.h中的
void SysTick_Handler(void)函数内
Input: 无
Output:无
*************************************************/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}

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

网站地图

Top