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

100us counter

OS in development environment :Windows7
HARD :(Taiyo Yuden)EBSHSN Series Evaluation Board : Central / Peripherals
CPU :(Nordic) nRF52832 / ARMR Cortex?-M4F 32 bit processor 28-pin Land Grid Array / 15GPIOs / SWD
Soft Ver:nRF5_SDK_15.2.0_9412b96

Please tell me the processing method to count variables by 100us without generating interrupt.

Please let me know if there is a sample program

Thank you very much.

  • I don't see that your program (the one you attached in your previous reply) contains anything about timer 1. Is that the program you are referring to?

    However, this setup:

    yokokawa said:
    1) Create 100 us with "timer0".
    2) Set "timer1" to "count mode".
    3) Execute "timer1" "COUNT task" at "timer 0" timing with PPI.

    seems good. Did you implement this?

  • I created a program but it doesn't work. Please point out the problem.
    /**
    
    **/
    #include <stdint.h>
    
    #include "nrf_delay.h"
    #include "app_error.h"
    
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define PPI_COUNT_TIMER2_INTERVAL	(100)									/* timer4 interval in 100us */
    
    static const nrf_drv_timer_t t1_counter = NRF_DRV_TIMER_INSTANCE(1);		/* timer2 counter mode */
    static const nrf_drv_timer_t t2_cycle = NRF_DRV_TIMER_INSTANCE(2);			/* timer4 100us */
    
    static nrf_ppi_channel_t m_ppi_channel2;
    
    
    /*-----------------------------------------*/
    /*--< Program operation processing area >--*/
    /*-----------------------------------------*/
    
    /*-< Timer event handler. Not used since Timer1 and Timer2 are used only for PPI. >-*/
    static void empty_timer_handler(nrf_timer_event_t event_type, void * p_context)
    {
    }
    
    /*-< Set timer1:CC1 to counter mode		(timer 1 initialization)	>-*/
    static void timer1_counter_init(void)
    {
    	nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    /*-< Configuration change of timer_cfg >-*/
    //	timer_cfg.frequency = NRF_TIMER_FREQ_16MHz		/* DEFAULT	*/
    	timer_cfg.mode = NRF_TIMER_MODE_COUNTER;		/* NRF_TIMER_MODE_TIMER -> NRF_TIMER_MODE_COUNTER	*/
    	timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;	/* NRF_TIMER_BIT_WIDTH_24 -> NRF_TIMER_BIT_WIDTH_32	*/
    //	timer_cfg.interrupt_priority = 6;				/* DEFAULT	*/
    //	timer_cfg.p_context = NULL;						/* DEFAULT	*/
    
    	ret_code_t err_code = nrf_drv_timer_init(&t1_counter, &timer_cfg, empty_timer_handler);
    	APP_ERROR_CHECK(err_code);
    
    	nrfx_timer_capture(&t1_counter, NRF_TIMER_CC_CHANNEL1);
    
    }
    
    /*-< Set timer2:CC2 to 100us timer mode		(timer 2 initialization)	>-*/
    static void timer2_cycle_init(void)
    {
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    	
    	ret_code_t err_code = nrf_drv_timer_init(&t2_cycle, &timer_cfg, empty_timer_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_timer_extended_compare(&t2_cycle,
    									NRF_TIMER_CC_CHANNEL2,
    									nrf_drv_timer_us_to_ticks(&t2_cycle, PPI_COUNT_TIMER2_INTERVAL),
    									NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK, false);
    }
    
    /*-< PPI controls the process of counting at 100us	(PPI 100us(timer1) counter(timer2) initialization)	>-*/
    static void ppi_Sync_counter_init(void)
    {
    	uint32_t err_code = NRF_SUCCESS;
    
    	err_code = nrf_drv_ppi_init();
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel2);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_assign(m_ppi_channel2,
                                              nrf_drv_timer_event_address_get(&t2_cycle, NRF_TIMER_EVENT_COMPARE2),
                                              nrf_drv_timer_task_address_get(&t1_counter,  NRF_TIMER_TASK_COUNT));
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_enable(m_ppi_channel2);
    	APP_ERROR_CHECK(err_code);
    }
    
    /*--------------------*/
    /*-< Main process	>-*/
    /*--------------------*/
    int main(void)
    {
    	/* Initialize */
    	ppi_Sync_counter_init();	
    	timer1_counter_init();
    	timer2_cycle_init();	
    	
    	/* Start */
    	nrf_drv_timer_enable(&t1_counter);
    	nrf_drv_timer_enable(&t2_cycle);
    	
    	while (true)
    	{
    		/**/
    	}
    }
    
    /** @} */
    
  • Does any of your err_codes return != NRF_SUCCESS?

  • An error does not occur.
    I can not count 100us.
    Please tell me because I want to know the cause that I can not count.
  • Does the attached program improve the accuracy of the counter?
    /**
    
    **/
    #include <stdint.h>
    
    #include "nrf_delay.h"
    #include "app_error.h"
    
    #include "nrf_drv_ppi.h"
    #include "nrf_drv_timer.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define PPI_COUNT_TIMER1_COUNT		(10)									/* timer1 count 10 (10us * 10 = 100us) */
    #define PPI_COUNT_TIMER2_INTERVAL	(10)									/* timer2 interval in 10us */
    
    static const nrf_drv_timer_t t1_counter = NRF_DRV_TIMER_INSTANCE(1);		/* timer1 counter mode */
    static const nrf_drv_timer_t t2_cycle = NRF_DRV_TIMER_INSTANCE(2);			/* timer2 100us */
    
    static nrf_ppi_channel_t m_ppi_channel2;
    
    static uint16_t Control_counter = 0;								/* Control counter  [0-65535(0-6553.5ms)] (1count:100us) */
    
    
    /*-----------------------------------------*/
    /*--< Program operation processing area >--*/
    /*-----------------------------------------*/
    
    /*-< Timer event handler. Not used since Timer1 and Timer2 are used only for PPI. >-*/
    static void empty_timer_handler(nrf_timer_event_t event_type, void * p_context)
    {
    }
    
    void timer1_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
                Control_counter ++;				/* Control_counter update */
    }
    
    /*-< Set timer1:CC1 to counter mode		(timer 1 initialization)	>-*/
    static void timer1_counter_init(void)
    {
    	nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    	timer_cfg.mode = NRF_TIMER_MODE_COUNTER;		/* NRF_TIMER_MODE_TIMER -> NRF_TIMER_MODE_COUNTER	*/
    
    	ret_code_t err_code = nrf_drv_timer_init(&t1_counter, &timer_cfg, timer1_event_handler);
    	APP_ERROR_CHECK(err_code);
    
    //	nrfx_timer_capture(&t1_counter, NRF_TIMER_CC_CHANNEL1);
        nrf_drv_timer_extended_compare(&t1_counter,
    									NRF_TIMER_CC_CHANNEL1,
    									COUNT_TIMER1_COUNT,
    									NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
    }
    
    /*-< Set timer2:CC2 to 100us timer mode		(timer 2 initialization)	>-*/
    static void timer2_cycle_init(void)
    {
        nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    	
    	ret_code_t err_code = nrf_drv_timer_init(&t2_cycle, &timer_cfg, empty_timer_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_timer_extended_compare(&t2_cycle,
    									NRF_TIMER_CC_CHANNEL2,
    									nrf_drv_timer_us_to_ticks(&t2_cycle, PPI_COUNT_TIMER2_INTERVAL),
    									NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK, false);
    }
    
    /*-< PPI controls the process of counting at 100us	(PPI 100us(timer1) counter(timer2) initialization)	>-*/
    static void ppi_Sync_counter_init(void)
    {
    	uint32_t err_code = NRF_SUCCESS;
    
    	err_code = nrf_drv_ppi_init();
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel2);
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_assign(m_ppi_channel2,
                                              nrf_drv_timer_event_address_get(&t2_cycle, NRF_TIMER_EVENT_COMPARE2),
                                              nrf_drv_timer_task_address_get(&t1_counter,  NRF_TIMER_TASK_COUNT));
    	APP_ERROR_CHECK(err_code);
    
    	err_code = nrf_drv_ppi_channel_enable(m_ppi_channel2);
    	APP_ERROR_CHECK(err_code);
    }
    
    /*--------------------*/
    /*-< Main process	>-*/
    /*--------------------*/
    int main(void)
    {
    	/* Initialize */
    	ppi_Sync_counter_init();	
    	timer1_counter_init();
    	timer2_cycle_init();	
    	
    	/* Start */
    	nrf_drv_timer_enable(&t1_counter);
    	nrf_drv_timer_enable(&t2_cycle);
    	
    	while (true)
    	{
    		/**/
    	}
    }
    
    /** @} */
    
Related