5 Replies Latest reply on Mar 29, 2018 8:39 AM by e.pratt_1639216

    How to generate a timestamp between two data readings

    EliasTALBI

      Hi,

       

      I am using PSoC 4 to develop a sensor. I need to timestamp the data I read, then I will only send the peaks and their timestamps to a mobile phone.

       

      When I code:

       

      #include<AFE4403_Sample.h>

      #include<time.h>

      #include <sys/time.h>

       

       

      // afe4403 sample data

      void AFE4403_Sample(int *num, uint32 (*data)[350])

      {

          uint32 temp1;

          int n=0;

          time_t stop,start;

          double Timestamp[350];

        

          start=time(NULL);

       

          if (*num<50)       // num = 340 when work; num = 34 when debug

          {

              while(n<350)

              {

                  temp1=BLE_AFE4403_ReadData(0x2A,0x00);  /

                  temp1=temp1& 0xffffff;  //set the highest two bits to 0

                  CyDelay(10);

                  stop=time(NULL);

                  Timestamp[n] = difftime(stop, start);

            

                  (*data)[n]=temp1;

                  n++;

              }

          }

      }

       

      My Timestamp[350] array will have 350 times the same value. Does anyone know how to get the time difference between the start of AFE4403_Sample and the reading of my data?

       

      Regards,

      Elias

        • 1. Re: How to generate a timestamp between two data readings
          bob.marlowe

          You will need to maintain your own time. When you need an absolute time stamping have a look at the RTC Component.

          When only a relative time is needed a Timer component which fires an interrupt to update the current time will do.

           

          Bob

          • 2. Re: How to generate a timestamp between two data readings
            EliasTALBI

            Thanks for the reply Bob!

             

            My problem is that I need to measure time between the start of the function and each reading of my data in the loop. RTC is not precise enough to do the job as the first time stamp should be something like 10 milliseconds and the last one something like 3500 mmilliseconds.

             

            Do you have any example on how to create my timestamps with a timer without using any external PIN? The problem when I  try to do it is that the function MyTimer_ReadCounter() will always return the same value.

             

            Regards,

            Elias

            • 3. Re: How to generate a timestamp between two data readings
              e.pratt_1639216

              Well, you also need to make sure the timer is running. If the timer is stopped, then the read counter function will return the same value.

              Also, you can create an interrupt that handles a timer with, say, a 1 ms interrupt, and then increment a custom timer variable on each count.

              Then, just clear the count when you want to reset it.

              If you create a new project, and select "code example", then filter by "time", there should be a "SysTick_Example" project that demonstrates using an internal timer to count timing. This should be useful for you.

              • 4. Re: How to generate a timestamp between two data readings
                EliasTALBI

                Thank you very much for your replies!

                 

                After many hours I finally found a way to get my timestamps. This what I have done to settle this problem:

                 

                1) Add a timer in my topdesign.cysh and feed it with a 1kHz clock. I also wired it's interrupt to ISR_interrupt.

                 

                2) The Code:

                 

                #include<project.h>

                 

                uint32 counter=0;

                 

                CY_ISR(Routine)

                {

                    if (Counter_1_GetInterruptSource() == Counter_1_INTR_MASK_CC_MATCH)

                    {

                       if(counter==999999)

                        {

                            counter=0;

                        }

                       counter+= Counter_1_ReadCounter();

                       Counter_1_WriteCounter(0);

                       Counter_1_ClearInterrupt(Counter_1_INTR_MASK_CC_MATCH);

                    }

                   

                    if (Counter_1_GetInterruptSource() == Counter_1_INTR_MASK_TC)

                    {

                       Counter_1_ClearInterrupt(Counter_1_INTR_MASK_TC);

                    }

                }

                 

                void My_funtion(uint32 (*data)[350], uint32 (*timestamp)[350])

                {

                    uint32 temp1;

                    int n=0;

                   

                    Counter_1_Start();

                    ISR_Counter_Start();

                    ISR_Counter_StartEx(Routine);

                 

                        while(n<350)

                        {

                            temp1=BLE_AFE4403_ReadData(0x2A,0x00);  // Data aquisition

                            Counter_1_SetInterrupt(Counter_1_INTR_MASK_CC_MATCH);

                            CyDelay(10); 

                            (*data)[n]=temp1;

                            (*timestamp)[n] = counter; //Timestamp in ms

                            n++;

                        }

                    }

                    Counter_1_Stop();

                    ISR_Counter_Stop();

                }

                 

                 

                I hope this code will help next person having problems to create timestamps.

                 

                Regards,

                Elias

                • 5. Re: How to generate a timestamp between two data readings
                  e.pratt_1639216

                  The function calls:

                      ISR_Counter_Start();

                      ISR_Counter_StartEx(Routine);

                  Are redundant. The first one starts the ISR with the default interrupt routine handling it (which is in generated code in the IDE), whereas the second one attaches the ISR to send execution to your "Routine" code when an interrupt occurs.

                  If you use only the second line (ISR_Counter_StartEx(Routine);) it will still work well and will have the useless instructions removed.