1.在sdk_config.h中加入宏
// PWM_ENABLED - nrf_drv_pwm - PWM peripheral driver - legacy layer
//==========================================================
#ifndef PWM_ENABLED
#define PWM_ENABLED 1
#endif
// PWM_DEFAULT_CONFIG_OUT0_PIN - Out0 pin
#ifndef PWM_DEFAULT_CONFIG_OUT0_PIN
#define PWM_DEFAULT_CONFIG_OUT0_PIN 31
#endif
// PWM_DEFAULT_CONFIG_OUT1_PIN - Out1 pin
#ifndef PWM_DEFAULT_CONFIG_OUT1_PIN
#define PWM_DEFAULT_CONFIG_OUT1_PIN 31
#endif
// PWM_DEFAULT_CONFIG_OUT2_PIN - Out2 pin
#ifndef PWM_DEFAULT_CONFIG_OUT2_PIN
#define PWM_DEFAULT_CONFIG_OUT2_PIN 31
#endif
// PWM_DEFAULT_CONFIG_OUT3_PIN - Out3 pin
#ifndef PWM_DEFAULT_CONFIG_OUT3_PIN
#define PWM_DEFAULT_CONFIG_OUT3_PIN 31
#endif
// PWM_DEFAULT_CONFIG_BASE_CLOCK - Base clock
// 16 MHz
// 8 MHz
// 4 MHz
// 2 MHz
// 1 MHz
// 500 kHz
// 250 kHz
// 125 kHz
#ifndef PWM_DEFAULT_CONFIG_BASE_CLOCK
#define PWM_DEFAULT_CONFIG_BASE_CLOCK 4
#endif
// PWM_DEFAULT_CONFIG_COUNT_MODE - Count mode
// Up
// Up and Down
#ifndef PWM_DEFAULT_CONFIG_COUNT_MODE
#define PWM_DEFAULT_CONFIG_COUNT_MODE 0
#endif
// PWM_DEFAULT_CONFIG_TOP_VALUE - Top value
#ifndef PWM_DEFAULT_CONFIG_TOP_VALUE
#define PWM_DEFAULT_CONFIG_TOP_VALUE 1000
#endif
// PWM_DEFAULT_CONFIG_LOAD_MODE - Load mode
// Common
// Grouped
// Individual
// Waveform
#ifndef PWM_DEFAULT_CONFIG_LOAD_MODE
#define PWM_DEFAULT_CONFIG_LOAD_MODE 0
#endif
// PWM_DEFAULT_CONFIG_STEP_MODE - Step mode
// Auto
// Triggered
#ifndef PWM_DEFAULT_CONFIG_STEP_MODE
#define PWM_DEFAULT_CONFIG_STEP_MODE 0
#endif
// PWM_DEFAULT_CONFIG_IRQ_PRIORITY - Interrupt priority
// Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// 0 (highest)
// 1
// 2
// 3
// 4
// 5
// 6
// 7
#ifndef PWM_DEFAULT_CONFIG_IRQ_PRIORITY
#define PWM_DEFAULT_CONFIG_IRQ_PRIORITY 6
#endif
// PWM0_ENABLED - Enable PWM0 instance
#ifndef PWM0_ENABLED
#define PWM0_ENABLED 1
#endif
// PWM1_ENABLED - Enable PWM1 instance
#ifndef PWM1_ENABLED
#define PWM1_ENABLED 1
#endif
// PWM2_ENABLED - Enable PWM2 instance
#ifndef PWM2_ENABLED
#define PWM2_ENABLED 1
#endif
// PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED - Enables nRF52 Anomaly 109 workaround for PWM.
// The workaround uses interrupts to wake up the CPU and ensure
// it is active when PWM is about to start a DMA transfer. For
// initial transfer, done when a playback is started via PPI,
// a specific EGU instance is used to generate the interrupt.
// During the playback, the PWM interrupt triggered on SEQEND
// event of a preceding sequence is used to protect the transfer
// done for the next sequence to be played.
//==========================================================
#ifndef PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED
#define PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED 0
#endif
// PWM_NRF52_ANOMALY_109_EGU_INSTANCE - EGU instance used by the nRF52 Anomaly 109 workaround for PWM.
// EGU0
// EGU1
// EGU2
// EGU3
// EGU4
// EGU5
#ifndef PWM_NRF52_ANOMALY_109_EGU_INSTANCE
#define PWM_NRF52_ANOMALY_109_EGU_INSTANCE 5
#endif
2.导入文件nrfx_pwm.c到工程中
3.引入头文件
#include "nrf_drv_pwm.h"
4.定义PWM实例
static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
5.定义变量
// This is for tracking PWM instances being used, so we can unintialize only
// the relevant ones when switching from one demo to another.
#define USED_PWM(idx) (1UL
