Clock speeds and CPU cycle

Status
Not open for further replies.

loosewire

New Member
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]
 
Thanks Nigel. I just took a quick look and will go over it in depth tomorrow. I did notice that your IR_pulse and NO_pulse routines occupy 26 instruction cycles and that you call them 23 times or a multiple of 23. I guess I had that part right, so my issue probably lies somewhere else.

One thing I noticed though: In the IR_pulse routine, the LED is on for about one third of the time instead of one half of the time. Why is that?
 
loosewire said:
One thing I noticed though: In the IR_pulse routine, the LED is on for about one third of the time instead of one half of the time. Why is that?

To reduce power consumption, the frequency remains the same though.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…