NRF的PWM信号占空比调节基本上有两种方式,一般地方讲的不清楚,在此说明下,参考程序源自nrf5_sdk_17.1.0_ddde560,代码如下:
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED, // channel 2
BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED // channel 3
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_125kHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 16000,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
m_used |= USED_PWM(0);
// This array cannot be allocated on stack (hence "static") and it must
// be in RAM (hence no "const", though its content is not changed).
static nrf_pwm_values_individual_t /*const*/ seq_values[] =
{
{ 0x8000, 0x8000, 0x8000, 0x8000 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
};
nrf_pwm_sequence_t const seq =
{
.values.p_individual = seq_values,
.length = NRF_PWM_VALUES_LENGTH(seq_values),
.repeats = 0,
.end_delay = 0
};
(void)nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1, NRF_DRV_PWM_FLAG_LOOP);
这其中包括了PWM的初始化,PWM序列的初始化和序列的播放;
第一种:用播放序列来确定占空比
static nrf_pwm_values_individual_t /*const*/ seq_values[] =
{
{ 0x8000, 0x8000, 0x8000, 0x8000 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0x8000, 0x8000, 0x8000, 0x8000 },//1,2,3,4通道输出PWM
{ 0, 0, 0, 0 },//1,2,3,4通道不输出PWM
{ 0, 0, 0, 0 },//1,2,3,4通道不输出PWM
{ 0, 0, 0, 0 },//1,2,3,4通道不输出PWM
横向代表了通道1-4,竖向代表了每个通道的四个PWM生成顺序。如此来看,每个通道的PWM输出情况就是在平均分成四份的时间上有一份时间输出PWM,剩下三份时间里不输出PWM,如下图:
| PWM | 0 | 0 | 0 |
那么占空比就是25%.示波器波形如图:
如果修改成下图的序列
{ 0x8000, 0x8000, 0x8000, 0x8000 },//1,2,3,4
{ 0x8000, 0x8000, 0x8000, 0x8000 },//
{ 0, 0, 0, 0 },//
{ 0, 0, 0, 0 },//
PWM的播放顺序就成了
| PWM | PWM | 0 | 0 |,1234通道都是一样的情况,占空比就成了50%,示波器波形如图:
第二种:调整播放序列中的值来调整占空比
这种调节方法和 .top_value = 16000,值有关系
nrf_drv_pwm_config_t const config0 =
{
.output_pins =
{
BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
BSP_LED_2 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
BSP_LED_3 | NRF_DRV_PWM_PIN_INVERTED, // channel 2
BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED // channel 3
},
.irq_priority = APP_IRQ_PRIORITY_LOWEST,
.base_clock = NRF_PWM_CLK_125kHz,
.count_mode = NRF_PWM_MODE_UP,
.top_value = 16000,
.load_mode = NRF_PWM_LOAD_INDIVIDUAL,
.step_mode = NRF_PWM_STEP_AUTO
};
APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
m_used |= USED_PWM(0);
// This array cannot be allocated on stack (hence "static") and it must
// be in RAM (hence no "const", though its content is not changed).
static nrf_pwm_values_individual_t /*const*/ seq_values[] =
{
{ 4000, 4000, 8000, 8000 },//1,2,3,4
};
如上面的代码那样,top_value值为16000,seq_values[] = { 4000, 4000, 8000, 8000 },//1,2,3,4通道
1,2通道占空比就是25%,3,4通道就是50%,1,2通道波形如图:
要注意的是,用调整播放序列的值来调整占空比时如果定义了两组序列而且值不同,也将会影响到占空比,比如:
seq_values[] =
{
{ 4000, 4000, 8000, 8000 },//1,2,3,4
{ 0, 0, 0, 0 },//1,2,3,4
}
第二个序列值为空,但是还是占用了1600个值的时间,此时12通道的占空比为12.5%,34通道为25%。1,2通道波形如图:
以上。