Before you read further, please take a look at my previous article on nRF51-DK programming using GCC, since we’re going to use the same setup here.
This is a simple example that demonstrates PWM, GPIOTE, and NUS (Nordic UART Service) using the S110 softdevice on the nRF51-DK. This example uses the nRF51 SDK version 9.0.0. To use it, modify pca10028/s110/armgcc/Makefile to point SDK_ROOT to your nRF51 SDK directory.
I have tried to remove all unnecessary layers (like the Nordic BSP support) to make this example simple to understand. It uses instances PWM1 and PWM2 associated with Timer1 and Timer2 respectively, and each of them support two PWM channels each. I have used pins 21, 22, 23, 24 for these channels, corresponding to LED1, LED2, LED3 and LED4 on the nRF51-DK (PCA10028) board. Notice that to use these timers, I have enabled them in config/nrf_drv_config.h.
When you upload this code, the LEDs will pulse as shown in the above video. In addition, if you press the switches SW1 and SW2 on the board, it will toggle PWM on LED1 and LED2 respectively. SW1 works by reading the actual value of pin 17, whereas SW2 works using GPIOTE events.
You will also see this appear as a BLE device called PWM and you will be able to communicate with it using UART over BLE using the Nordic nRFToolbox app.
You can get the source code for this project here:
- Nordic nRF51 SDK version 9.0.0 documentation.