CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Tight loop timing

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

Tight loop timing
PostPosted: Sun Jul 24, 2005 10:52 am     Reply with quote

PCWH v. 3.228, 18F452 & 18F4610 running at 4 MHz.

I'm trying to flash an IR LED. Specifically, I'm trying to recreate the on/off etc codes to run A/V equipment. This involves "bursting" the IR LED at about 38 kHz, 50% duty cycle, then turning it off, then "bursting" again, etc.

Anyway, I can't use the PWM module(s), so I was bit-banging it myself.

I know that at 4 MHz, the system clock is only 1 us. To create the 38 kHz IR carrier, I coded a loop as follows:

Code:
for (i = 0; i < 328; i++) { // lead in burst of 8.5 ms
      output_high(PIN_C0);
      delay_us(12);
      output_low(PIN_C0);
      delay_us(12);
   }


One instruction to set the pin high, another 12 in the delay, one more to set the pin low, then wait another 12. Repeat. A total of 26 cycles, 26 us, which corresponds to a frequency of 38.5 kHz, which is what I need. Only problem is that if you actually measure this waveform with a scope, it's not even close to a 50% duty cycle, and not 38.5 kHz either.

Turns out the high time is 13 us, exactly as I thought, but the low time is about 19 us. Enough to throw it off so that a standard IR decoder can't detect it.

I had to tweak the second delay to just 9 us. With that, the output waveform is a perfect 50% duty cycle, with a 26 us period.

In addition, the lead in pulse must be about 8.5 ms. 328 times through the loop x 26 us = 8.53 ms. Only it isn't. In order to get that lead in pulse to be 8.5 ms, I have to change the loop to run 260 times, not 328. This is with the low time delay changed to 9 us.

Obviously, the loop overhead is not being taken into account, by me or by CCS. My question is this: doesn't CCS take these things into account? I've done loops with tight timing restrictions on earlier versions of the compiler without problems. Is this a bug?

And I know, I should have used the PWM module.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Jul 24, 2005 12:02 pm     Reply with quote

Quote:
Obviously, the loop overhead is not being taken into account, by me or by CCS. My question is this: doesn't CCS take these things into account? I've done loops with tight timing restrictions on earlier versions of the compiler without problems. Is this a bug?


Now why do you think they should take into account the instructions for the loop? They execute the delay routine. The loop overhead is after the delay and the error is in your technique.
newguy



Joined: 24 Jun 2004
Posts: 1909

View user's profile Send private message

PostPosted: Sun Jul 24, 2005 12:14 pm     Reply with quote

Mark wrote:
Now why do you think they should take into account the instructions for the loop? They execute the delay routine. The loop overhead is after the delay and the error is in your technique.


I think I know what was causing me so much grief.

I prototyped this system about 6 months ago and was able to control anything I wanted using this code. The difference was that when I prototyped it, I'm pretty sure I was running the PIC at 20 MHz, not 4. The overhead in executing the loops is then minute compared with the large delays (12 us).

Let this be a lesson to everyone: keep good notes!!! Embarassed
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group