PWM
Introduction
The MikroSDK.Driver.PWM
library provides a robust API for controlling Pulse Width Modulation (PWM) peripherals on microcontrollers. This documentation covers the essential functions, data structures, and examples needed to effectively use the PWM driver in your projects.
PWM is a technique used to encode a signal into a pulsing sequence, often used for controlling motors, LEDs, and other devices requiring precise control over power delivery. The MikroSDK.Driver.PWM library simplifies the integration of PWM functionality by providing a standardized API to set frequencies, duty cycles, and manage the state of the PWM module.
API Name
- MikroSDK.Driver.PWM
API Files
-
GitHub Repository:
Prerequisites
-
Library Manager:
- Select Driver.PWM to add the API to your project using the Library Manager in NECTO Studio.
-
Headers:
- Include the Driver.PWM header in your source files to access the PWM functions.
- This header pops up in the lower right part of NECTO Studio once you perform the previous task (while selecting Driver.PWM in Library Manager)
- Alternatively, include the drv_pwm.h header in your source files to access the GPIO output functions.
-
CMakeLists:
- You do not have to perform anything, configuration (CMake) file is already taken care of automatically while you performed the first task in this Prerequisites!
- You can ensure that your CMakeLists.txt includes the necessary configurations to link against the MikroSDK.Driver.PWM library.
Code Examples
- How do I initialize the PWM driver using
drv_pwm.h
?
/* Project name:
* How do I initialize the PWM driver?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM
* using mikroSDK v2.0.
* - PWM shall be initialized, PWM pin shall be driving the
* voltage.
* Library dependencies?
* - Make sure `Driver.PWM` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
* How to test this code example?
* - Make sure you enabled necessary libraries (previous step);
* - Make sure you selected/created desired hardware setup -
* check lower right corner of NECTO's environment
* whilst being in the project;
* - Flash the code!
* - LED on mikroBUS 1 socket-based PWM pin shall blink
* infinite times (voltage on PWM pin shall be incremented
* from 0V to VCC).
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
// Error handling.
return PWM_ERROR;
}
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
// No error
} else {
// Handle the error
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
// No error
} else {
// Handle the error
}
// Perform PWM 'blink' infinite times
while ( 1 ) {
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
}
return 0;
}
- How do I close the PWM driver?
/* Project name:
* How do I close the PWM driver?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, and finally closing the PWM driver shall be called.
* Library dependencies?
* - Make sure `Driver.PWM` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
* How to test this code example?
* - Make sure you enabled necessary libraries (previous step);
* - Make sure you selected/created desired hardware setup -
* check lower right corner of NECTO's environment whilst being in the project;
* - Toggle breakpoint on the line where `pwm_close` functionality is called;
* - Run debug mode by pressing F9 on your keyboard;
* - Continue debug (F6 on your keyboard) to get to the line;
* - Insert `pwm_t` PWM driver context structure in Debug Watch window;
* - Step Over (F8 on your keyboard) to see the de-allocation in action.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
#define PWM_LOOP_RANGE (5) // How many PWM "blinks" to be processed
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
// Error handling
return PWM_ERROR;
}
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
// No error
} else {
// Handle the error
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
// No error
} else {
// Handle the error
}
// Perform PWM 'blink' five times
while ( pwm_loop_counter < PWM_LOOP_RANGE ) {
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
pwm_loop_counter++;
}
// Stop PWM driver
if ( PWM_SUCCESS == pwm_stop( &pwm ) )
{
// No error
} else {
// Handle the error
}
// Close PWM driver context object
if ( PWM_SUCCESS == pwm_close( &pwm ) ) // De-allocates hardware resources for the driver object
{
// No error
} else {
// Handle the error
}
return 0;
}
- How do I reconfigure the PWM driver after it has been initialized?
/* Project name:
* How do I reconfigure the PWM driver after it has been initialized?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, closing the PWM driver
* shall be called followed by PWM reconfiguration and finally another
* opening of the PWM object shall be called.
* Library dependencies?
* - Make sure `Driver.PWM` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
* How to test this code example?
* - Make sure you enabled necessary libraries (previous step);
* - Make sure you selected/created desired hardware setup -
* check lower right corner of NECTO's environment whilst being in the project;
* - Toggle breakpoint on the line where `pwm_close` functionality is called;
* - Run debug mode by pressing F9 on your keyboard;
* - Continue debug (F6 on your keyboard) to get to the line;
* - Insert `pwm_t` PWM driver context structure in Debug Watch window;
* - Step Over (F8 on your keyboard) to see the de-allocation and allocation in action.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_UPDATED_FREQ_HZ (5000) // Updated PWM frequency in Hz
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
#define PWM_LOOP_RANGE (5) // How many PWM "blinks" to be processed
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
// Error handling
return PWM_ERROR;
}
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
// No error
} else {
// Handle the error
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
// No error
} else {
// Handle the error
}
// Perform PWM 'blink' five times
while ( pwm_loop_counter < PWM_LOOP_RANGE ) {
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
pwm_loop_counter++;
}
pwm_loop_counter = 0;
// Stop PWM driver
if ( PWM_SUCCESS == pwm_stop( &pwm ) )
{
// No error
} else {
// Handle the error
}
// Close PWM driver context object
if ( PWM_SUCCESS == pwm_close( &pwm ) ) // De-allocates hardware resources for the driver object
{
// No error
} else {
// Handle the error
}
// Reconfigure some of the PWM parameters (PWM frequency for example)
pwm_cfg.freq_hz = PWM_UPDATED_FREQ_HZ;
// It is mandatory to set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
// Now perform `pwm_open` to allocate newly configured hardware resources
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
// Error handling.
return PWM_ERROR;
}
// Update PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
// No error
} else {
// Handle the error
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
// No error
} else {
// Handle the error
}
// Perform PWM 'blink' five times
while ( pwm_loop_counter < PWM_LOOP_RANGE ) {
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
pwm_loop_counter++;
}
return 0;
}
- How do I configure the PWM to use a specific output pin?
/* Project name:
* How do I configure the PWM to use a specific output pin?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM specific output pin shall be configured
* and PWM shall be initialized.
* Library dependencies?
* - Make sure `Driver.PWM`, `Driver.GPIO.Out` and `Board` libraries
* are enabled in NECTO's Library Manager to ensure a successful build.
* How to test this code example?
* - Make sure you enabled necessary libraries (previous step);
* - Make sure you selected/created desired hardware setup -
* check lower right corner of NECTO's environment whilst being in the project.
*/
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
// Error handling.
return PWM_ERROR;
}
return 0;
}
- How can I check if the PWM driver is properly initialized?
/* Project name:
* How can I check if the PWM driver is properly initialized?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized as well to be able to track proper allocation for PWM.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
* How to test this code example?
* - Make sure you enabled necessary libraries (previous step);
* - Make sure you selected/created desired hardware setup -
* check lower right corner of NECTO's environment whilst being in the project;
* - Run debug mode by pressing F9 on your keyboard;
* - Continue debug (F6 on your keyboard);
* - Monitor the `Application Output` in the lower part of the NECTO's environment
* to check proper initialization of an PWM driver.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
log_printf( &logger, "PWM start successful.\n" );
} else {
log_printf( &logger, "PWM start failed.\n" );
}
// Perform PWM 'blink' infinite times
while ( 1 ) {
log_printf( &logger, "Increasing PWM duty cycle to 100%% ...\n" );
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
log_printf( &logger, "PWM duty cycle configured to 0%% ...\n" );
}
return 0;
}
- How do I reset the PWM to its default configuration?
/* Project name:
* How do I reset the PWM to its default configuration?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - Logging (printing characters) shall be initialized, PWM configuration
* structure shall be initialized to default values.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
return 0;
}
- How do I check if the PWM pin is set?
/* Project name:
* How do I check if the PWM pin is set?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - Logging (printing characters) shall be initialized, checking if the PWM
* pin is set shall be conducted.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Check if the PWM pin is set
if (pwm_cfg.pin != HAL_PIN_NC) {
// Output pin is set
} else {
pwm_cfg.pin = MIKROBUS_1_PWM;
}
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
return 0;
}
- How do I set the PWM frequency?
/* Project name:
* How do I set the PWM frequency?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized, proper PWM frequency shall be configured.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
return 0;
}
- How do I start the PWM module?
/* Project name:
* How do I start the PWM module?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized, start of the PWM module shall be conducted.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
log_printf( &logger, "PWM start successful.\n" );
} else {
log_printf( &logger, "PWM start failed.\n" );
}
return 0;
}
- How do I set the PWM duty cycle?
/* Project name:
* How do I set the PWM duty cycle?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized, PWM duty cycle shall be configured.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
log_printf( &logger, "PWM start successful.\n" );
} else {
log_printf( &logger, "PWM start failed.\n" );
}
// Perform PWM 'blink' infinite times
while ( 1 ) {
log_printf( &logger, "Increasing PWM duty cycle to 100%% ...\n" );
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
log_printf( &logger, "PWM duty cycle configured to 0%% ...\n" );
}
return 0;
}
- How do I stop the PWM module?
/* Project name:
* How do I stop the PWM module?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized, PWM duty cycle shall be configured, stopping
* of the PWM driver shall be conducted.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (100) // PWM duty dividend value
#define PWM_LOOP_RANGE (5) // How many PWM "blinks" to be processed
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
log_printf( &logger, "PWM start successful.\n" );
} else {
log_printf( &logger, "PWM start failed.\n" );
}
// Perform PWM 'blink' five times
while ( pwm_loop_counter < PWM_LOOP_RANGE ) {
log_printf( &logger, "Increasing PWM duty cycle to 100%% ...\n" );
while ( pwm_duty < PWM_DUTY_MAX ) {
pwm_set_duty( &pwm, (float)pwm_duty / PWM_DUTY_MAX );
Delay_ms(10);
pwm_duty++;
}
pwm_duty = 0;
pwm_loop_counter++;
log_printf( &logger, "PWM duty cycle configured to 0%% ...\n" );
}
// Stop PWM driver
if ( PWM_SUCCESS == pwm_stop( &pwm ) )
{
// No error
} else {
// Handle the error
}
log_printf( &logger, "PWM driver stopped ...\n" );
return 0;
}
- How do I set the PWM duty cycle to 100%?
/* Project name:
* How do I set the PWM duty cycle to 100%?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - This example demonstrates the functionality of PWM using mikroSDK v2.0.
* - PWM shall be initialized, logging (printing characters)
* shall be initialized, PWM duty cycle shall be configured, configuration
* of the PWM duty cycle to 100% shall be conducted.
* Library dependencies?
* - Make sure `Driver.PWM`, `Log` and `Board` libraries are enabled in NECTO's
* Library Manager to ensure a successful build.
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_pwm.h" // API for PWM driver
#include "log.h" // API for Logger driver
#include "board.h" // Main board pin mapping
// -------------------------------------------------------------------- MACROS
// If hardware without mikroBUS socket is used, make sure to define adequate pin.
#ifndef MIKROBUS_1_PWM
#define MIKROBUS_1_PWM NC // Example: `PA0` or `MIKROBUS_5_PWM`
#endif
#define PWM_FREQ_HZ (1000) // PWM frequency in Hz
#define PWM_DUTY_MAX (1.0f) // PWM max duty cycle value
#define PWM_LOOP_RANGE (5) // How many PWM "blinks" to be processed
// ------------------------------------------------------------------ VARIABLES
pwm_t pwm; // PWM driver context structure
pwm_config_t pwm_cfg; // PWM configuration structure
static log_t logger; // Logger driver object.
static uint8_t pwm_duty;
static uint8_t pwm_loop_counter;
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
// Logger settings
log_cfg_t log_cfg; // Logger configuration structure instance
log_init( &logger, &log_cfg ); // Initialize Logger
log_printf( &logger, "Logger initialized.\n" ); // Print message
// Initializes PWM configuration structure to default values
pwm_configure_default( &pwm_cfg );
log_printf( &logger, "Configured default PWM settings.\n" );
// Set PWM pin
pwm_cfg.pin = MIKROBUS_1_PWM;
log_printf( &logger, "Selected `MIKROBUS_1_PWM` as a PWM pin.\n" );
// Set PWM frequency
pwm_cfg.freq_hz = PWM_FREQ_HZ;
log_printf( &logger, "Selected 1000 Hz for PWM frequency.\n" );
// Allocate resources for PWM module
if( PWM_ERROR == pwm_open( &pwm, &pwm_cfg )) {
log_printf( &logger, "PWM open failed.\n" );
return PWM_ERROR;
}
log_printf( &logger, "PWM open successful.\n" );
// Set PWM frequency and initialize PWM module on hardware level
if ( PWM_SUCCESS == pwm_set_freq( &pwm, pwm_cfg.freq_hz ) ) {
log_printf( &logger, "PWM set frequency successful.\n" );
} else {
log_printf( &logger, "PWM set frequency failed.\n" );
}
// Start PWM module
if ( PWM_SUCCESS == pwm_start( &pwm ) ) {
log_printf( &logger, "PWM start successful.\n" );
} else {
log_printf( &logger, "PWM start failed.\n" );
}
// Set the PWM duty cycle to 100%
pwm_set_duty( &pwm, PWM_DUTY_MAX );
return 0;
}