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

Wrong # of cycles in delay_cycles...?

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



Joined: 28 Nov 2004
Posts: 14

View user's profile Send private message ICQ Number

Wrong # of cycles in delay_cycles...?
PostPosted: Thu May 05, 2005 5:04 pm     Reply with quote

So, I'm trying to produce a 38,5kHz square wave on my PIC for an IR diod. I'm running a 16F874A in 4MHz, wich means that the internal clock is 1MHz. I have figured out that I need a pulse period of ~26us to achive 38,5kHz. Wich means that i can set a PIN to be high for 13us, then low for 13us and so on... right?
Well, i tried this code: (sample)
Code:
while(true){
     output_high(PIN_D1);
     delay_cycles(13);
     output_low(PIN_D1);
     delay_cycles(13);
}


But when looking in the asm listing, I don't end up with 13 cycles of delay, rather 8. Looks like this:
Code:
....................          delay_cycles(13);
0013:  MOVLW  04
0014:  MOVWF  20
0015:  DECFSZ 20,F
0016:  GOTO   015


Am I looking at this wrong?

By the way...
I'm trying measure a distance with my IR diodes, does anyone have any tips on that?

Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 05, 2005 5:12 pm     Reply with quote

Any instruction that does a jump takes two cycles.
Look at it again.
smashHIT



Joined: 28 Nov 2004
Posts: 14

View user's profile Send private message ICQ Number

PostPosted: Thu May 05, 2005 5:21 pm     Reply with quote

Yes, forgot about about that NOP due to the jump. Thanks!
Rolling Eyes
Ttelmah
Guest







PostPosted: Fri May 06, 2005 4:13 am     Reply with quote

Remember also that the 'output_high', and 'output_low', each take a cycle, and the while loop involves both a test, and a jump. You'd probably need the first delay to be twelve cycles (to allow for the output instruction), and the second to be 9 (guessing slightly here).

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri May 06, 2005 7:00 am     Reply with quote

How long a distance are you trying to measure? What resolution and accuracy do you need? What is the medium, air, water, vacuum?
_________________
The search for better is endless. Instead simply find very good and get the job done.
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Fri May 06, 2005 11:59 am     Reply with quote

Looks like you could accomplish what you are doing mor precisely using PWM.
_________________
-Matt
smashHIT



Joined: 28 Nov 2004
Posts: 14

View user's profile Send private message ICQ Number

PostPosted: Fri May 06, 2005 3:23 pm     Reply with quote

My mind is set on a miniature "radar" unit, but with IR-diodes. The medium will be air, and the maximum rage will be arund 40cm (with 5 different zones). It's just fur fun! Laughing
The diodes sit ontop of a servo, that spins 180 degrees, back and fourth.

Okey, so you say I should use PWM... how do I manage to get the dividers and stuff right for a 38,5kHz pulse?

I red that it was recomended to use another IC, like 555 or so, to get the modulation pulse. And then control that signal (on and off), with the PIC +some logic. Anyone familiar with that?
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Mon May 09, 2005 2:37 pm     Reply with quote

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,26,1); //(1/4MHz)*4*1*26 = 26us = 38.5Khz
SET_PWM_DUTY(52); //13us/(1*(1/4MHz))

should set things up for you. You will have to use RC2 though.
_________________
-Matt
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