void TIM3_PWM_Init(void)
{
GPIO_Set(GPIOB,PIN5,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);
GPIO_AF_Set(GPIOB,5,2); //PB5 AF2 TIM3_CH2
TIM3->ARR=10;
TIM3->CCR2=1;
TIM3->CR2 |= 5<<4; //TRGO OC2REF
TIM3->SMCR |= 1<<7; //slave mode
TIM3->SMCR |= 1<<4; //TIM2 trigger
TIM3->SMCR |= 7<<0; //TIM2 TRGO Rising edge count+
TIM3->CCMR1|= 3<<12; //CNT==CCR2, OC2 output evert
TIM3->CCER |= 1<<4; //enable OC2
TIM3->CCER |= 0<<5; //OC2 polarity high Level effective
TIM3->DIER |= 1<<2; //enable compare2 interrupt
MY_NVIC_Init(1,3,TIM3_IRQn ,2);
TIM3->CR1 |= 1<<0;
}
void TIM3_IRQHandler(void)
{
if(TIM3->CCR2==1)
{
TIM3->CCR2=8;
}
else
{
TIM3->CCR2=1;
}
TIM3->SR &= ~(1<<2); //clear Compare 2 interrupt flag
}
CLK由TIM2產生的
原本想產生第一個CLK上升 第8個CLK下降的PWM
結果連週期都錯的....應該是10個CLK 結果變20個 duty也錯的
是不是TIMx_CCMR1 OC1M:输出比较 1 模式 (Output compare 1 mode) 011:翻转––TIMx_CNT=TIMx_CCR1 时,OC1REF 发生翻转。
不能這樣玩阿...想說在中斷修改CCR讓它在別的地方翻轉就好....
有沒有大俠知道為啥~~
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void TIM2_PWM_Init(u32 arr, u32 psc)
{
RCC->APB1ENR|=7<<0; //enable TIM2 TIM3 TIM4 clk
RCC->AHB1ENR|=1<<1; //enable PORTB clk
GPIO_Set(GPIOB,PIN10,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);
GPIO_AF_Set(GPIOB,10,1); //PB10 AF1 TIM2_CH3
TIM2->CR2=2<<4; //UI TRGO
TIM2->ARR = arr;
TIM2->PSC = psc;
TIM2->CCR3= arr*0.5; //D=0.5
TIM2->CCMR2 |= 7<<4; //PWM mode CNT<CCR output=effective Level, CNT>CCR output=invalid Level
TIM2->CCER |= 1<<8; //enable OC3 output
TIM2->CCER |= 0<<9; //OC3 polarity High Level effective
TIM2->DIER |= 1<<3; //ebable CC3I
MY_NVIC_Init(1,3,TIM2_IRQn ,2);
}
void TIM2_IRQHandler(void)
{
PCout(7)=~PCout(7); <===看示波器都在TIMCH3的上升沿之後 連續反轉2次
TIM2->SR = 0; //clear
}
一周热门 更多>