9 Replies Latest reply on Jan 5, 2016 2:06 PM by JoMe_264151

    Millisecond stopwatch

      Hello, I am trying to create an accurate millisecond stopwatch which records the timing of external triggers. I would like to be able to record hours long samples but accurately time the stopwatch down to milliseconds. 


      At the moment, I feel like my counter (see attached) is not recording accurately. 


      For testing, my project has an internal 1s PWM, which when measured on a  scope is quite accurate.


      This PWM fires an interrupt, which reads my Counter (which counts on an 1kHz clock). The main loop then prints this count number to my LCD, as well as the difference from the previous reading. I have as minimal work between interrupt fire and counter read as I can. 


      The problem is, when I print the difference to my screen for the every 1s count read, I consistently get a difference of 993 counts, instead of the 1000 counts I expect. 




      Can anyone offer any advice on how to improve the accuracy of my stopwatch counter? 

        • 1. Re: Millisecond stopwatch

          Use the capture feature of the counter. It allows a hardware signal to store the current counter value in a FIFO 8the capture register). When you then configure the counter to reset on capture, you are done :)

          • 2. Re: Millisecond stopwatch

            Thank you.


            I've now tried the capture register part, but the difference was still coming out as 993.


            Though, I have another question now. I'm using a 1khz signal into my PWM, which is generating a 1s signal, but according to the module it takes 992 to create a period of 1s.   Shouldn't this be 1000 for a 1s period?  Am I misunderstanding the PWM, or the Clock itself?

            • 3. Re: Millisecond stopwatch

              Your 1 kHz clock runs on 992.97Hz because you derive it from the 32.768kHz source from which you cannot get a true 1kHz.


              Change your settings for the clocks and timers to get a clean source and count.





              • 4. Re: Millisecond stopwatch

                That was the problem. Thank you.

                • 5. Re: Millisecond stopwatch

                  You are always welcome.





                  • 6. Re: Millisecond stopwatch

                    Ok, I have a new issue.


                    I am using comparators on an external signal (on analog high impedance pins) as the input to my counter capture, which is set to capture at either edge.


                    I know for a fact that my analog signals are taking 28800 ms and  10 min , respectively, to switch high/low so the edges should at those time intervals. However, currently my LCD is still showing mostly 505ish ms (from my CyDelay(500) ) for both signals.


                    My LCD should only be showing numbers read from the Counter_n_Capture() api,  so this seems to indicate my capture FIFO is fireing all the time, but I can see the signals hold steady.  Is there anything else that could be fireing my captures, or could I be using something wrong? 


                    My project is attached, and I appreciate any advice.

                    • 7. Re: Millisecond stopwatch

                      You must reset the counter's interrupt cause by  counter_ReadStatusRegister(), The capture status bit is sticky and has to be read.





                      • 8. Re: Millisecond stopwatch

                        Thank you.
                        however, that doesn't explain why the actual value in the Capture register seems to be constantly updating.


                        I've tried clearing by Counter_1_ReadStatusRegister() inside the isr caused by the capture, and that hasn't changed the numbers at all. I still see the 505ish ms from the CyDelay,  instead of the  2880 ms I know the signal is taking.  


                        I've got my capture set to both edges, and the comparator should only be transistioning up/down every 2880 ms. So, I can't figure out why my difference from previous capture to current capture whould be 505 unless it's constantly capturing for some reason. 




                        Thank you for any assistance. 

                        • 9. Re: Millisecond stopwatch

                          As a try I would suggest to enable hysteresis in comparators to avoid oscillation.