贴块 STM32 用高级定时器 T1 生成CH1 CH4 的PWM 的初始化程序,大家小走弯路
RCC->APB2ENR|=1<<11; //TIM1时钟使能
////////////////////////////////////////
GPIOA->CRH&=0XFFFF0FF0;//PA8 11 输出
GPIOA->CRH|=0X0000B00B;//复用功能输出
////////////////////////////////////////
TIM1->BDTR |=0xC0; //ARPE使能
TIM1->ARR=arr;//设定计数器自动重装值
TIM1->
SC=psc;//预分频器不分频
TIM1->CCMR1|=6<<4; //CH2 PWM2模式 CH1/2
TIM1->CCMR1|=1<<3; //CH2预装载使能
TIM1->CCMR2|=6<<12; //CH2 PWM2模式 CH3/4
TIM1->CCMR2|=1<<11; //CH2预装载使能
TIM1->CCER|=1<<0; //OC2 输出使能
TIM1->CCER|=1<<12; //OC2 输出使能
TIM1->CR1 |=0x80; //ARPE使能
TIM1->CR1|=0x01; //使能定时器1
TIM1->BDTR|=0x8000; //使能定时器1输出
T1 和其它的定时器不一样,T2 T3 T4 都不需要最后这一句, 而调T1时,没有最后的这个BDTR,死活不出PWM。感谢 正点原子的指导。
{
if (timer >= TIM_COUNT) return FALSE;
ptr_TIM_TypeDef treg = g_Timers[timer - 1];
//debug_printf("TIM%d_%d IOTimer::Init ", timer, tchnl);
// 检查并保护针脚
GPIO_PIN pin = g_IOTimer_Pins[(timer - 1) * 4 + tchnl];
if (CPU_GPIO_ReservePin(pin, TRUE) == FALSE) {
debug_printf("TIM%d_%d IOTimer::Init Failed %c%d was Reserved ", timer, tchnl, ('A' + (pin >> 4)), (pin & 0x0F));
return FALSE;
}
// relevant RCC register & bit
__IO uint32_t* enReg = &RCC->APB1ENR;
if ((UINT32)treg & 0x10000) enReg = &RCC->APB2ENR;
int enBit = 1 << (((UINT32)treg >> 10) & 0x1F);
if (!(*enReg & enBit)) { // not yet initialized
*enReg |= enBit; // enable timer clock
treg->CR1 = TIM_CR1_URS | TIM_CR1_ARPE; // double buffered update
treg->EGR = TIM_EGR_UG; // enforce first update
if (timer == 1 || timer == 8) {
treg->BDTR |= TIM_BDTR_MOE; // main output enable (timer 1 & 8 only)
}
}
*(__IO uint16_t*)&((uint32_t*)&treg->CCR1)[tchnl] = 0; // reset compare register
// 激活PWM通道
UINT32 mode = TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE; // WM1 mode, double buffered
if (tchnl & 1) mode <<= 8; // 1 or 3
__IO uint16_t* reg = &treg->CCMR1;
if (tchnl & 2) reg = &treg->CCMR2; // 2 or 3
*reg |= mode;
return TRUE;
}
TIM1和TIM8都需要设置那个
一周热门 更多>