//TIM2 PWM,ch3,ch4部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM2_PWM_Init(u16 arr,u16 psc)
{
//此部分需手动修改IO口设置
RCC->APB1ENR |= 0x01;//TIM2时钟使能
RCC->APB2ENR |= 1<<2;//使能PORTA时钟
GPIOA->CRL &= 0XFFFF00FF; //PA2,PA3 Output mode, max speed 2 MHz.
GPIOA->CRL |= 0X0000BB00; //CNF:MODE=10:11,General purpose output push-pull
TIM2->ARR = arr;//设定计数器自动重装值
TIM2->PSC = psc;//预分频器不分频
TIM2->CCMR2 |= 7<<4; //CH3 PWM模式
TIM2->CCMR2 |= 1<<3; //CH3预装载使能
TIM2->CCMR2 |= 7<<12; //CH4 PWM模式
TIM2->CCMR2 |= 1<<11; //CH4预装载使能
TIM2->CCR3 = 1200;
TIM2->CCR4 = 1200;
TIM2->CCER |= 1<<8; //OC3 输出使能
TIM2->CCER |= 1<<12; //OC4 输出使能
TIM2->CR1 = 0x0080; //ARPE使能
TIM2->EGR |= 0x01;
TIM2->CR1 |= 0x01; //使能定时器2
}
|