STM32 TIM1配置为PWM2模式输出并赋值重复计数器TIM1->RCR,无法产生准确的脉冲,在计数器第一次溢出时就进入中断
[mw_shl_code=c,true]void TIM1_PWM_Init(void)
{
RCC->APB2ENR|=1<<11; //TIM1 时钟使能
RCC->APB2ENR|=1<<6; //使能 PORTE 时钟
GPIOE->CRH&=0XFF0FFFFF; //PE13输出
GPIOE->CRH|=0X00B00000; //PE13复用功能输出
RCC->APB2ENR|=1<<0; //开启辅助时钟
AFIO->MAPR&=0XFFFFFF3F; //清除 MAPR 的[12]
AFIO->MAPR|=3<<6; //部分重映像,TIM1_CH3->
E13
TIM1->ARR=49; //设定计数器自动重装值
TIM1->
SC=71; //预分频器分频设置
TIM1->RCR=10;
TIM1->CCMR2|=7<<4; //CH3 PWM2 模式
TIM1->CCMR2|=1<<3; //CH3 预装载使能
TIM1->CCER|=1<<9; //OC3 低电平有效
TIM1->CCER|=1<<8; //OC3 输出使能
TIM1->CCR3=10;
TIM1->BDTR|=1<<15;
TIM1->CR1=0x0080; //ARPE 使能
TIM1->CR1|=0x01; //使能定时器 1
TIM1->DIER|=1<<0; //允许更新中断
MY_NVIC_Init(1,3,TIM1_UP_IRQn,2);//抢占 1,子优先级 3,组 2
}
void TIM1_UP_IRQHandler(void)
{
if(TIM1->SR&0X0001) //更新中断
{
TIM1->CR1&=~(1<<0);
TIM1->SR=0;
}
} [/mw_shl_code]
一周热门 更多>