NRF52832 更改占空比

How to modify the PWM value with easydma for NRF52???

Zkernel

Zkernelover 2 years ago

now,I want to use a serial port to transmit data, to change the duty and cycle of PWM .

  • Ole Bauck

    Ole Bauck over 2 years ago

    This code will setup PWM on one pin. You can update the duty cycle between 0 and m_top:

    static uint16_t const m_top  = 10000;
    
    static nrf_pwm_values_common_t m_seq_values;
    static nrf_pwm_sequence_t const m_seq =
    {
        .values.p_common    = &m_seq_values,
        .length              = NRF_PWM_VALUES_LENGTH(m_seq_values),
        .repeats             = 0,
        .end_delay           = 0
    };
    
    static void pwm_init(void)
    {
        uint32_t err_code;
        nrf_drv_pwm_config_t const config0 =
        {
            .output_pins =
            {
                BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
                NRF_DRV_PWM_PIN_NOT_USED, // channel 1
                NRF_DRV_PWM_PIN_NOT_USED, // channel 2
                NRF_DRV_PWM_PIN_NOT_USED  // channel 3
            },
            .irq_priority = APP_IRQ_PRIORITY_LOW,
            .base_clock   = NRF_PWM_CLK_1MHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = m_top,
            .load_mode    = NRF_PWM_LOAD_COMMON,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        err_code = nrf_drv_pwm_init(&m_pwm0, &config0, NULL);
        APP_ERROR_CHECK(err_code);
        m_used |= USED_PWM(0);
    }
    
    void update_pwm(int16_t duty_cycle)
    {
        m_seq_values = duty_cycle;
        nrf_drv_pwm_simple_playback(&m_pwm0, &m_seq, 1, 0);
    }
    
    • Zkernel

      Zkernel over 2 years ago in reply to Ole Bauck

      Thank you very much, duty than I can be changed. now ,i want change the cycle?

        typedef struct
      {
          uint16_t period  ;
          nrf_pwm_values_grouped_t  P_groups; 
      } nrf_pwm_running_values; 
      
      static nrf_pwm_running_values nrf_pwm={10000,{100,100|0x8000}};
      
      
       static nrf_pwm_sequence_t const pwm0_seq =
          {
              .values.p_grouped = &nrf_pwm.P_groups,
              .length           = 2,
              .repeats          = 0,
              .end_delay        = 0
          };
      

      This is my definition of a cycle and duty cycle of the structure, when I serial interface to modify the structure,but cycle has not changed.

      static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
      {
          for (uint32_t i = 0; i < length; i++)
          {
              while(app_uart_put(p_data[i]) != NRF_SUCCESS);
              SEGGER_RTT_printf(0,"UART DATA %02X \n\n",p_data[i]) ; 
          }
          nrf_pwm.period=20000;
          nrf_pwm.P_groups.group_0=200;
          nrf_pwm.P_groups.group_1=200|0x8000;
          nrf_drv_pwm_simple_playback(&m_pwm0, &pwm0_seq, 1,
          NRF_DRV_PWM_FLAG_LOOP);
          while(app_uart_put('\n') != NRF_SUCCESS);
      }
      
    • Ole Bauck

      Ole Bauck over 2 years ago in reply to Ole Bauck

      then you have to set the load to waveform (see product specification for how this works):

      .load_mode    = PWM_DECODER_LOAD_WaveForm
      

      And use nrf_pwm_values_wave_form_t struct for the values.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值