da_Alex
da_Alex

Reputation: 68

Pic Microcontroller - how to calculate time between 2 events?

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

Answers (2)

user3334978
user3334978

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

Deamonata
Deamonata

Reputation: 45

It depends on the micro really, without that it is next to impossible to know what timers are available. However you should be able to find example code on the Texas Instruments website, I can point you to the msp430 example code here

Upvotes: 0

Related Questions