I am working on the timer of STM32. I need two pulses like in the picture.
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?
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);
}
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