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

Inverting PWM in nRF52840 pinn

Hello,

I want to output in one pin 400kHz and inverse of 400kHz in the other pin. I have somehow managed to output the the 400kHz but haven't managed to output its inverse.

Here is my code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>
#include "nrf_drv_pwm.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "boards.h"
#include "bsp.h"
#include "app_timer.h"
#include "nrf_drv_clock.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#define m_top 20
#define m_step 1
#define led_external 26
#define led_reversed 27
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I was looking into the previous answers but I wasn't able to still apply their solution into my program. From what I know so far is that "NRFX_PWM_PIN_INVERTED" only works in idle state and that "seq_values->channel_0 = duty_cycle | 0x8000" is a possible answer but I am not sure how to integrate that in my program. 

Asking for help/advice how to integrate 400kHz output in pin 0.26 and inverse 400kHz frequency in pin 0.27 of nRF52840 board thru modifying my code?

  • The decoder load mode decides how a sequence is read from ram and spread to the compare register, so it is not related to duty cycle. The duty cycle is decided by compare value in sequence and top value. 

    You want to run 400KHZ 80% duty PWM on Pin26, and reverse output on Pin27, one solution is:

    Fullscreen
    1
    2
    3
    4
    NRF_PWM_CLK_4MHz //4M/10=400KHZ
    sequence_values[]={2, 2|0x8000,0,10}; // (10-2)/10=80%
    NRF_PWM_LOAD_WAVE_FORM//(See nrf52840 ps section 6.17.1)
    NRF_PWM_MODE_UP //(See nrf52840 ps section 6.17.1)
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Here are the codes with the above settings:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "app_timer.h"
    #include "nrf_drv_clock.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #define led_external 26
    #define led_reversed 27
    static nrfx_pwm_t m_pwm0 = NRFX_PWM_INSTANCE(0);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

1 2 3