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

How can I get 4 PWM channels using 4 capture compare regiters of timer2

I have already made code of 3 pwm channels using c[0],cc[1],cc[2],cc[3]. I want 4 PWM channels but I dont have more cc registers left so how can i code it. plz help me. ALso I want to run it with softdevice so what changes do i need to make.

code done so far is:

#include <stdbool.h> #include <stdint.h> #include "nrf.h" #include "nrf_gpiote.h" #include "nrf_gpio.h"

#define RED_LED (21) /**< Pin number for PWM output. */ #define GREEN_LED (22) #define BLUE_LED (23)

#define TIMER_PRESCALERS 6U /**< Prescaler setting for timer. */

static void timer2_init(void) { // Start 16 MHz crystal oscillator . NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; NRF_CLOCK->TASKS_HFCLKSTART = 1;

// Wait for the external oscillator to start up.
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
{
    //Do nothing.
}

NRF_TIMER2->MODE        = TIMER_MODE_MODE_Timer;
NRF_TIMER2->BITMODE     = TIMER_BITMODE_BITMODE_08Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER2->PRESCALER   = TIMER_PRESCALERS;

// Clears the timer, sets it to 0.
NRF_TIMER2->TASKS_CLEAR = 1;

// Load the initial values to TIMER2 CC registers.
NRF_TIMER2->CC[0] = 0;

NRF_TIMER2->CC[1] = 1;

NRF_TIMER2->CC[2] = 255;

NRF_TIMER2->CC[3] =1;

}

/** @brief Function for initializing the GPIO Tasks/Events peripheral. */ static void gpiote_init(void) { // Connect GPIO input buffers and configure PWM_OUTPUT_PIN_NUMBER as an output. //nrf_gpio_range_cfg_input(BUTTON_START, BUTTON_STOP, BUTTON_PULL); nrf_gpio_port_clear(NRF_GPIO_PORT_SELECT_PORT2, 0xFF);

nrf_gpio_cfg_output(RED_LED);
nrf_gpiote_task_config(0, RED_LED, \
                       NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW);

nrf_gpio_cfg_output(GREEN_LED);
nrf_gpiote_task_config(1, GREEN_LED, \
                           NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW);

nrf_gpio_cfg_output(BLUE_LED);
nrf_gpiote_task_config(2, BLUE_LED, \
                               NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW);

}

/** @brief Function for initializing the Programmable Peripheral Interconnect peripheral. */ static void ppi_init(void) { // 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];

// Configure PPI channel 1 to toggle PWM_OUTPUT_PIN on every TIMER2 COMPARE[1] match.
NRF_PPI->CH[1].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[1];
NRF_PPI->CH[1].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];

NRF_PPI->CH[2].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[0];
NRF_PPI->CH[2].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];

NRF_PPI->CH[3].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[2];
NRF_PPI->CH[3].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[1];

NRF_PPI->CH[4].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[0];
NRF_PPI->CH[4].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[2];

NRF_PPI->CH[5].EEP = (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[3];
NRF_PPI->CH[5].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[2];
// Enable PPI channels 0-2.
NRF_PPI->CHEN = (PPI_CHEN_CH0_Enabled << PPI_CHEN_CH0_Pos)
                | (PPI_CHEN_CH1_Enabled << PPI_CHEN_CH1_Pos)
                | (PPI_CHEN_CH2_Enabled << PPI_CHEN_CH2_Pos)
                | (PPI_CHEN_CH3_Enabled << PPI_CHEN_CH3_Pos)
                | (PPI_CHEN_CH4_Enabled << PPI_CHEN_CH4_Pos)
                | (PPI_CHEN_CH5_Enabled << PPI_CHEN_CH5_Pos);

}

/**

  • @brief Function for application main entry. */ int main(void) { gpiote_init(); ppi_init(); timer2_init();

    NRF_TIMER2->TASKS_START = 1; // Start the timer.

    while (true) { // Do nothing. } }

Related