Mahtab
Mahtab

Reputation: 107

Using STM32 Timer

I am working on the timer of STM32. I need two pulses like in the picture.

enter image description here

Frequency of CLK is 50 kHz and frequency of SI is 381.67 Hz. I need the SI to go low before the rising edge of next clock pulse but in the logic analyzer of Keil I saw the SI was in a different position relatively to CLK for each pulse, like shown on below picture. How can I fix it?

enter image description here

void Init_TIMER(void)
{
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure2, TIM_BaseInitStructure3, TIM_BaseInitStructure4;
    TIM_OCInitTypeDef TIM_OCInitStructure2, TIM_OCInitStructure3;

    TIM_DeInit(TIM2);
    TIM_DeInit(TIM3);
    TIM_DeInit(TIM4);

    TIM_InternalClockConfig(TIM2);
    TIM_InternalClockConfig(TIM3);
    TIM_InternalClockConfig(TIM4);

    /************************************************/
    /********TIMER2**********************************/
    /************************************************/
    TIM_BaseInitStructure2.TIM_Period = 180; //180/9000000=..ms 50KHZ                                                                                                                                                                   
    TIM_BaseInitStructure2.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure2.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure2.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM2, & TIM_BaseInitStructure2);

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    TIM_OCInitStructure2.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure2.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure2.TIM_Pulse = 90;
    TIM_OCInitStructure2.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC2Init(TIM2, & TIM_OCInitStructure2);
    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);

    /*****************************************************/
    /********TIMER3***************************************/
    /*****************************************************/
    TIM_BaseInitStructure3.TIM_Period = 23580; //23580/9000000=... ms  381.67HZ    50KHZ/131=381.67HZ                                                                                                                                                                   
    TIM_BaseInitStructure3.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure3.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure3.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM3, & TIM_BaseInitStructure3);

    TIM_ClearFlag(TIM3, TIM_FLAG_Update);

    TIM_OCInitStructure3.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure3.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure3.TIM_Pulse = 50;
    TIM_OCInitStructure3.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM3, & TIM_OCInitStructure3);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

    /********************************************************/
    /********TIMER4 to make sampling frequency **************/
    /*******************************************************/
    TIM_BaseInitStructure4.TIM_Period = 90; //90/9000000=0.01ms    100KHZ                                                                                                                                                               
    TIM_BaseInitStructure4.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure4.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure4.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM4, & TIM_BaseInitStructure4);
    TIM_ARRPreloadConfig(TIM4, DISABLE);
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);

    /**********************************************/

    TIM_ARRPreloadConfig(TIM2, ENABLE);

    TIM_Cmd(TIM2, ENABLE);

    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}

void GPIO_Configuration()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /*PA1  TIM2_CH2*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, & GPIO_InitStructure);
    /*PA7  TIM3_CH2*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, & GPIO_InitStructure);
}

Upvotes: 2

Views: 3699

Answers (1)

picoworm
picoworm

Reputation: 310

I had similar project where I had used taos TSL1402R matrix. Instead of timer I used an SPI with circular DMA. DMA buffer was 0x0, 0x0.....0x0, 0x1.

SI was connected to SPI MISO, And CLK was connected to SPI_CLK.

Hope this will help.

Upvotes: 2

Related Questions