Running @ 4 MHz your machine cycle is 1 µs, so it´s not possible to achieve 6.25 µs (40 KHz @ 25% duty cycle).
If you need "exactly" 6.25 µs on and 18.75 µs off (40KHz @ 25%) you will have to choose among these clock
frequencies.
Xtal Freq........# machine cycles on.......# machine cycles off
MHz
1.280.........................2................................6
1.920.........................3................................9
2.560.........................4...............................12
3.200.........................5...............................15
3.840.........................6...............................18
4.480.........................7...............................21
5.120.........................8...............................24
5.760.........................9...............................27
6.400........................10..............................30
7.040........................11..............................33
7.680........................12..............................36
8.320........................13..............................39
8.960........................14..............................42
9.600........................15..............................45
10.240.......................16.............................48
10.880.......................17.............................51
11.520.......................18.............................54
12.160.......................19.............................57
12.800.......................20.............................60
13.440.......................21.............................63
14.080.......................22.............................66
14.720.......................23.............................69
15.360.......................24.............................72
16.000.......................25.............................75
.
.
.
This type of problem is a good example of those you get better results using assembly,
Below a possibility for a 16 MHz Xtal (thanks to PICLOOPS
http://www.biltronix.com/picloops.html)
Code:
#asm
LOOP movlw 0xff
movwf GPIO
movlw D'7'
movwf CounterA
loopon decfsz CounterA,1
goto loopon
nop
movlw 0
movwf GPIO
movlw D'23'
movwf CounterA
Loopoff decfsz CounterA,1
goto Loopoff
nop
goto LOOP
#endasm
Because in PIC12F629, The GPIO3 is only Input!!!
I like to use binary number in these registers...
Change to
Code:
GPIO= 0xFF;
__delay_us(6);
GPIO= 0;
__delay_us(18);