cancel
Showing results for 
Search instead for 
Did you mean: 

How to display the value of GPIO_ReadPin quickly and correctly on STM32F103C8T6 ?

BangDuLzGG
Associate II

Dear Members,

How can the speed of displaying the value of HAL_GPIO_ReadPin when reading a pwm signal be equivalent to the speed of displaying the value in another variable located outside WHILE ?

I made a code to read the pwm signal to find out whether the pwm signal is in high mode or low mode using HAL_GPIO_ReadPin. W
hen mode is high it will send a value of 1 and when mode is low it will send a value of 0.

 

I tested it by reading a 1Hz frequency which has a duty cycle that changes every second starting from 10%, 30%, 50% and I set a delay of 100ms on WHILE.


When I looked at the code output with UART, I saw that the time value in high mode for each duty cycle that the program read was correct.

 

However, the number of values 1 appearing when reading one duty cycle does not correspond to the size of the duty cycle being read. Here are the results of the code output:

 

DUTY CYCLE 10%

[10:01:46:124] 1C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:225] 1C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:326] 1C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:427] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:528] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:629] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:730] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:831] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:46:932] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

[10:01:47:033] 0C | Ton_ms: 00099.90 | Ton_us: 99899.98 ␍␊

 

DUTY CYCLE 30%

[10:01:47:134] 1C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:235] 1C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:336] 1C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:437] 1C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:538] 1C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:639] 0C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:740] 0C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:841] 0C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:47:942] 0C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

[10:01:48:043] 0C | Ton_ms: 00299.92 | Ton_us: 299922.22 ␍␊

 

DUTY CYCLE 50%

[10:01:48:144] 1C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:245] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:346] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:447] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:548] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:649] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:750] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:851] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:48:952] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

[10:01:49:053] 0C | Ton_ms: 00499.94 | Ton_us: 499944.41 ␍␊

In the output display above, it is found that 1C is displayed as follows:
• Duty cycle 10% = 1C appears 3x
• Duty cycle 30% = 1C appears 5x
• Duty cycle 50% = 1C appears 1x


The output should display 1C as much as:
• Duty cycle 10% = 1C appears 1x
• Duty cycle 30% = 1C appears 3x
• Duty cycle 50% = 1C appears 5x


So, the appearance of the 1C status is not correct for every type of duty cycle that exists.

 

How do I ensure that the appearance of 1C matches each duty cycle reading that the code is reading?

 

This is the STM32CUBEIDE program that I made.

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */


uint32_t IC_direct_mode_periode = 0;
uint32_t IC_indirect_mode_Ton   = 0;
uint32_t nilai_prescaler        = 1999;
uint32_t untuk_rumus_frekuensi  = 18000;
float    untuk_rumus_waktu_Ton  = 0.017998;

float    duty_cycle   = 0;
float    frekuensi 	  = 0;
float    waktu_Ton_us = 0;
float    waktu_Ton_ms = 0;
float    waktu_Ton_s  = 0;
char     status[1000];

uint32_t    status_pin3;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
  HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

	  //BACA SINYAL PWM 3 DI PIN A3
	  if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) == GPIO_PIN_SET)
		 {
		  status_pin3 = 1;
		 }

		  if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) == GPIO_PIN_RESET)
			{
			  status_pin3 = 0;
			}
			/* USER CODE END WHILE */
			  sprintf(status, "%luC | Ton_ms: %08.2f | Ton_us: %08.2f \r\n", status_pin3, waktu_Ton_ms, waktu_Ton_us);
			  HAL_UART_Transmit_IT(&huart1, status, strlen(status));

			  frekuensi  = 0;
			  duty_cycle = 0;

			  HAL_Delay(100);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief TIM3 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_TIM3_Init(void)
{

  /* USER CODE BEGIN TIM3_Init 0 */

  /* USER CODE END TIM3_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};

  /* USER CODE BEGIN TIM3_Init 1 */
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 0;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 65535;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
  sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
  sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sSlaveConfig.TriggerFilter = 0;
  if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
  sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
  if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */

  /* USER CODE END TIM3_Init 2 */

}

/**
  * @brief USART1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pins : Baca_Sinyal_3_XOR_Pin Baca_Sinyal_1_Pin Baca_Sinyal_2_Pin */
  GPIO_InitStruct.Pin = Baca_Sinyal_3_XOR_Pin|Baca_Sinyal_1_Pin|Baca_Sinyal_2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
		  if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
		  {

			//mendapatkan nilai input capture dari channel 1 yang merupakan direct mode untuk periode
			IC_direct_mode_periode = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

			if (IC_direct_mode_periode != 0)
			{
				//mendapatkan nilai input capture dari channel 2 yang merupakan indirect mode untuk Ton
				IC_indirect_mode_Ton = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

				//rumus frekuensi
				frekuensi = (float) untuk_rumus_frekuensi / IC_direct_mode_periode;

			  if (frekuensi > 500) //jika frekuensi lebih dari 500Hz, maka
			  	  {
			        nilai_prescaler 	  = 0;        //ubah prescaler menjadi 0 untuk pembacaan frekuensi lebih tinggi
			        untuk_rumus_frekuensi = 36000000; //ubah rumus menjadi 36Mhz untuk pembacaan frekuensi lebih tinggi
			        untuk_rumus_waktu_Ton = 35.95;	  //ubah rumus menjadi 35.95 untuk pembacaan frekuensi lebih tinggi
			      }

			  	  else //jika frekuensi tidak lebih dari 500Hz, maka
			  	  {
			        nilai_prescaler 	  = 1999;     //ubah prescaler menjadi 1999 untuk pembacaan frekuensi lebih rendah
			        untuk_rumus_frekuensi = 18000;    //ubah rumus menjadi 18Khz untuk pembacaan frekuensi lebih rendah
			        untuk_rumus_waktu_Ton = 0.017998; //ubah rumus menjadi 0.017998 untuk pembacaan frekuensi lebih rendah
			      }

			  TIM3->PSC = nilai_prescaler; //nilai prescaler akan sama dengan isi "nilai_prescaler"

			  //rumus duty cycle
			  duty_cycle = (float)(IC_indirect_mode_Ton * 100) / IC_direct_mode_periode;

			  //rumus waktu Ton "HIGH" dalam detik
			  waktu_Ton_us = IC_indirect_mode_Ton / untuk_rumus_waktu_Ton;

			  //rumus waktu Ton "HIGH" dalam milidetik
			  waktu_Ton_ms = waktu_Ton_us / 1000;

			  //rumus waktu Ton "HIGH" dalam mikrodetik
			  waktu_Ton_s = waktu_Ton_ms / 1000;

			}

			else
			{
			  duty_cycle = 0;
			  frekuensi = 0;
			  waktu_Ton_us = 0;
			  waktu_Ton_ms = 0;
			}
		  }
}


/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @PAram  file: pointer to the source file name
  * @PAram  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 




1 ACCEPTED SOLUTION

Accepted Solutions

You can use a TIM CH1/CH2 to record period and duty/phase in TIM count units via PWM Input Capture mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2
Sarra.S
ST Employee

Hello @BangDuLzGG, thank you for your question 

For a 1Hz PWM signal, a 100ms delay in your while loop is too long 

also, Instead of using HAL_GPIO_ReadPin in a while loop with a delay, consider using timer interrupts to capture the PWM signal

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

You can use a TIM CH1/CH2 to record period and duty/phase in TIM count units via PWM Input Capture mode.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..