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

Data from sensor can not be sampled at 1000 Hz.

Hi,

I am using nRF_SDK_15.0.0_a53641 and writing a program with keil.

I am receiving data from the sensor with TWI communication and writing it to the sensor by SPI communication.

So I sampled the data with reference to the apptimer example,

timer_ms = 1;
time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TWI, time_ms);

As I tried to write data every 1 ms (1000 Hz), it runs at sampling frequency much lower than 1000 Hz.

Is it due to some other configuration?

The frequency setting for TWI communication with the sensor and SPI communication with the SD card is the default.

I will attach a part of the program below.

/**
 * @brief Handler for timer events.
 */
void timer_twi_event_handler(nrf_timer_event_t event_type, void* p_context)
{

    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE1:
					
        
			flag = 1;
					
            break;

            default:
            //Do nothing.
            break;
    }
}

int main(void)
{
	float time = 0.000;
	float g = 9.81;
	float t = 0.001;
	uint32_t time_ms = 1; //Time(in miliseconds) between consecutive compare events.
	uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

	//SPI start condition
	SPI_start();
	
		
    bsp_board_init(BSP_INIT_LEDS);
	
	
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("FATFS example started.");
	
	//Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_TWI, &timer_cfg, timer_twi_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_TWI, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_TWI, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_TWI);

	TWI_start();//ST
	nrf_delay_us(5);
    twi_init();
	nrf_delay_us(5);
    LM75B_set_mode();//SAD+W,SUB(SUB[7] set to 1)
   	nrf_delay_us(100);
   	
    while (true)
    {

		if(flag){
			time += t;
   	        TWI_start();//SR
		    nrf_delay_us(600);//600-500
		    read_sensor_data();//SAD+R
		    nrf_delay_us(4);
	        TWI_stop();//SP
		    nrf_delay_us(5);
	
	        //uint8 to short(16bit)
	        x = (short)(m_sample[1] << 8 | m_sample[0]);
	        y = (short)(m_sample[3] << 8 | m_sample[2]);
		    z = (short)(m_sample[5] << 8 | m_sample[4]);

    
            x_g = (((float)x*2*2)/(float)65536);
		    y_g = (((float)y*2*2)/(float)65536);
		    z_g = (((float)z*2*2)/(float)65536);
		
		    x_g = x_g * g;
		    y_g = y_g * g;
		    z_g = z_g * g;

		    sprintf(time_b,"%lf",time);
	        sprintf(buf0,"%lf",x_g);
	        sprintf(buf1,"%lf",y_g);
	        sprintf(buf2,"%lf",z_g);
		

            fatfs_example(time_b,buf0,buf1,buf2);
		
		    flag = 0;
			
			
		}
		__WFI();
    }
}

Thank you.

Parents
  • I am receiving data from the sensor with TWI communication and writing it to the sensor (sic?) by SPI communication.

    You mean, "writing it to the SD Card by SPI communication" ?

    Is your flag defined as volatile ?

    As I tried to write data every 1 ms (1000 Hz), it runs at sampling frequency much lower than 1000 Hz.

    So what frequency is it?

    Is is the sampling that appears not to work,or the writing?

    Have you looked at  the hardware activity using a scope or analyser?

    Please fix the indentation on your posted code.

  • Thank you for reply!I am sorry that I have lacked much explanation

    ・You mean, "writing it to the SD Card by SPI communication" ?

    Yes.sensor and TWI communication.I will communicate with the SD card via SPI.

    Is your flag defined as volatile ?

    How do I set it? I would appreciate it if you could tell me.

    So what frequency is it?

    This is the sampling frequency at which data read from the sensor is written to the SD card.

    Please fix the indentation on your posted code.

    fixed! Verification please.


    Is my usage of apptimer correct?

    If you have anything to fix such as the contents of a while loop, please point out.

    Thank you for your many cooperation!!

Reply
  • Thank you for reply!I am sorry that I have lacked much explanation

    ・You mean, "writing it to the SD Card by SPI communication" ?

    Yes.sensor and TWI communication.I will communicate with the SD card via SPI.

    Is your flag defined as volatile ?

    How do I set it? I would appreciate it if you could tell me.

    So what frequency is it?

    This is the sampling frequency at which data read from the sensor is written to the SD card.

    Please fix the indentation on your posted code.

    fixed! Verification please.


    Is my usage of apptimer correct?

    If you have anything to fix such as the contents of a while loop, please point out.

    Thank you for your many cooperation!!

Children
No Data
Related