2018年4月9日 星期一

Nordic nRF52832 PWM driver程式介紹

nRF52832 有三個PWM模組,每個模組有4各channels,所以總共有12各channels可以使用,每支GPIO都可以設定為PWM。以下圖是PWM模組的方塊圖。
PWM 方塊圖
Wave Counter有兩種模式,分別為PWM up counter與PWM up and down counter。主要是產生duty-cycle pulses,如下兩圖示。
PWM up counter
PWM up and down counter
Decoder with EasyDMA,分成四種模式Common、Grouped、Single、WaveForm,如下圖示。
Decoder
以下是SDK的PWM範例程式
static void demo5(void)
{
    NRF_LOG_INFO("Demo 5");

    /*
     * This demo, similarly to demo1, plays back a sequence with different
     * values for individual channels. Unlike demo 1, however, it does not use
     * an event handler. Therefore, the PWM peripheral does not use interrupts
     * and the CPU can stay in sleep mode.
     * The LEDs (1-4) blink separately. They are turned on for 125 ms each,
     * in counterclockwise order (looking at the board).
     */

    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    = 15625,
        .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,      0,      0,      0 },
        {      0, 0x8000,      0,      0 },
        {      0,      0, 0x8000,      0 },
        {      0,      0,      0, 0x8000 }
    };
    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範例程式參數設定介紹
.output_pins 設定4組PWM channel的輸出PIN。
.base_clock設定基頻,NRF_PWM_CLK_16MHz、NRF_PWM_CLK_8MHz、NRF_PWM_CLK_4MHz、NRF_PWM_CLK_2MHz、NRF_PWM_CLK_1MHz、NRF_PWM_CLK_500kHz、NRF_PWM_CLK_250kHz、NRF_PWM_CLK_125kHz。可設定這8種頻率。
.count_mode設定Wave Counter模式,NRF_PWM_MODE_UP、NRF_PWM_MODE_UP_AND_DOWN。

沒有留言:

張貼留言