now,I want to use a serial port to transmit data, to change the duty and cycle of PWM .
now,I want to use a serial port to transmit data, to change the duty and cycle of PWM .
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);
}
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);
}
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.