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

how to make a Pulse with Timer1

Hi,

i need to make pulse with one of my Timers. Timer0 is used by the softdevice, and Timer2 is used to make PWM. I need to make a Pulse wich is 100us long, every 250ms (4hz). If i use Timer0 in my configuration i only can make this Pulse every 50ms not longer but how can i change that? Do i overflow the CC-Register with a big value ? Maybe someone can have a look at my Code.


void Timer1Init (void)
{
	uint32_t Pulsweite = 25000;   //3s
	uint32_t Periode = 80000;    //5s

	
	NRF_TIMER1->TASKS_STOP = 1;
	// Create an Event-Task shortcut to clear TIMER0 on COMPARE[0] event
	NRF_TIMER1->MODE        = TIMER_MODE_MODE_Timer;
	NRF_TIMER1->BITMODE     = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
	NRF_TIMER1->PRESCALER   = 4;  // 1us resolution

	NRF_TIMER1->TASKS_CLEAR = 1;               	// clear the task first to be usable for later
	
	NRF_TIMER1->CC[0] = 100;   			//second timeout
	NRF_TIMER1->CC[1] = 50000; 				//first timeout
	
	NRF_TIMER1->INTENSET    = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
	NRF_TIMER1->INTENSET    = TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;
	/* Create an Event-Task shortcut to clear TIMER1 on COMPARE[0] event. */
	NRF_TIMER1->SHORTS      = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos);
		
	sd_nvic_SetPriority (TIMER1_IRQn, APP_IRQ_PRIORITY_HIGH);		// Setzt die Prioroität auf HIGH
  sd_nvic_EnableIRQ 	(TIMER1_IRQn);													// Aktiviert den Interrupt

	NRF_TIMER1->TASKS_START = 1;
}


void TIMER1_IRQHandler(void) 
{
    if (NRF_TIMER1->EVENTS_COMPARE[0] != 0)
    {
      NRF_TIMER1->EVENTS_COMPARE[0] = 0;
			switch_intensi(1);
    }
    if (NRF_TIMER1->EVENTS_COMPARE[1] != 0)
    {
      NRF_TIMER1->EVENTS_COMPARE[1] = 0;
			switch_intensi(0);
    }

}


Thank you for your help, Nils ;)

  • The function you want should be achievable with using just the GPIOTE, a TIMER and the PPI. Please see the attached main-file, which looks something like this:

    
        ...
        NRF_CLOCK->TASKS_HFCLKSTART = 1;
        
        NRF_TIMER0->CC[0] = 100;
        NRF_TIMER0->CC[1] = 250000;
        NRF_TIMER0->PRESCALER = 4;
        NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos;
        
        nrf_gpiote_task_config(0, OUT_PIN_NUMBER, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_HIGH);
        
        NRF_PPI->CH[0].EEP = (uint32_t) &NRF_TIMER0->EVENTS_COMPARE[0];
        NRF_PPI->CH[0].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
        NRF_PPI->CH[1].EEP = (uint32_t) &NRF_TIMER0->EVENTS_COMPARE[1];
        NRF_PPI->CH[1].TEP = (uint32_t) &NRF_GPIOTE->TASKS_OUT[0];
        NRF_PPI->CHENSET = PPI_CHENSET_CH0_Enabled << PPI_CHENSET_CH0_Pos | 
                           PPI_CHENSET_CH1_Enabled << PPI_CHENSET_CH1_Pos;
        
        NRF_TIMER0->TASKS_START = 1;
    
        ...
    
    

    Edit: If you want interrupts, it should be sufficient to enable interrupts on the COMPARE events, and then implement a TIMER0_IRQHandler. I haven't tested this code, but it should be something like this:

    
    
    void TIMER0_IRQHandler(void)
    {
        if (NRF_TIMER0->EVENTS_COMPARE[0] == 1)
        {
            NRF_TIMER0->EVENTS_COMPARE[0] = 0;
            ...
        } 
        if (NRF_TIMER0->EVENTS_COMPARE[1] == 1)
        {
            NRF_TIMER0->EVENTS_COMPARE[1] = 0;
            ...
        }
    }
    ...
        NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos |
                               TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos;
        NVIC_EnableIRQ(TIMER0_IRQn);
    ...
    
    

    main.c

  • Hi thank you Ole, This i a good example. I Understand what happens in this code. But how is it when i want to call a Method every CC[0] or CC[1] happens?

  • Please see my edit. I haven't tested this, but it should at least make it clear how you could do it.

Related