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

  • 2627.pwm_driver_simple.zip

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

  • 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

  • Thank you for your patience.

    refer_pin said:
    I meant was how do I access for example pin P0.06 on the board?

    You may access P0.06 with the NRF_GPIO_PIN_MAP(0,6) function call.
    Please also know that P0.06 is usually used for the UART TX PIN in all the SDK examples.

    To see how this is all done in the nRF52840 DK file, you oculd take a look at the pca10056.h file located in SDK\components\boards\pca10056.h

    Best regards,
    Karl

  • refer_pin said:
    Yes, I think I'm somehow in a way grasping how pwm is generated here but like a really vague understanding to begin with

    Great! It might seem like a ton of things to familiarize with, but it gets easier fast.

    refer_pin said:
    This is something I am not good at right now and I was wondering if you could provide guidance on what possible resource I can look up to better understand these terminologies or what topic/subject are they tackled in.

    For figuring out the meaning of specific words being used, like prescaler, I think the best way to go about it is to google it as you encounter them. I do not know of a single source that covers these from start to finish, but I also think that would be a very tiresome way of learning their meaning.
    Instead, I recommend that you google each of them as they are encountered, and make a small note to yourself about their meaning.
    Registers for example is a central topic in embedded programming, which basically boils down to literal transistor states.

    I highly recommend reading an introduction to embedded programming to better grasp the concepts of registers and register manipulation.

    refer_pin said:
    Yup, I think it's a really simple program to start studying to get familiar with. I'm a bit confuse where I can read up on "_WFI()" and "NRF_SUCCESS"? It's my first time seeing them in C

    _WFI (wait for event) is a CPU function that places the CPU in a low-power POWER ON mode. The CPU will thus use less power while nothing is happening, but as soon as something happens ( an event is generated ) it will be ready to go. You will see that in most of the examples from the SDK the main loop of the program primarily does one thing - such as calling the idle_state_handler function - which essentially is to go into this low-power POWER ON mode. This is one of the main differences between Arduino programming and embedded C programming - while the program in Arduino often is based on a sequential execution of the loop function, the embedded C programs are rather based on an interrupt-event system. In short, this means that the main loop is just going to sleep ( and has the lowest priority ), and instead being interrupted from doing so whenever something needs to be done.

    Lastly, NRF_SUCCESS is one of the error codes we use in the SDK, to signal that a function call was successful. This is also likely a new element when coming from Arduino development, but different driver functions might fail for different reasons, and when that happens there needs to be a proper error handling in place for the particular failure.
    You will learn more about this as you go, but make sure that you have defined DEBUG in your preprocessor defines, like shown in the included image, to have a detailed error message be printed to your logger output whenever such an error is encountered.

    It is therefore also essential that you pass every returned error code to an APP_ERROR_CHECK in order to verify whether the function call was successful.

    This all might be a lot to take in right now, but please do not hesitate to ask if anything should be unclear - then I will try to answer as best I can.

    Hope the above explanations makes things at least somewhat clearer! :) 

    Best regards,
    Karl

  • Please also know that P0.06 is usually used for the UART TX PIN in all the SDK examples.

    Hello, 

    So what pins should I use for PWM output? I'm actually planning to push my code to an Adafruit nRF52840 Feather Board. I dont know if my process of coding the nRF52840 dk first then transferring the code to a Adafruit nRF52840 Feather Board is correct but that's my intial plan. Basing from the feather board schematics, my other pin options in the nRF52840 dk that's used in the feather board are 0.07, 0.08, 0.26, 0.27,1.08 and 1.09. From my pin options, what pins are suitable for producing a PWM of 400kHz? 

Related