🛠 Intro to motor control with PWM
Objectifs
This tutorial guides you through configuring the HPM5100 PWM module to control the speed of a DC motor. You will learn to utilize the 32 available PWM channels and understand the principle of Pulse Width Modulation. This is an excellent introduction to the motor management capabilities of the RISC-V microcontroller.
Matériel nécessaire
- HPMicro HPM5100 board (HPM5151).
- A small DC motor (3V-6V) or a high-power LED to simulate the load.
- A MOSFET transistor or motor driver for isolation.
- Connecting cables (Dupont).
- HPM SDK v1.13.0 installed and RISC-V toolchain.
Étapes
``c pwm_config_t config = { .period = 20000, // Example period .pulse = 10000, // 50% duty cycle }; pwm_set_config(HPM_PWM1, &config); pwm_enable(HPM_PWM1, PWM_CHANNEL_0, true); ``
``c while(1) { for(int duty = 0; duty <= 100; duty+=5) { pwm_set_duty(HPM_PWM1, PWM_CHANNEL_0, duty, 100); delay_ms(100); } // Reverse variation direction if necessary } ``
- Hardware Connection : Connect the PWM output pin of the HPM5100 to the Gate of the MOSFET. Connect the MOSFET drain to the motor positive pole. The MOSFET source goes to Ground (GND). The other motor pole goes to the external positive supply. Do not forget the flyback diode in parallel with the motor.
- Project Configuration : Open your IDE (e.g., VS Code with HPM plugin) and create a new project based on the
drivers/pwmtemplate. - PWM Initialization : In your C code, initialize the PWM peripheral. The HPM5100 has synchronized timers. Configure the switching frequency (e.g., 20 kHz to avoid audible noise).
- Main Loop : Dynamically modify the
pulsevalue to change speed. Use a variable that increases and decreases progressively. - Compile and Flash : Compile the firmware with
makeand flash the board via USB.
Vérification
Observe the motor. It should start slowly, accelerate to maximum speed, then slow down. If using an LED, it should brighten gradually without visible flickering. If available, check with an oscilloscope that the signal is square and stable at 20 kHz.
Pour aller plus loin
Explore advanced features like complementary mode for H-bridges, or use FreeRTOS to create a task for speed control regulated by feedback via the integrated 16-bit ADCs.