2022-03-12 01:04 PM
Hi, I am trying to detect a 3v PWM frequency with my STM32, I have my live expresions setup for my variables, but the values do not budge from 0. Im sure it is something up with my code. Any help would be appreciated cheers.
2022-03-12 01:07 PM
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t ICValue = 0;
uint32_t Frequency = 0;
float Duty = 0;
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) // If the interrupt is triggered by channel 1
{
// Read the IC value
ICValue = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
if (ICValue != 0)
{
// calculate the Duty Cycle
Duty = (HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2) *100)/ICValue;
Frequency = 36000000/ICValue;
}
}
}
/* USER CODE END 0 */
2022-03-12 01:07 PM
*/
int main(void)
{
/* USER CODE BEGIN 1 */
HAL_TIM_IC_Start_IT(&htim1, TIM_CHANNEL_1); // channel 1 main
HAL_TIM_IC_Start(&htim1, TIM_CHANNEL_2); // channel 2 indirect
/* USER CODE END 1 */
2022-03-12 01:07 PM
/** 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.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 5;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 4;
RCC_OscInitStruct.PLL.PLLR = 4;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
2022-03-12 01:09 PM
The input signal is connect to my PA8 terminal of my STM32
2022-03-12 07:59 PM
Did you enable the Global interrupt of the timer you use for the capture?
I've got a working example described here: https://stm32world.com/wiki/STM32_Timer_PWM_Input_Capture
2022-03-13 07:53 AM
Ah, I think thats where I have gone wrong. I'll sort that out, if that doesn't work i'll give your example a trial. Cheers!