| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			smashHIT
 
 
  Joined: 28 Nov 2004 Posts: 14
  
			
			 
			 
			
			
			
			
			
			 
			
  
		  | 
		
			
				| Wrong # of cycles in delay_cycles...? | 
			 
			
				 Posted: Thu May 05, 2005 5:04 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu May 05, 2005 5:12 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Any instruction that does a jump takes two cycles.
 
Look at it again. | 
			 
		  | 
	
	
		  | 
	
	
		
			smashHIT
 
 
  Joined: 28 Nov 2004 Posts: 14
  
			
			 
			 
			
			
			
			
			
			 
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu May 05, 2005 5:21 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Yes, forgot about about that NOP due to the jump. Thanks!
 
   | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah Guest
 
 
 
 
  
			
			
			
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 06, 2005 4:13 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 06, 2005 7:00 am     | 
				     | 
			 
			
				
  | 
			 
			
				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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 06, 2005 11:59 am     | 
				     | 
			 
			
				
  | 
			 
			
				Looks like you could accomplish what you are doing mor precisely using PWM. _________________ -Matt | 
			 
		  | 
	
	
		  | 
	
	
		
			smashHIT
 
 
  Joined: 28 Nov 2004 Posts: 14
  
			
			 
			 
			
			
			
			
			
			 
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri May 06, 2005 3:23 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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!    
 
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
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon May 09, 2005 2:37 pm     | 
				     | 
			 
			
				
  | 
			 
			
				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 | 
			 
		  | 
	
	
		  | 
	
	
		 |