This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Invert PWM Behaviour

  I'm using this PWM output. Now I want to reverse PWM. What should I do? I can't find the register I need

  • I am using this code to generate the PWM

    NRF_PWM0->PSEL.OUT[0] = (first_pin << PWM_PSEL_OUT_PIN_Pos) |
    (PWM_PSEL_OUT_CONNECT_Connected <<
    PWM_PSEL_OUT_CONNECT_Pos);
    NRF_PWM0->ENABLE = (PWM_ENABLE_ENABLE_Enabled << PWM_ENABLE_ENABLE_Pos);
    NRF_PWM0->MODE = (PWM_MODE_UPDOWN_Up << PWM_MODE_UPDOWN_Pos);
    NRF_PWM0->PRESCALER = (PWM_PRESCALER_PRESCALER_DIV_1 <<
    PWM_PRESCALER_PRESCALER_Pos);
    NRF_PWM0->COUNTERTOP = (16000 << PWM_COUNTERTOP_COUNTERTOP_Pos); //1 msec
    NRF_PWM0->LOOP = (PWM_LOOP_CNT_Disabled << PWM_LOOP_CNT_Pos);
    NRF_PWM0->DECODER = (PWM_DECODER_LOAD_Common << PWM_DECODER_LOAD_Pos) |
    (PWM_DECODER_MODE_RefreshCount << PWM_DECODER_MODE_Pos);
    NRF_PWM0->SEQ[0].PTR = ((uint32_t)(seq0_ram) << PWM_SEQ_PTR_PTR_Pos);
    NRF_PWM0->SEQ[0].CNT = ((sizeof(seq0_ram) / sizeof(uint16_t)) <<
    PWM_SEQ_CNT_CNT_Pos);
    NRF_PWM0->SEQ[0].REFRESH = 0;
    NRF_PWM0->SEQ[0].ENDDELAY = 0;
    NRF_PWM0->TASKS_SEQSTART[0] = 1;

    The first waveform it generates is low level and I need high level

  • If were using the PWM driver, the answer can be found here.

    But, I see that you are not using the PWM driver, but accessing the PWM peripheral directly. In that case you need to set bit 15 in the buffer you provide in NRF_PWM0->SEQ[0].PTR to inverse the polarity.

    Snippet:

    int16_t buf[] = {(1 << 15) | 1500}; // Inverse polarity (bit 15), 1500us duty cycle
    NRF_PWM0->SEQ[0].PTR = (uint32_t)&buf[0];
    
     

    See the example here.

Related