Lập trình PWM STM32 CUBEMX
STEP1:Cài đặt timer 1 internal clock
STEP2:Cấu hình PWM OUTPUT
STEP3:Cài đặt thông số PWM
72MHz / [(7199+1)*(499+1)]=20Hz
STEP4:Cấu hình thạch anh 72Mhz
STEP 5:CODE Biên dịch
- intmain(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1*/
- /* MCU Configuration———————————————————-*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM2_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
- HAL_TIM_PWM_Start(&htim1 TIM_CHANNEL_4);
- __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, 250);
- /* USER CODE END 2*/
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while(1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3*/