我配置TIM2的通道输出PWM波形,用寄存器操作的,但是怎么输出一直是个高电平,求高手指点,谢谢!配置程序如下: void PWM_Timer2_Init(void) { GPIO_Pin_Init(PORTA,GPIO_Pin_3,GPIO_MODE_AF_PP,GPIO_PULLUP, GPIO_SPEED_HIGH,AF1); TIM_TypeDef *Timer = TIM2; /* Enable the Low Speed APB (APB1) peripheral clock */ RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN; //__TIM2_CLK_ENABLE() /* Timer2 control register 1 (TIMx_CR1) */ Timer->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS); // Clear direcion and center aligned mode bits Timer->CR1 |= TIM_COUNTERMODE_CENTERALIGNED1; Timer->CR1 &= ~TIM_CR1_CKD; // clesr clock division bits Timer->CR1 |= TIM_CLOCKDIVISION_DIV1; // division = 1 // Timer2 prescaler (TIMx_PSC) Timer->PSC = TIM2_PRESCALER_VALUE; // initialize Timer2 prescale value = 1 // TIM2 capture/compare mode register 4 (TIMx_CCMR2) Timer->CCMR2 &= ~TIM_CCMR2_CC4S; // Capture/Compare Channel4 is configured as output // Timer->CCMR2 &= ~TIM_CCMR2_OC4PE; // Output Compare Channel4 Preload disabled, TIMx_CCR1 can be written at anytime Timer->CCMR2 |= TIM_CCMR2_OC4PE; // Output Compare 4 preload enable Timer->CCMR2 &= ~TIM_CCMR2_OC4M; // clear Output compare 4 mode Timer->CCMR2 |= TIM_CCMR2_OC4M_1; // OC1M: Output compare 4 mode = 6, PWM mode 1 Timer->CCMR2 |= TIM_CCMR2_OC4M_2; Timer->CCER |= TIM_CCER_CC4P; Timer->CCMR2 &= ~TIM_CCMR2_OC4FE; Timer->CR2 &= ~TIM_CR2_OIS1; Timer->CCMR2 |= TIM_CCMR2_OC4CE; // Output Compare Ch4 Clear Enable // TIM2 auto-reload register (TIM2_ARR) // initialize PWM frequency Timer->ARR = 699; // initialize Timer2 Auto Reload Register // TIM2 capture/compare register 4 (TIM2_CCR4) // initialize duty cycle Timer->CCR4 = 200; // initialize Timer2 Capture/Compare Register 4 // TIM2 control register 1 (TIMx_CR1) Timer->CR1 |= TIM_CR1_CEN; // Timer2 enable // TIM2 capture/compare enable register (TIMx_CCER) Timer->CCER = Timer->CCER |= TIM_CCER_CC4E; } |
评分
查看全部评分