RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // 开启时钟
RCC->APB1RSTR |= RCC_APB1Periph_TIM2; // 复位定时器
RCC->APB1RSTR &= ~RCC_APB1Periph_TIM2;
TIM2->CR1 |= TIM_CounterMode_Up; // 计数方向
TIM2->CR1 |= TIM_OPMode_Repetitive; // 在发生更新事件时计数器不停止
TIM2->CR1 &= ~TIM_CR1_UDIS; // 产生更新事件
TIM2->CR1 &= ~TIM_CR1_URS; // 更新请求源
TIM2->CR1 |= TIM_CR1_ARPE; // ARPE使能
TIM2->PSC = (u16)(72 - 1); // 预分频
TIM2->ARR = (u16)(1000 - 1); // 周期
TIM2->CCMR1 = 0;
TIM2->CCMR2 = 0;
TIM2->CCER = 0;
TIM2->CCMR1 |= 0; // 输出比较模式
TIM2->CCMR1 |= TIM_OCMode_PWM1; // 设置输出模式
TIM2->CCMR1 |= TIM_OCClear_Disable; // 清零使能ETRF影响
TIM2->CCMR1 |= TIM_OCPreload_Disable; // 预装载使能
TIM2->CCMR1 |= TIM_OCFast_Enable; // 快速使能
TIM2->CCER |= TIM_OCPolarity_High; // 有效电平为高
TIM2->CCER |= TIM_CCx_Enable; // 允许输出
TIM2->CCR1 = 200; // 比较值
TIM2->CCMR1 |= 0 << 8; // 输出比较
TIM2->CCMR1 |= TIM_OCMode_PWM1 << 8; // 比较模式
TIM2->CCMR1 |= TIM_OCClear_Disable << 8; // 清零使能ETRF影响
TIM2->CCMR1 |= TIM_OCPreload_Disable<< 8; // 预装载使能
TIM2->CCMR1 |= TIM_OCFast_Enable << 8; // 快速使能
TIM2->CCER |= TIM_OCPolarity_High << 4; // 有效电平
TIM2->CCER |= TIM_CCx_Enable << 4; // 允许输出
TIM2->CCR2 = 400; // 比较值
TIM2->CCMR2 |= 0; // 输出比较模式
TIM2->CCMR2 |= TIM_OCMode_PWM1; // 设置输出模式
TIM2->CCMR2 |= TIM_OCClear_Disable; // 清零使能ETRF影响
TIM2->CCMR2 |= TIM_OCPreload_Disable; // 预装载使能
TIM2->CCMR2 |= TIM_OCFast_Enable; // 快速使能
TIM2->CCER |= TIM_OCPolarity_High << 8; // 有效电平为高
TIM2->CCER |= TIM_CCx_Enable << 8; // 允许输出
TIM2->CCR3 = 600; // 比较值
TIM2->CCMR2 |= 0 << 8; // 输出比较
TIM2->CCMR2 |= TIM_OCMode_PWM1 << 8; // 比较模式
TIM2->CCMR2 |= TIM_OCClear_Disable << 8; // 清零使能ETRF影响
TIM2->CCMR2 |= TIM_OCPreload_Disable<< 8; // 预装载使能
TIM2->CCMR2 |= TIM_OCFast_Enable << 8; // 快速使能
TIM2->CCER |= TIM_OCPolarity_High << 12; // 有效电平
TIM2->CCER |= TIM_CCx_Enable << 12; // 允许输出
TIM2->CCR4 = 800; // 比较值
TIM2->BDTR |= TIM_BDTR_MOE; // 主输出使能(1/8才有效)
TIM2->EGR |= TIM_EventSource_Update; // 产生一次更新事件
TIM2->CR1 |= TIM_CR1_CEN; // 开启定时器
TIM2->SR = 0; // 状态位清零
TAB对不齐,强迫症怎么活???!!! |