stm32F407的TIM定时器输出PWM波
/*配置输出比较,产生占空比为20%的PWM方波*/
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1为正常占空比模式,PWM2为反极性模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2000;//输入CCR(占空比数值)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//High为占空比高极性,此时占空比为20%;Low则为反极性,占空比为80%
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//CCR自动装载默认也是打开的
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARR自动装载默认是打开的,可以不设置
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2, ENABLE); //使能TIM2定时器
}
/*************************************************
Function: void NVIC_Config(void)
Description: 嵌套中断断配置函数
Input: 无
Output: 无
*************************************************/
void NVIC_Config(void)
{
/*配置定时器TIM2中断嵌套*/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 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--;
}
}
stm32F407TIM定时器输出PWM 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)