Skip to main content

[STM32] How to configure Timer 1, Channel 3 is PWM 1kHz, duty cycle 20% to control BLDC motor

 To configure Timer1 for a 1 kHz PWM signal with a 20% duty cycle on an STM32H7S3L8 microcontroller, follow these steps:

1. Understand the Timer Parameters

  • PWM Frequency: 1 kHz → Period = 1 ms.
  • Duty Cycle: 20% → ON time = 0.2 ms.

The timer’s clock frequency is derived from the APB clock (e.g., TIMCLK). Let’s assume you know the APB clock frequency. The Timer prescaler and auto-reload register (ARR) define the PWM frequency.

2. Compute Timer Parameters

Formula:

PWM Frequency=Timer Clock(Prescaler + 1) * (ARR + 1)\text{PWM Frequency} = \frac{\text{Timer Clock}}{\text{(Prescaler + 1) * (ARR + 1)}}

For 1 kHz PWM:

ARR=Timer ClockPWM Frequency∗(Prescaler + 1)−1ARR = \frac{\text{Timer Clock}}{\text{PWM Frequency} * (\text{Prescaler + 1)}} - 1

  • Prescaler: Adjust it to reduce the timer clock to a manageable range.
  • ARR: Defines the PWM period.
  • CCR1 (Compare Register): Controls the duty cycle.

Example: If TIMCLK = 200 MHz:

  • Choose Prescaler = 199 (to divide TIMCLK to 1 MHz).
  • ARR = 999 (to set a 1 kHz PWM frequency).
  • CCR1 = ARR∗Duty Cycle=999∗0.2=199ARR * \text{Duty Cycle} = 999 * 0.2 = 199.

3. Code Example

Using HAL in STM32CubeIDE:

IOC 


#include "main.h"

void PWM_Config(void)
{
    TIM_HandleTypeDef htim1;
    TIM_OC_InitTypeDef sConfigOC = {0};

    // Initialize Timer1
    __HAL_RCC_TIM1_CLK_ENABLE(); // Enable TIM1 clock

    htim1.Instance = TIM1;
    htim1.Init.Prescaler = 199;           // Prescaler to divide clock to 1 MHz
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 999;              // ARR value for 1 kHz PWM
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    htim1.Init.RepetitionCounter = 0;
    HAL_TIM_PWM_Init(&htim1);

    // Configure PWM mode for Channel 1
    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 199;                // Duty cycle = 20%
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);

    // Start PWM on Channel 1
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
}


Comments

Popular posts from this blog

[CanadaLife] Example of answers for final test in Smart Service Ontario 2024

If you have failed the final exam in the first attempt. Congratulations, you are not odd. Here, take a look and grab some corrected answers for your next attempt.  Don't waste your money and time for more failure.  Good luck bros. Quick note: remember that, the system will change the bunch of questions after each attempt. Then your next questionnaire will be different compared with the first trial. ========================================== =============FINAL TEST=================== ========================================== TIP: Here is a list of questions you did not answer correctly. *Please note: You will only be able to view this list immediately following your test attempt. ========================================== Alcohol slows down the central nervous system and impacts how a person thinks, acts, and moves. This means alcohol is a: depressant ========================================== Alcohol is metabolized in the body at a set rate. For most people, that rate is: one...

[AI] How to pass ATS (Applicant Tracking Systems)

  When it comes to passing through the initial screening by AI (Applicant Tracking Systems or ATS), it's important to optimize your resume for the specific keywords and formatting preferences that these systems use. Here are some tips: Use Relevant Keywords: Identify keywords from the job description and include them in your resume. ATS systems look for matches between the job requirements and your resume content. Keyword Placement: Integrate keywords naturally throughout your resume, especially in the skills, experience, and education sections. However, avoid keyword stuffing; ensure the language remains clear and coherent. Simple Formatting: Stick to a clean and straightforward format. Avoid complex designs, tables, or graphics that may confuse the ATS. Use standard fonts and keep the layout consistent. Use Standard Headings: ATS systems are designed to recognize specific sections. Use standard headings such as "Education," "Skills," "Experience," an...