I'm trying to just set up a Timer for 8 MHz frequency and have that output on a pin. It seems simple but I'm having some problems (the frequency is a lot lower). What am I doing wrong?
See code (I'm using the Soft Device). I imagine we only need to this:
main.c
#include "allneededheadersofcourse.h" // :)
#define PWM_OUTPUT_PIN_NUMBER (LED0) /*!< Pin number for PWM output / #define TIMER_PRESCALER 0U /!< Prescaler setting for timer */
/** Initializes Timer 2 peripheral. / static void pwm_timer_init(void) { / Start 16 MHz crystal oscillator */ uint32_t p_is_running;
sd_clock_hfclk_request();
do
{
sd_clock_hfclk_is_running(&p_is_running);
} while (p_is_running == 0);
NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; NRF_TIMER2->PRESCALER = TIMER_PRESCALER;
// Clears the timer, sets it to 0 NRF_TIMER2->TASKS_CLEAR = 1;
NRF_TIMER2->CC[2] = 1; }
/** Initializes GPIO Tasks/Events peripheral. */ static void pwm_gpiote_init(void) { // Configure PWM_OUTPUT_PIN_NUMBER as output nrf_gpio_cfg_output(PWM_OUTPUT_PIN_NUMBER);
// Configure GPIOTE channel 0 to toggle the PWM pin state // Note that we can only connect one GPIOTE task to an output pin nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW); }
/** Initializes Programmable Peripheral Interconnect peripheral. */ static void pwm_ppi_init(void) { sd_ppi_channel_assign(PPI_CHEN_CH2_Pos, (uint32_t)&NRF_TIMER2->EVENTS_COMPARE[2], (uint32_t)&NRF_GPIOTE->TASKS_OUT[0]); sd_ppi_channel_enable_set(PPI_CHEN_CH2_Enabled << PPI_CHEN_CH2_Pos); }
/**
- @brief Application main function.
- @details Initializes the system and enters the main loop\
- @param None
- @return None */ int main(void) { pwm_gpiote_init(); pwm_ppi_init(); pwm_timer_init();
NRF_TIMER2->TASKS_START = 1;
while(1) { }
Thank you!!!
Gil