cancel
Showing results for 
Search instead for 
Did you mean: 

3-Phase generation via timers...

jason22
Associate II
Posted on May 17, 2012 at 02:36

Does anyone know how to produce a solid 3-phase implementation via the timers on these STM32F4-Discovery? I'm wanting to be able to produce a simple 3-phase digital signal upon 3 output channels are varying frequencies from low Hz to nothing more than 1 MHz. So, nothing crazy, but the part I'm wondering about mainly is how to 'best' produce the 3-phased signals upon the 3 output channels. Any ideas guys?!?

Thanks in advance!!!

#3-phase-timer-stm32f4discovery
4 REPLIES 4
bjfree
Associate II
Posted on May 17, 2012 at 02:50

I believe DM00037051.pdf would cover what you want.

page 29

do a google

I apologize, I have not ported my 56F103 code over yet.

jason22
Associate II
Posted on May 17, 2012 at 03:27

Ok, I was thinking that, for instance, TIM1, all its channels had to be at the same time base, but apparently not, its 4 channels can have different prescalers. Still much to consider, but thanks for the guidance!

jason22
Associate II
Posted on May 19, 2012 at 04:19

Ok,

I got it all working now, the 3 phase generation, it was a bit easier than I had thought, amazing what a few days can do lol!

Not only that, but I can change the frequency of the 3 phase signal on the fly via a usb connection to my pc, and also have a nice lcd where the current frequency is displayed. All controlled by this awesome STM32F4-Discovery!!!

Thanks guys!
jason22
Associate II
Posted on May 19, 2012 at 05:08

If anyone is interested in generating 3 phase digital signals, you merely have to make sure that you use compare values that are divisible by 3 exactly, to ensure perfect 3 phase generation, in addition, pattern your ARR accordingly...

Here are some key snippets below...

snippet from timer.c:

----------------------------------------------------------------------------------------------

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

__IO uint16_t CCR1_Val = 65534;

__IO uint16_t CCR2_Val = 43689;

__IO uint16_t CCR3_Val = 21844;

uint16_t PrescalerValue = 0;

/* Private function prototypes -----------------------------------------------*/

void Setup3PhaseTimer(void);

void NVICConfig(void);

/* Private functions ---------------------------------------------------------*/

void Setup3PhaseTimer(void)

{

    NVICConfig();

    /* Turn on LED4, LED3, LED5 and LED6 */

    STM32F4_Discovery_LEDOn(LED4);

    STM32F4_Discovery_LEDOn(LED3);

    STM32F4_Discovery_LEDOn(LED5);

    PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 250000) - 1;

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period = 65534;

    TIM_TimeBaseStructure.TIM_Prescaler = 0;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* Prescaler configuration */

    TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);

    /* Output Compare Timing Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);

    /* Output Compare Timing Mode configuration: Channel2 */

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

    TIM_OC2Init(TIM3, &TIM_OCInitStructure);

    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);

    /* Output Compare Timing Mode configuration: Channel3 */

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

    TIM_OC3Init(TIM3, &TIM_OCInitStructure);

    TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);

    /* TIM Interrupts enable */

    TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3, ENABLE);

    /* TIM3 enable counter */

    TIM_Cmd(TIM3, ENABLE);

}

void NVICConfig(void)

{

    NVIC_InitTypeDef NVIC_InitStructure;

    /* TIM3 clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* Enable the TIM3 gloabal Interrupt */

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

}

--------------------------------------------------------------------------------------------

snippet from stm32f4xx_it.c:

--------------------------------------------------------------------------------------------

void TIM3_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

    STM32F4_Discovery_LEDToggle(LED4);

  }

  else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

    STM32F4_Discovery_LEDToggle(LED3);

  }

  else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);

    STM32F4_Discovery_LEDToggle(LED5);

  }

}

---------------------------------------------------------------------------------------------