Hi Team,
We use stm32f303Rbt6 microcontroller. I need a output for Input capture, I am using two timers. I generate pulse for Tim1(1khz) and I set for Tim2 for input capture mode(input capture direct mode). TIM1 generate the pulse for continuously(1khz). But TIM2 set the mode for frequency capture in rising edge only. But we did not get the output and I enable the global interrupt for TIM2. But HAL_TIM_IC_CaptureCallback function does not woring my code. So we did not get the output for Frequency. So I'm already try this method to develop my code. I attach my code and I made a mistake pls correct me.
TIM1 and TIM2 Configuration Pictures.
#include "main.h"


#include "tim.h"
#include "gpio.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 ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t IC_Val1 = 0;
uint32_t IC_Val2 = 0;
uint32_t Difference = 0;
int Is_First_Captured = 0;
uint16_t i=0,hi=0;
float Ref_clock;
uint32_t Frequency=0;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) // Input_Capture Call Back Function
{
if(htim->Instance==TIM3)
{
if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2) // if first Value is not captured
{
if(Is_First_Captured==0)
{
IC_Val1 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2); // captured the first value
Is_First_Captured=1; // set the value is captured now
}
else
{
IC_Val2 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2); // captured the second value
if(IC_Val2 > IC_Val1) // if the second value is higher
{
Difference = IC_Val2 - IC_Val1;
}
else if(IC_Val2 > IC_Val1)
{
Difference = ((0xffff - IC_Val1) + IC_Val2) +1;
}
// frequency = tim2_clk/Difference
Ref_clock = 36000000/719;
Frequency = Ref_clock/ Difference;
__HAL_TIM_SET_COUNTER(htim, 0); // reset the counter
Is_First_Captured=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_TIM1_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // PWM_GENERATE_TIM1_15KHZ
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); // INPUT_CAPTURE_PWM_IT_START
HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1);
// HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1);
//HAL_Delay(10);
// HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 0);
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}