nRF51 SDK - S210 SoftDevice
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
PWM Example

The PWM Example shows how the Programmable Peripheral Interconnect (PPI) and a free running timer can be used to output a PWM waveform over a GPIO. The GPIO pin could drive a small speaker, for example.

This example generates a variable duty cycle pulse. The duty cycle is changed by buttons and will vary between 1 and 255. Note that the value 0 is omitted because it would cause two events to be triggered at the same time, which would result in only one toggle of the output pin. Timer 2 is used to dispatch GPIOTE toggle events on every compare match. CC1 will set the output pin high, while CC0 and CC2 will toggle it low every other turn, ending a duty cycle. These compare timings will be set by an interrupt that happens every CC1 at the 0 (or 256) mark for the period. This interrupt will increase CC1 with one more period. Also, this interrupt will set CC0 or CC2 to the value retrieved from the state of the buttons. If CC0 or CC2 is set changes every turn.

The application will emit light on LED 1, corresponding to the duty cycle. With a higher value, the LED will emit less intense light.

Setup

The name of the example is pwm_pca10028. If you are not using the Keil Pack Installer, you can find the source code and project file of the example in the following folder: <InstallFolder>\Nordic\nrf51\examples\peripheral\pwm

LED assignments:

  • LED 1 shows the duty cycle of the current state.

Button assignments:

  • Button 1 and button 2 change the duty cycle.

Testing

Test the PWM Example application by performing the following steps:

  1. Compile and program the application.
  2. LED 1 should give high light intensity.
  3. Press BUTTON_1 or BUTTON_2 to change LED light intensity.