I'm using a timer to count how long a pin is high for. I'm currently getting a value that is 80% of the value that it should be for example if the pin is high for 2 seconds I get 1.66 seconds.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I'm using a timer to count how long a pin is high for. I'm currently getting a value that is 80% of the value that it should be for example if the pin is high for 2 seconds I get 1.66 seconds.
Zach Szczesniak said:I'm using a timer to count how long a pin is high
Good that - yet you've not revealed your problem-solving technique in (usable) detail - is that not so?
It often proves wise to analyze the potential "cause" of such issues. Have you considered your (possible) error sources?
It is extremely unlikely that the Timer - or any value it acquires - proves wrong. Your methods - and the accuracy of the MCU's System Clock - rise FAR Higher on the "suspect list."
Questions posed aim at assisting "you" to implement the necessary "code implementation" corrections...
[edit] while there is a 30 minute difference between poster's timestamp & my own - his 2nd post here was not present when I composed the above.
The code you show fails to "loop" - thus it is a "one & done" - surely that's NOT what you intended.
I continue in the belief that your "Answers to the questions posed" will lead to your success...
Follows - a brief attempt to strengthen the code blurb earlier provided:
This method "follows" your earlier code's lead - yet adds the (required) looping structure. It must be noted - that "sitting w/in those loops" blocks the MCU from anything but "interrupt servicing." For a simple measurement - that (may) be ok - but not for general MCU usage.
Converting your code from such "loops" into "Interrupt based" prevents the code from "blocking" - enabling the MCU to perform (other) needed tasks.
To achieve such "interrupt operation of your timer:"
Again - this interrupt-based method "Frees the MCU for other activities" - and while adding complexity - most always is preferred...
Unknown is how you arrived at the variable "time" being divided by 80,000. It is assumed your System Clock was at/near 80MHz - thus each Timer "Tick" is 12.5nS. It is suspected that your use of a 32 bit timer will "Prevent Timer Roll-Over" - thus expanding the range of your measurement capability... (to at/around 53 seconds - if my late night math proves correct)