Exactly 40Khz or thereabouts?
40Khz has a period of 25uS which means you have a 12.5uS window per half cycle... hard to achieve using 1uS timing intervals at 4MHz Clock... at 8MHz Clock you can do it... but here we go... give it a shot and see how close you get with a scope...
For CounterA=1 to 16
Toggle PulsePin
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
Next CounterA
I've made some assumptions... you're running at 4MHz and that the FOR/NEXT takes 1uS and the TOGGLE takes 1uS, so there's ten 1uS Delays...
Now if you can't get close enough to 40kHz by adding or subtracting @NOP's try this one...
For CounterA=1 to 8
High PulsePin
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
Low PulsePin
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
@NOP
Next CounterA
If removing a NOP, try it from BOTH sections, but in the end you might find that you have one less NOP in one or other section... you may end up with an assymetric waveform, but it will be 40kHz.
It's play time...




Bookmarks