From your example it should be obvious that COUNTER_PERIOD is not an integer multiple of TMR_PERIOD. After adding TMR_PERIOD to my_counter and testing the result for >= to COUNNTER_PERIOD some number of times you will find the comparison to be true. By subtracting COUNTER_PERIOD from my_counter the result will be between zero and TMR_PERIOD. This new initial value will correct for the previous counting of too many TMR_PERIOD's.
100,000 / 512 = 195 Remainder 160
So on the first cycle you will count 196 TMR_PERIOD's
512 * 196 = 100,352
On the second cycle you will count 195 TMR_PERIOD's
352 + 195*512 = 100,192
On the third cycle you will count 195 TMR_PERIOD's
192 + 195*512 = 100,032
On the fourth cycle you will count 196 TMR_PERIOD's
32 + 196*512 =100,384
and so on...
Does that help?
100,000 / 512 = 195 Remainder 160
So on the first cycle you will count 196 TMR_PERIOD's
512 * 196 = 100,352
On the second cycle you will count 195 TMR_PERIOD's
352 + 195*512 = 100,192
On the third cycle you will count 195 TMR_PERIOD's
192 + 195*512 = 100,032
On the fourth cycle you will count 196 TMR_PERIOD's
32 + 196*512 =100,384
and so on...
Does that help?