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

Outputting a PWM signal and it's inverse thru gpio pins

Hello,

I have two questions:

How do I access the gpio pins of nrf52840 dk? what example program from nrf5 sdk can I use as example?

Is there a simpler example program to output a pwm signal of example like 400kHz and it's inverse in the pins?I'm looking at the PWM driver example of nrf5 sdk and it's a really big leap for someone like me who's use to programming via Arduino IDE

Parents
  • Hello,

    How do I access the gpio pins of nrf52840 dk? what example program from nrf5 sdk can I use as example?

    The description of the application above sounds very similar to the functionality demonstrated in our PWM Library example from the SDK. I highly recommend checking out this example, to see how you may go about setting up two inverted PWM's using the PWM library provided in the SDK.

    pwm signal of example like 400kHz

    Unfortunately, the pwm library instances are limited to 200 kHz. If you need 400 kHz, you will need to make use of the PWM peripheral. You could see how this can be used in the PWM driver example - however, this example might seem a little intense for someone who's most used to Arduino development. The example contains multiple demons, which in turn makes it seem daunting to familiarize with.

    To make things easier, you could instead have a look at the project I have included below, which is made for the nRF52840 DK. This project is the same as the one referenced by my colleague Torbjørn in this ticket. It showcases a much simpler approach to setting up the PWM peripheral, with a much less complex output. Once you get the hang of this example and how it works, you can begin looking at modifying it for your application needs.
    Mind you that this example uses the legacy nrf_drv driver, but in never SDK's this is using the nrfx driver behind the scenes through forwarding macros.
    2627.pwm_driver_simple.zip
    You should have a look at the nrfx_pwm API Reference when you start modifying the example.

    I'm looking at the PWM driver example of nrf5 sdk and it's a really big leap for someone like me who's use to programming via Arduino IDE

    No worries at all - we've all been new to this at some point! :) 
    Please do not hesitate to ask, if you should encounter any issues or questions.

    Best regards,
    Karl

  • 2627.pwm_driver_simple.zip

    Hello, in the file you sent me, what does PWM_TOP_VALUE do?

Reply Children
  • Hello,

    refer_pin said:
    in the file you sent me, what does PWM_TOP_VALUE do?

    The COUNTERTOP is explained in the section immediately following Figure 2 in the PWM peripheral documentation.
    In essence, the COUNTERTOP value determines how high the counter will count, before being reset. As you can see in the aforementioned Figure 2, the counter is reset to 0 when it hits the counter top value.

    An exempt from the documentation reads:

    The counter is automatically reset to zero when COUNTERTOP is reached and OUT[n] will invert. OUT[n] is held low if the compare value is 0 and held high if set to COUNTERTOP, given that the polarity is set to FallingEdge. Counter running in up mode results in pulse widths that are edge-aligned.

    Thus, the COUNTERTOP, PRESCALER and COMPARE values is what determines the shape of the PWM waveform.
    The PRESCALER value determines how often the counter will be incremented ( for example, a prescaler value of 16 means that the COUNTER will increment 1 for every 16th clock pulse ).
    The COMPARE value determines when the PWM output signal should be high - effectively setting the duty cycle - which again is relative to the value of the COUNTERTOP.
    So, with COMPARE value of 0.5 * COUNTERTOP, you will have a 50% duty cycle.
    Does this make sense? Please let me know if any part of this should still be unclear.

    Best regards,
    Karl

Related