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

NRF51 sine wave with pwm and timer

Hi,

I want to generate a sine wave using the nrf pwm library and a timer CC instead of a delay that is used in the sine example here to get better accuracy.

It basically works but I keep getting strange "jumps" in the signal. Here is an image of the signal after an RC filter (5kOhm, 100nF).

image description

It seems like the phase variable changes value randomly sometimes and I cannot understand why.

Below is my code:

 #include <msp/ble-peripheral/BluetoothPeripheral.h>
#include <msp/drivers/delay.h>
#include <msp/drivers/board_ViTheSSG4rev1.h>

// timer stuff
#include <components/drivers_nrf/hal/nrf_gpiote.h>
#include <components/drivers_nrf/hal/nrf51.h>
#include <components/softdevice/s110/headers/nrf_soc.h>
#include <components/drivers_nrf/hal/nrf51_bitfields.h>

extern "C" {
#include <msp/drivers/nrf_pwm.h>
}

using namespace msp::drivers;

namespace {

uint8_t phase = 0;

bool active = false;

const uint8_t sine26[] = {0, 4, 16, 35, 59, 88, 120, 152, 182, 210, 232, 247,
    255, 255, 247, 232, 210, 182, 152, 120, 88, 59, 35, 16, 4, 0};

}

extern "C" {

void TIMER1_IRQHandler(void) {
  if (active) {
    nrf_pwm_set_value(0, sine26[phase]);
    if (phase < 26) {
      phase += 1;
    } else {
      phase = 0;
    }
  }

//pinToggle(LED_RED);

  NRF_TIMER1->EVENTS_COMPARE[0] = 0; // Clear compare match register

  NRF_TIMER1->TASKS_CLEAR = 1; // Reset timer

}
} // end of extern C

void initPWM(uint8_t pin, nrf_pwm_mode_t nrfPwmMode);
void initTimer(uint16_t frequency);

int main() {
  setupLEDPins();

  pinOutput(PIEZO_PWM_PIN);

// Init softdevice
  BluetoothPeripheral::instance().initialize();

// init pwm
//PWM_MODE_VITHESS_VIBRATOR is 0-255 (8-bit) resolution, 62.5 kHz PWM frequency, 16 MHz timer frequency (prescaler 0)
  initPWM(PIEZO_PWM_PIN, PWM_MODE_VITHESS_VIBRATOR);

  initTimer(128);

  pinClear(LED_GREEN);
  active = true;
  NRF_TIMER1->TASKS_START = 1;               // Start timer.

  while (1) {
    delayMs(1000);
    pinSet(LED_GREEN);
    NRF_TIMER1->TASKS_STOP = 1;
    active = false;
    nrf_pwm_set_value(0, 0);
    delayMs(1000);
    pinClear(LED_GREEN);
    active = true;
    NRF_TIMER1->TASKS_START = 1;               // Start timer.
  }

  return 0;
}

void initPWM(uint8_t pin, nrf_pwm_mode_t nrfPwmMode) {
  nrf_pwm_config_t
  pwm_config = PWM_DEFAULT_CONFIG;

  pwm_config.mode = nrfPwmMode;
  pwm_config.num_channels = 1;
  pwm_config.gpio_num[0] = pin;

// Initialize the PWM library
  nrf_pwm_init (&pwm_config);

// Start the external 16 MHz clock for a more accurate PWM frequency
  NRF_CLOCK->TASKS_HFCLKSTART = 1;
}

void initTimer(uint16_t frequency) {
  NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode
  NRF_TIMER1->TASKS_CLEAR = 1;  // clear the task first to be usable for later
  NRF_TIMER1->PRESCALER = 5; //Set prescaler. Higher number gives slower timer. Prescaler = X gives 16MHz/2^X Hz timer
  NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution
//NRF_TIMER1->CC[0] = 500000 / (frequency * 26); //Set value for timer compare register 0, timer period is 2us, pwm period is (1/(freq*255)), pwm period/timer period is 15
  NRF_TIMER1->CC[0] = 150; //Set value for timer compare register 0 to 150 as test value

// Enable interrupt on Timer 1,for CC[0]
  NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled
      << TIMER_INTENSET_COMPARE0_Pos);
  sd_nvic_SetPriority(TIMER1_IRQn, 3);
  sd_nvic_EnableIRQ(TIMER1_IRQn);
}

Does anyone understand this behaviour?

Thanks!

Parents
  • Hi John, Thanks for your workaround. I tried to implement it but I am not sure if I understand you correctly, is it something like this you mean:

    namespace {
    
    uint8_t phase = 1;
    
    bool active = false;
    
    const uint8_t sine26[] = {0, 4, 16, 35, 59, 88, 120, 152, 182, 210, 232, 247,
        255, 255, 247, 232, 210, 182, 152, 120, 88, 59, 35, 16, 4, 0};
    
    }
    void initSimplePWM(uint8_t pin);
    
    extern "C" {
    
    void TIMER2_IRQHandler(void) {
      NRF_TIMER2->CC[0] = sine26[phase];
      if (phase < 26) {
        phase++;
      } else {
        phase = 1;
      }
    }
    
    } // end of extern C
    
    int main() {
      setupLEDPins();
    
      pinOutput(PIEZO_PWM_PIN);
    
    // Init softdevice
      BluetoothPeripheral::instance().initialize();
    
      initSimplePWM(PIEZO_PWM_PIN);
      NRF_TIMER2->TASKS_START = 1;               // Start timer.
    
      while (1) {
        delayMs(100);
        pinToggle(LED_RED);
      }
      
      return 0;
    }
    
    void initSimplePWM(uint8_t pin) {
      // Configure GPIOTE channel 0 and 1 to change pin state
      nrf_gpiote_task_config(0, pin, NRF_GPIOTE_POLARITY_LOTOHI,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    
      nrf_gpiote_task_config(1, pin, NRF_GPIOTE_POLARITY_HITOLO,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    
      // Configure PPI channel 0 to toggle PWM_OUTPUT_PIN on every TIMER2 COMPARE[0] match.
      NRF_PPI->CH[0].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[0];
      NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
      NRF_PPI->CH[1].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[1];
      NRF_PPI->CH[1].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];
    
      // Enable PPI channel 0.
      NRF_PPI->CHEN = (PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos |
      PPI_CHEN_CH1_Enabled << PPI_CHEN_CH1_Pos);
      //sd_ppi_channel_enable_set(1 << 0);
    
      // Init TIMER2 for 10kHz PWM
      NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;     // Set the timer in Counter Mode
      NRF_TIMER2->TASKS_CLEAR = 1;   // Clear the tasks first to be usable for later
      NRF_TIMER2->PRESCALER = 4; // 2^PRESCALER (values authorized 1 to 9) 4 : 16MHz / 16 = 1MHz
      NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution
      NRF_TIMER2->CC[0] = phase; //1Mhz timer with prescaler 4
      NRF_TIMER2->CC[1] = 400; //1Mhz timer with prescaler 4
      NRF_TIMER2->CC[2] = 256; //1Mhz timer with prescaler 4
      NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled
          << TIMER_SHORTS_COMPARE1_CLEAR_Pos);
    
      NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE2_Enabled
          << TIMER_INTENSET_COMPARE2_Pos);
      sd_nvic_SetPriority(TIMER2_IRQn, 3);
      sd_nvic_EnableIRQ(TIMER2_IRQn);
    }
    

    This code does not work but if this is what you meant and you got it to work I will continue on this path as there does not seem to be any other way to generate a sine wave output with the nrf51 device.

  • One other thing. In your code

    // Configure GPIOTE channel 0 and 1 to change pin state
      nrf_gpiote_task_config(0, pin, NRF_GPIOTE_POLARITY_LOTOHI,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    
      nrf_gpiote_task_config(1, pin, NRF_GPIOTE_POLARITY_HITOLO,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    

    it looks like you are trying to use two GPIOTE task that target the same pin. You can't do that. You have to use the toggle function. That's why there are so many PWM problem questions. It would be a much better system if you could SET and CLEAR the same pin via PPI/GPIOTE, but I guess the silicon designers have to make compromises. I believe the new nRF52 has proper PWM hardware.

Reply
  • One other thing. In your code

    // Configure GPIOTE channel 0 and 1 to change pin state
      nrf_gpiote_task_config(0, pin, NRF_GPIOTE_POLARITY_LOTOHI,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    
      nrf_gpiote_task_config(1, pin, NRF_GPIOTE_POLARITY_HITOLO,
          NRF_GPIOTE_INITIAL_VALUE_LOW);
    

    it looks like you are trying to use two GPIOTE task that target the same pin. You can't do that. You have to use the toggle function. That's why there are so many PWM problem questions. It would be a much better system if you could SET and CLEAR the same pin via PPI/GPIOTE, but I guess the silicon designers have to make compromises. I believe the new nRF52 has proper PWM hardware.

Children
No Data
Related