Do you intend to do anything else with your PIC while your sending 40KHz pulses? If so, your approach is wrong, since all your code is tied up doing software delays.
The 16F628A has a hardware module that will output various frequencies while you do other code processes. I recommend the CCP module which does PWM.
You can set up PWM for a variety of frequencies based on Timer2, its prescaler setting, and the PR2 compare register value. You would likely want 50% duty cycle and that can be set up in CCPR1L and CCPCON<5..4> bits.
Assuming a 4MHz oscillator, 40KHz at 50% duty cycle can be set up with these registers:
PORTB, pin3 is the PWM output. You can easily turn off the output by CCP1CON = 0.
Refer to the datasheet for more information on this hardware feature.
Oct 01, 2007 Rating
PIC Microcontroller assembler delay fixes and debug by: John Main
Hi Nicolae,
The way to solve these kinds of problems is always to go to the lowest level i.e. assembler so you need to generate the assembler code output - there is usually an option or it is generated in the same directory as the source.
When you do this you'll be able to see the exact code generated by the delayUs(12) functions etc. It sounds like this routine is no good for low delay values (you have to be very careful i.e. the compiler creators ;-) must be careful) when creating these types of functions. MikroC ones seem good but you should be able to sort it out using the assembler output. Note make a small change and compare the assembler outputs e.g. delayUs(13).
I don't know why you get 300ms but just looking at the assembler will help.
The delay routine you have won't work because the dollar sign is the current program counter position and is not usually available in embedded assembler (in C code). To get round it either create a new label for each statement and use goto label on each line or replace each goto with two nops (the delay uses goto as it is a jump and takes two instructon cycles so it is more efficient).
Note: loops take different amounts of time depending on whether they continue or exit.
See also the ultrasonic range finder on this site - the code does exactly as you are trying to do (with MikroC compiler though)