/* apb timer clock = input clock 108mhz input clock / prescaler-1 = timer clock 108mhz/ 107 = 1mhz prescaler divides down input clock, 0 prescaler = 0 division timer clock / htim9.Init.Period-1 = pwm frequency 1mhz / 99 = 10khz htim9.Init.Period-1 = timer count before auto reload sConfigOC.Pulse = how many timer counts the output is enabled for 50 sConfigOC.Pulse/htim9.Init.Period-1 = output dutycycle, should = 0.5 for 50% duty for nHz output, prescaler = ((1,080,000)/nHz)-1 for 50hz = 21599 for 4550hz = 236~ for 1337hz = 806~ prescaler is only 16bit so min freq is 15hz~ for ch2, all the same but; */ sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; //ideally call HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_2); //at the exact same time so the counters start countign together but stuff it, close enough is good enough, if you never stop them they'll never go out of sync anymore than they start with __HAL_TIM_SET_PRESCALER(&htim9, NewPrescaler); //updates timer clock so both channels are updated simulataneously. //exmple static void MX_TIM3_Init(void) { /* USER CODE BEGIN TIM9_Init 0 */ /* USER CODE END TIM9_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; /* USER CODE BEGIN TIM9_Init 1 */ /* USER CODE END TIM9_Init 1 */ htim9.Instance = TIM9; htim9.Init.Prescaler = 107; htim9.Init.CounterMode = TIM_COUNTERMODE_UP; htim9.Init.Period = 99; htim9.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_PWM_Init(&htim9) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim9, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 50; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM9_Init 2 */ /* USER CODE END TIM9_Init 2 */ HAL_TIM_MspPostInit(&htim9); } //example pwm output is 10khz, 50% duty