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

Explanation of pwm example code please

Hi, I need to configure pwm with 14uSec off time and 46uSec on time. I had a look at the pwm example in the install directory and also : developer.nordicsemi.com/.../a00039.html. But it is bit confusing, in the sense, if I need to set a duty cycle of 25% which registers to change. When I change the duty_cycle to 250, or, 100(in the local variable "duty_cycle"), there is no effect in the PWM wave generated. The On and Off times are 256msec always. Even tried pressing the configured buttons, but still no change in duty cycle. Attached my code here: main.c

Questions:

  1. Which register to change to set a desired duty cycle(say 25% off time and 75% on time)
  2. If I want to use the same PWM module for generating 25% duty cycle outputting on PIN1 for the first time, then 100% duty cycle outputting on PIN2 the next time and the above continues forever, do I need to deinit the gpiote and configure(nrf_gpio_tsk_config) every time I swap the PWM pins?
  • Hi Ole, Thanks for your reply and sorry for not being clear. I needed to control the PWM pulse duty cycle via BLE. Initially the duty cycle would be 75% (42usec on time, 14usec off time), after getting an input from the BLE bonded phone connection, the duty cycle would reduce(example: 50%). After getting a second input from BLE bonded phone connection, the period of the PWM pulse would reduce (for example, period of PWM pulse would reduce from 56usec to 24usec with on time 10usec and off time 14usec), etc.

  • I was wondering what would happen if an interrupt(from timer) for the PWM pulse generation is overridden by the BLE interrupt. This is because, timer has low priority interrupt than BLE. How to handle this situation? Thanks.

  • As the code is now BLE interrupts can invert the PWM if the TIMER_IRQHandler is interrupted an a toggle is missed (for example if a BLE interrupt lasting longer than MAX_SAMPLE_LEVELS happen just before if(cc0_turn)).

    The PWM library does not have this problem, but with this library you can only set the duty cycle of the PWM and not the frequency. I would use this library if you can, if not you will have to find out a clever way to update the PWM without risking invertions or dissortions in the signal. One way is to stop the timer, update the CC values and then start the timer again, this will avoid invertion, but will create a long low or high pulse on the line.

  • Hi Ole, thanks for your reply.

    1. When you meant PWM library, did you mean app_pwm.c and app_pwm.h (which has app_pwm_channel_init etc in it)?
    2. I dont have any problems using the PWM library. But, I want on/off time to go as low as 10usec. Is it possible with app_pwm.c library? I thought app_pwm.c uses softdevice. Since softdevice uses RTC, the lowest RTC can go is 30.5usec. But I need PWM pulses as low as 10usec. Also I need to use BLE in the same code base with PWM. Do you think I can still go for PWM library(app_pwm.c)? If not, what is your recommendation? Thanks.
    1. Yes, I mean app_pwm.c and app_pwm.h.
    2. You can have on/off time as low as 10usec. The PWM library uses the TIMER peripheral just as your code.

    The PWM library can be used with or without the SoftDevice, no problem.

Related