2
votes

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);
}
1
This pulses are used to work with a sensor. 130 is the best but datasheet says , after 129th clock, sensor is ready to receive SI pulse .Anyway ,with 130 still SI wasn't in a right position to CLK.I checked it.Mahtab
yes its normal because : TIMxCLK= SYSCLK/(prescaler+1)Mahtab
yes just the comment was wrong and i changed it.Mahtab
Your comment is still wrong, the maximum frequency for APB1 is 36 MHz and you wrote that it is 72 MHz. The timer clock is twice the frequency of APB1 in this case (see reference manual section 7.2: otherwise, they are set to twice (×2) the frequency of the APB domain to which the timers are connected). However I don't know why your program does not work correctly.Étienne
I read the manual again you are right just the comment was wrong . i corrected it again. but you know its not my problem issue because frequencies are correct and i checked them with oscope. my real problem is position of the si . but thanks for your attention dear Etienne.Mahtab

1 Answers

2
votes

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.