Reputation: 68
I have to measure the time between 2 events (pressing buttons), like this:
if button1 is pressed -> start the timer if button2 is pressed -> stop the timer
print the time (in milliseconds) between the events
i am using the CCS Compiler and the microcontroller is a PIC18F27J53.
please can someone give me an example code for this? would be very nice
greetz Alex
Upvotes: 1
Views: 3532
Reputation: 28
I would fire the timer once the rising or falling edge of your first button is detected, then the timer would count until the second button is detected. When this happens, store the timer value in a variable and clear it for the next reading.
Take in consideration that PIC micro's clock cycle is 4 oscillator cycles so your system frequency would really be your oscillator source frequency divided by four (FOSC/4).
Also pay attention at your timer pre-scaling setting. if its 1:1 you know each timer tick is 1/(FOSC/4) seconds.
What's your output interface? Is it an LCD screen? Some LEDs on a port? Are you adding a watch window in your debugger?
Hope this helps
Upvotes: 1