cancel
Showing results for 
Search instead for 
Did you mean: 

Pulse delay in STM32F051C6T6

FLast.5
Associate II

I am using STM32f051c6t6. I am using internal crystal clock source. I have also set Timer clock source to 24 MHz. I am writing simple program to generate low and high pulse. The 10 us pulse is not generated properly. The rise and fall times has delay of 10us which is unexpected . What could be the reason ? Could the reason be internal clock source ?

Below is my code:

static void MX_GPIO_Init(void)
 
{
 
      GPIO_InitTypeDef GPIO_InitStruct = {0};
 
      GPIO_InitStruct.Pin = SEN1_LO_Pin;
 
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
      GPIO_InitStruct.Pull = GPIO_PULLUP;	//GPIO_NOPULL;
 
      GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
 
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
}
 
 
 
static void MX_TIM2_Init(void)
 
{
  /* USER CODE BEGIN TIM2_Init 0 */
  /* USER CODE END TIM2_Init 0 */
 
       TIM_ClockConfigTypeDef sClockSourceConfig = {0}; 
       TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM2_Init 1 */
 
  /* USER CODE END TIM2_Init 1 */
 
       htim2.Instance = TIM2;
       htim2.Init.Prescaler = 24;
 
       htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
 
       htim2.Init.Period = 4294967295;
 
       htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 
       htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 
       if (HAL_TIM_Base_Init(&htim2) != HAL_OK) 
       {
              Error_Handler();
       } 
       sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
       if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
       {
              Error_Handler();
       }
       sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
       sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
       if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
       {
              Error_Handler();
       }
       /* USER CODE BEGIN TIM2_Init 2 */
       /* USER CODE END TIM2_Init 2 */
}
 
void delay_us(uint16_t us)
{
 
	__HAL_TIM_SET_COUNTER(&htim2,0); // set the counter value a 0
 
	while (__HAL_TIM_GET_COUNTER(&htim2) < us); // wait for the counter to reach the us input in the parameter
}
 
int main(void)
{
       // Other HAL initializations
       MX_GPIO_Init();
       MX_TIM2_Init();
       HAL_TIM_Base_Start(&htim2);
       while(1)
       {
                delay_us(150);
                HAL_GPIO_WritePin(SEN1_LO_GPIO_Port,SEN1_LO_Pin, GPIO_PIN_RESET);
 
                delay_us(15);  
                HAL_GPIO_WritePin(SEN1_LO_GPIO_Port,SEN1_LO_Pin, GPIO_PIN_SET);
            }
}

Below is the image0693W00000NsgKwQAJ.jpg 

What is the reason for imperfect pulse ?

Is this the problem in STM32F051C6T6 or could it be other reasons ?

3 REPLIES 3
Andrew Neil
Evangelist III

"internal crystal clock source"

There is no internal crystal clock - the internal oscillator is an RC oscillator.

"The rise and fall times has delay of 10us"

What are you measuring this delay relative to?

"Below is the image"

Doesn't your scope have a facility to capture a screenshot? That would be much clearer than a photo!

What hardware is this? Which pin are you using? What is connected to that pin?

JW

Check your oscilloscope measurment method (!) or pin load, waveforms have suspiciously "slow" edges. 10us puls with heavy RC loading should look like that you see ! First edge is time when rising start, second edge is time when fall start. That moments are 10us delayed like you expect.

BTW1: Use Timer output to generating precision pulses. Way you are using have jitter (uncertainty) few (or more) CPU ticks.

BTW2: Using internal oscillator, there will be up to 1% frequency (and pulse width) deviation due oscillator inaccuracy.