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 www.waveshare.com/.../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.
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.
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.
One important thing to remember is that the Softdevice trumps everything. If you are doing anything at all other than BLE you need to assume that what you are doing might be interrupted by the Softdevice and put on hold for a certain amount of time. So consider the following:
If you are certain that the positive edge ISR has the highest priority in your system I suppose you are safe. But why not trigger the ADC sample task directly via PPI anyway? Then you don't need CPU resources to start the sampling and wait for the result, but only to handle the conversion complete interrupt. You can also be certain that the Softdevice or other interrupts won't interfere with the timings of the sampling.
Thank you, Martin.