NRF52芯片硬件设备初探之—PWM

PWM设备

  • 什么是PWM
  • PWM寄存器及结构
  • PWM驱动程序
    • PWMx使能(x=0,1,2)
    • PWMx关闭(x=0,1,2)
    • PWMx配置(x=0,1,2)
    • PWMx通道输出脚设置(x=0,1,2)
    • PWMx占空比序列设置(x=0,1,2)
  • 典型应用代码示例
  • 参考网站

什么是PWM

PWM — Pulse width modulation,脉宽调制器。
PWM可以输出任意宽度的脉冲方波,三个PWM硬件,每个PWM硬件支持4路独立的输出通道。每个通道可以独立设置高低有效电平和占空比,并且可以在每个周期后还可以改变高低有效电平,占空比及PWM时钟频率。通过DMA可以输出任意占空比任意高低有效电平的波形数据。
NRF52芯片硬件设备初探之—PWM_第1张图片

PWM寄存器及结构

NRF52芯片硬件设备初探之—PWM_第2张图片

PWM驱动程序

PWMx使能(x=0,1,2)

NRF_PWMx->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);

PWMx关闭(x=0,1,2)

NRF_PWMx->ENABLE = (PWM_ENABLE_ENABLE_Disabled << PWM_ENABLE_ENABLE_Pos);

PWMx配置(x=0,1,2)

NRF_PWMx->PRESCALER = base_clock;
NRF_PWMx->MODE = mode;
NRF_PWMx->COUNTERTOP = top_value;

PWMx通道输出脚设置(x=0,1,2)

NRF_PWMx->PSEL.OUT[i] = out_pins[i]; //i = 0, 1, 2, 3

PWMx占空比序列设置(x=0,1,2)

NRF_PWMx->SEQ[n].PTR = (uint32_t)p_values; //n = 0, 1
NRF_PWMx->SEQ[n].CNT = length; //n = 0, 1
NRF_PWMx->SEQ[n].REFRESH = refresh; //n = 0, 1
NRF_PWMx->SEQ[n].ENDDELAY = end_delay; //n = 0, 1
NRF_PWMx->SEQ[n].PTR = (uint32_t)p_values; //n = 0, 1

典型应用代码示例

//PWM初始化
nrfx_err_t nrfx_err;

nrfx_pwm_config_t config = NRFX_PWM_DEFAULT_CONFIG;
config.output_pins[0] = pin;
for (uint8_t i = 1; i < NRF_PWM_CHANNEL_COUNT; i++)
{
    config.output_pins[i] = NRFX_PWM_PIN_NOT_USED;
}
config.irq_priority = APP_IRQ_PRIORITY_LOW;
config.base_clock = (nrf_pwm_clk_t)PWM_TRANSMITTER_BASE_CLOCK;
config.count_mode = (nrf_pwm_mode_t)NRF_PWM_MODE_UP;
config.top_value = (uint16_t)top_value;
config.load_mode = (nrf_pwm_dec_load_t)NRF_PWM_LOAD_COMMON;
config.step_mode = (nrf_pwm_dec_step_t)NRF_PWM_STEP_AUTO;

nrfx_err = nrfx_pwm_init(&m_pwm, &config, pwm_event_handler);
if (nrfx_err != NRFX_SUCCESS)
{
    return NRF_ERROR_INVALID_STATE;
}

nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_set(pin); 

//PWM通过DMA输出波形
nrf_pwm_sequence_t       m_current_sequence = {0};
m_current_sequence.values = (nrf_pwm_values_t) {.p_common = &pwm_vals[0]};
m_current_sequence.length = bits_count;
m_current_sequence.repeats = 0;
m_current_sequence.end_delay = 0;
UNUSED_RETURN_VALUE(nrfx_pwm_simple_playback(&m_pwm, &m_current_sequence, 1, PWM_TRANSMITTER_SEQUENCE_FLAGS));    
//PWM回调函数

static void pwm_event_handler(nrfx_pwm_evt_type_t event)
{
switch (event)
{
case NRFX_PWM_EVT_FINISHED:
{
break;
}
case NRFX_PWM_EVT_END_SEQ0:
case NRFX_PWM_EVT_END_SEQ1:
{
break;
}
case NRFX_PWM_EVT_STOPPED:
{
break;
}
}
}

参考网站

1: http://www.nordicsemi.com/
2: https://infocenter.nordicsemi.com/index.jsp
3: https://devzone.nordicsemi.com/

你可能感兴趣的:(技术)