I am working on an IR obstacle detector for a mobile robot. As part of this, I need to toggle some IR LEDs on and off at 38khz. This needs to occur for a duration of 600uS. I am trying to do this with a PIC16F628a.
If I understand correctly, the internal oscillator will always operate at 4mhz. And one instruction cycle takes 4 clock cycles. So to operate the LEDs at 38khz, one cycle of my loop should take 26 instruction cycles. And to pulse that for 600uS, I should repeat the loop 23 times (gets me to 597uS).
My questions boil down to:
Is the internal clock accurate enough for this task?
How accurate is the simulator that comes with the MPLAB IDE?
Is my math right for the above loops?
When I run this in the MPLAB simulator, everything works okay. But when I run it on the chip, the IR detector module (sensitive to 38khz modulation) never picks up anything.
I created a much simpler program to toggle a red LED on/off every .3 seconds. This worked okay in the simulator. On the actual chip, it was changing state much faster than every .3 seconds. This is why I think the problem is that I don't quite get what's going on with the instruction cycle speed.
Any help would be greatly appreciated. Thanks!
[edit]Replaced use of "CPU cycle" with "instruction cycle".[/edit]