hal
Hardware Abstraction Layer
pwm.h File Reference

Control for a PWM signal generated by the micro. More...

#include <stdbool.h>
#include "hal_types.h"
Include dependency graph for pwm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

hal_status_t hal_pwm_init (uint32_t pwm_frequency_hz)
 Initializes all the hardware (GPIO, TIMER, and CHANNEL) to deliver a PWM signal. More...
 
void hal_pwm_enable (bool enable)
 enable or disable the pwm signal. If disabled, nothing will show up on pins. More...
 
void hal_pwm_set_duty_cycle (uint8_t percent)
 Sets the duty cycle 0% - 100% for the PWM signal. More...
 
void hal_pwm_set_frequency (uint32_t pwm_frequency_hz)
 Sets the PWM signal frequency to a new value after the driver has already been initialized. More...
 

Detailed Description

Control for a PWM signal generated by the micro.

Note
General steps for use: 0. Choose a frequency.
  1. Initialize pwm driver (hal_pwm_init())
  2. Enable the output (hal_pwm_enable())
  3. Set duty cycle (hal_pwm_set_duty_cycle())

At this point a PWM signal of your chosen frequency and duty cycle is being output to pin.

Copyright (c) 2025 Cory McKiel. Licensed under the MIT License. See LICENSE file in the project root.

Function Documentation

◆ hal_pwm_enable()

void hal_pwm_enable ( bool  enable)

enable or disable the pwm signal. If disabled, nothing will show up on pins.

Parameters
enabletrue to enable the driver, false to disable.

◆ hal_pwm_init()

hal_status_t hal_pwm_init ( uint32_t  pwm_frequency_hz)

Initializes all the hardware (GPIO, TIMER, and CHANNEL) to deliver a PWM signal.

Parameters
pwm_frequency_hzThe frequency of the PWM signal you would like generated in Hz. eg for 20kHz -> 20,000.
Returns
HAL_STATUS_OK on success.

◆ hal_pwm_set_duty_cycle()

void hal_pwm_set_duty_cycle ( uint8_t  percent)

Sets the duty cycle 0% - 100% for the PWM signal.

Parameters
percentTakes the values 0 through 100 as integers to express the desired DC.

◆ hal_pwm_set_frequency()

void hal_pwm_set_frequency ( uint32_t  pwm_frequency_hz)

Sets the PWM signal frequency to a new value after the driver has already been initialized.

Parameters
pwm_frequency_hzThe frequency of the PWM signal you would like generated in Hz. eg for 20kHz -> 20,000.