How to trigger ADC from a timer interrupt, while generating PWM with the same timer and while using BLE on nRF51822?

pabloxid gravatar image

asked 2017-12-06 00:13:55 +0100

Hi. I am a newbie in Nordic chips, quite knowledgeable in Atmel AVR, though. I'm working with an nRF51822 on a board like this https://www.waveshare.com/wiki/BLE400. I use the Mbed environment.

What I'm trying to do is this:

Generate a PWM signal on a pin, with period approximately equal to 4ms (200 ~ 250Hz) and variable duty cycle (within some limits, without ever reaching 0). Simultaneously, I want to measure voltage in another pin, but I want the ADC to trigger during the positive part of the PWM, in each cycle. That is, I'm controlling a heater with PWM, and want to measure current as a feedback, but I need to sample it during the positive cycle, synchronized to PWM, otherwise I would have random errors in the measure.

I have done this without trouble in AVR, using a hardware timer with interrupts, but I have no idea how to do it in the nRF51822 (maybe PPI? also no idea how to code it in Mbed..). In addition, I need to maintain a BLE connection while doing simultaneously PWM and ADC.

Any advice you can give me, I'm grateful.


edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
MartinBL gravatar image

answered 2017-12-07 12:21:49 +0100


I suggest that you use the PWM library for nRF51, a GPIOTE channel, and PPI. Since the PWM library use a GPIOTE channel to control the PWM pin, you will need to connect the PWM pin to a different GPIO externally and use a second GPIOTE channel to register positive edges there. A GPIOTE event on a positive edge can then directly be used to trigger an ADC START task and make it do a conversion via PPI.

However you might not want to sample the voltage on every edge? That sounds like quite a lot of work for the CPU while at the same time doing BLE. So what you could do then, is to use a TIMER/COUNTER and use the GPIOTE positive edge event to trigger a COUNT task in the counter. Then you can do measurements every time the counter reaches a certain count. This can all be done autonomously and you need only wake up when the ADC conversion is completed.

edit flag offensive delete publish link more


Hi Martin, thank you for your response.

Do you mean that I have to connect the pwm pin externally to another pin to capture the event? Another possibility would be to trigger the ADC conversion manually from any of the ISRs used for PWM?

Regarding the second issue: yes, actually I want to sample the voltage on each positive edge. That's why the PWM frequency is not really that high, 200Hz. I suppose that the ADC would have to be perfectly capable of sampling at 200Hz without consuming CPU (at least that is the case in AVR). I need that many samples to apply some algorithms like filters, etc. One piece of information I did not find in the datasheet is how the ADC works and how much time it takes to do its job.

pabloxid ( 2017-12-07 15:15:38 +0100 )editconvert to answer

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

Question Tools

1 follower


Asked: 2017-12-06 00:13:55 +0100

Seen: 66 times

Last updated: des. 07