Help with Free Running Timers

Ian Shannon Weber wrote 04/29/2020 at 00:51 0 points

Technical Context: I am working on a project that uses the PIC18F45K20. This is a project to learn more and help develop my skills. I am making a cable tester so I am firing an output and watching an input.

Problem: I am having issues comprehending how a free running timer works. I understand that provided that I have not overflowed my timer. When I call a function, get the time from my timer buffer if it is the "first" call, else see if CURRENT_TIME - START_TIME = DESIRED_ELAPSED_TIME. I am just not sure how to handle if my CURRENT_TIME is less than my START_TIME.

Does anybody have a guide or advice on how to understand this?

I have tried the following trail of logic and failed on some level:

If CURRENT_TIME is greater than START_TIME than does CURRENT_TIME minus START_TIME equal DESIRED_ELAPSED_TIME or more? If yes do some code.

Else If CURRENT_TIME is less than START_TIME than does 0xFFFF (16 bit timer maximum value) minus START_TIME plus CURRENT_TIME equal DESIRED_ELAPSED_TIME or more? If yes do some code.

Else return to main loop.