cancel
Showing results for 
Search instead for 
Did you mean: 

TIM4 CCR1 + CCR2

dhandzic
Associate II
Posted on December 16, 2007 at 11:02

TIM4 CCR1 + CCR2

1 REPLY 1
dhandzic
Associate II
Posted on May 17, 2011 at 12:20

Hi,

I want to use TIM4 with CCR1 and CCR2 to generate pulse for two independent stepper motors running att different speed. Everything works as long as not both motors are running at the same time att different speeds.

My initialisation sequence:

TIM4_ARR = 0xFFFF;

TIM4_PSC = PRESCALE - 1;

TIM4_CCER &= ~0x1111; // Disable all channels

TIM4_CCMR1_OUTPUT_COMPARE_MODE = 0x0000;

TIM4_SR = 0;

TIM4_CCER |= 0x0011; // Enable channels 1 + 2

Irq_28_to_31_Priority |= 0x00000000; // set priority 0

Irq_0_to_31_Set_Enable |= 0x40000000; // enable irq 30

TIM4_DIER = 0;

TIM4_CR1 = 0x0001; // enable TIM4

rewriting registers in the interrupt routine:

Motor1

if (((TIM4_SR & 0x0002) != 0x00) && ((TIM4_DIER & 0x0002) != 0x00)){

GPIOC_BSRR = 0x0004;

capture1 = TIM4_CCR1; // Update capture register for next edge

TIM4_CCR1 = capture1 + motor1.steptime;

TIM4_SR &= ~0x0002;

GPIOC_BRR = 0x0004;

}

Motor 2

if (((TIM4_SR & 0x0004) != 0x00) && ((TIM4_DIER & 0x0004) != 0x00)){

GPIOC_BSRR = 0x0020;

capture2 = TIM4_CCR2; // Update capture register for next edge

TIM4_CCR2 = capture2 + motor2.steptime;

TIM4_SR &= ~0x0004;

GPIOC_BRR = 0x0020;

}

With both motors running att different speed there are long pauses in the pulse train. However during short periods of time (ms) both are running as they should. I am puzzled.