OK so the code sort of works. I used a different serial-to-usb cable and my problem was solved.
The problem now is , I am getting wrong gap with the right gap value.
For example: Using 20 Khz, 50% duty cycle, Fosc = 8MHz
F= 20 Khz, T = 5 *10^-5 s , Gap = 2.5*10^-5 sec
Count = 2.5*10^-5 / (1/2MHz) = 50
I do get 50 counts out but along with other values such as 9, 11 etc
Example output :
50
50
50
50
50
9
50
9
50
9
11
14
27
38
45
50
50
50
50
50
50
9
50
Now where am I going wrong?