hpm5100_pwm_motor_control.ino

HPMicro HPM5100: 400 MHz RISC-V MCU with Integrated Analog Peripherals · File : hpm5100_pwm_motor_control.ino · Language : C · Use case : Générer un signal PWM pour contrôler la vitesse d'un moteur via un pont en H.

Configures the PWM module to generate a 20 kHz signal with a 50% duty cycle.

Uses the dedicated PWM controller of the HPM5100. The reload calculation depends on the AXI clock frequency. Ideal for motor control or LED lighting. Verify the mapping of physical PWM pins to the chosen software channel.

hpm5100_pwm_motor_control.ino

#include "board.h"
#include "hpm_pwm_drv.h"
#include "hpm_clock_drv.h"

#define PWM_CHANNEL 0
#define PWM_FREQ_HZ 20000
#define PWM_DUTY_CYCLE 50

void init_pwm(void)
{
    pwm_config_t pwm_config;
    pwm_channel_config_t channel_config;
    uint32_t period;

    clock_add_to_group(clock_pwm0, 0);
    
    pwm_get_default_config(&pwm_config);
    pwm_config.clk_src = pwm_clk_src_axi;
    pwm_config.reload = (clock_get_frequency(clock_pwm0) / PWM_FREQ_HZ) - 1;
    pwm_init(HPM_PWM0, &pwm_config);

    pwm_channel_get_default_config(&channel_config);
    channel_config.mode = pwm_mode_edge_aligned;
    pwm_channel_init(HPM_PWM0, PWM_CHANNEL, &channel_config);
    
    period = pwm_config.reload;
    pwm_channel_set_duty_cycle(HPM_PWM0, PWM_CHANNEL, period * PWM_DUTY_CYCLE / 100);
    pwm_channel_enable(HPM_PWM0, PWM_CHANNEL, true);
    pwm_enable(HPM_PWM0, true);
}

void main(void)
{
    board_init();
    init_pwm();
    while (1) {
        /* Dynamic duty cycle adjustment can be added here */
        board_delay_ms(1000);
    }
}

← Back to the board