STM32F103C8T6 Blue Pill Timer2 4-channel PWM config

98 Views Asked by At

I'm trying to set TIM2 in PWM mode, but I'm getting weird results. CH1 to CH4 are enabled but CH3 on PA2 shows no output no matter I do. The weird thing happens when I try to remap TIM2. There are three remap options, Remap2 is the one I'm interested in:

No remap: CH1=PA0, CH2=PA1, CH3=PA2, CH4=PA3 => no output on CH3(PA2)

Remap1: CH1=PA2, CH2=PA3, CH3=PA15, CH4=PB3 => no output on CH1(PA2) and CH3(PA15)

Remap2: CH1=PA0, CH2=PA1, CH3=PB10, CH4=PB11 => no output on CH1(PA0), CH2(PA1) and CH3(PB10)

Remap3: CH1=PA15, CH2=PB3, CH3=PB10, CH4=PB11 => no output on CH2(PB3) and CH3(PB10)

All GPIO ports are configured correctly, CNF=AF and MODE=OUT. TIM2 clock = 36MHz. Tried lower clock but same result. Tried another Blue Pill but same result.

Here is the TIM2 setup I'm using:

    ldr r0, = TIM2      @ APB1 Clock - 36MHz
    
    ldr r1, = 0
    str r1, [r0, #TIMx_PSC] @ Clock/Prescaler

    ldr r1, = 256
    str r1, [r0, #TIMx_ARR] @ Count-Down value for timer

    ldr r1, = 128           @ Output Compare preload value
    str r1, [r0, #TIMx_CCR1]    @ CH1
    str r1, [r0, #TIMx_CCR2]    @ CH2
    str r1, [r0, #TIMx_CCR3]    @ CH3
    str r1, [r0, #TIMx_CCR4]    @ CH4
    
    ldr r1, = 6<<OC2M | 1<<OC2PE | 6<<OC1M | 1<<OC1PE
    str r1, [r0, #TIMx_CCMR1]   @ CH1 & CH2 PWM Mode1
    
    ldr r1, = 6<<OC4M | 1<<OC4PE | 6<<OC3M | 1<<OC3PE
    str r1, [r0, #TIMx_CCMR2]   @ CH3 & CH4 PWM Mode1
    
    ldr r1, = 1<<CC4E | 1<<CC3E | 1<<CC2E | 1<<CC1E
    str r1, [r0, #TIMx_CCER]    @ CH1 to CH4 Enable

    ldr r1, = 3<<CMS
    str r1, [r0, #TIMx_CR1]

    dsb

    ldr r1, [r0, #TIMx_CR1]
    orr r1, #1<<CEN     @ Counter Enable
    str r1, [r0, #TIMx_CR1]

Tried everything I can possible imagine based on the available documentation. This is my first question here. If you need more info I'll be glad providing it. Thanks.

0

There are 0 best solutions below