AnsweredAssumed Answered

STM32F407 Synchronised timers

Question asked by apothus on Oct 24, 2014
Latest reply on Oct 26, 2014 by apothus
I am trying to synchronise timer 3 and timer 4 to output a series of complimentary signals

I have timer 3 outputting a square wave at 1MHz and I want timer 4 to output a square wave at 62.5kHz (factor of 16). 

Timer 3 appears to be configured correctly and outputing the 1MHz signal however TIM4 is outputing a signal at ~30Mhz as just a short blip to logic 0. Not having chained timers before I am unsure of my configuration.

#define GSCK_PWM_duty  50
#define GSCK_PWM_freq  1000000
#define GSCK_prescaler 0
 
 
#define timer3_freq  84000000 / (GSCK_prescaler +                // 84,000,000
#define GSCK_PWM_count  timer3_freq / GSCK_PWM_freq - 1              // 84 0x54
#define GSCK_PWM_duty_count  ((GSCK_PWM_count +1) * GSCK_PWM_duty) / 100 - 1        // 41
 
#define XLAT_PWM_duty 50           
#define XLAT_PWM_freq GSCK_PWM_freq/16         // 62500
#define XLAT_prescaler 0
 
#define timer4_freq  GSCK_PWM_freq / (XLAT_prescaler + 1)                                               // 1,000,000
#define XLAT_PWM_count  timer4_freq / XLAT_PWM_freq - 1                                                 // 16
#define XLAT_PWM_duty_count  ((XLAT_PWM_count +1) * XLAT_PWM_duty) / 100 - 1        // 7
 
void tlc5941_init(){
    tlc5941_IO_configuration();
    GSCK_generator();
    XLAT_generator();
}
 
void tlc5941_IO_configuration(){
    GPIO_InitTypeDef GPIO_InitStruct;  
     
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
     
    //Configure greyscale clocking pin for alternate function driven by timer
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
     
    GPIO_InitStruct.GPIO_Pin = tlc5941_GSCK;
    GPIO_Init(tlc5941_IOPORT, &GPIO_InitStruct);
    GPIO_PinAFConfig(tlc5941_IOPORT, GPIO_PinSource6, GPIO_AF_TIM3);
     
    GPIO_InitStruct.GPIO_Pin = tlc5941_XLAT;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4);
     
}
     
 
void GSCK_generator(){
 
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStruct;
    TIM_OCInitTypeDef  TIM_OCStruct;
         
    //Clock config
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
     
    TIM_TimeBaseStruct.TIM_Prescaler = GSCK_prescaler;
    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
     
    TIM_TimeBaseStruct.TIM_Period = GSCK_PWM_count;
    TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStruct);
    TIM_Cmd(TIM3, ENABLE);
     
    TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_High;
     
    TIM_OCStruct.TIM_Pulse = GSCK_PWM_duty_count;
    TIM_OC1Init(TIM3, &TIM_OCStruct);   //First comparator for Output
    TIM_OC2Init(TIM3, &TIM_OCStruct);   //Second comparator to drive XLAT
     
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
 
}
 
// XLAT latch pulse timing generator
// The XLAT pulse should be sent after the 16 pulse GSCK sequence has finished a cycle
// Therefore the timing factor for XLAT slaved to GSCK mush be factor of 16 it must also
// opperate at above 25hz to ensure addequate display refresh rates
 
void XLAT_generator(){
     
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStruct;
    TIM_OCInitTypeDef  TIM_OCStruct;
     
    //Configure TIM3 CCR1 as internal trigger generator
    TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_OC2Ref);
     
    //Set input signal for TIM4 as triggerx
    TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);
    TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2); 
     
    //Start the peripheral clock
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
     
    //Configure timer 4
    TIM_TimeBaseStruct.TIM_Prescaler = XLAT_prescaler;
    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStruct.TIM_Period = XLAT_PWM_count;
    TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStruct);
    TIM_Cmd(TIM4, ENABLE);
     
    TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
     
    TIM_OCStruct.TIM_Pulse = XLAT_PWM_duty_count;
    TIM_OC2Init(TIM4, &TIM_OCStruct);
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
}

Outcomes