rhfarm
 
 
  Joined: 04 Apr 2006 Posts: 1
  
			
			 
			 
			 
			
			
			
			
			
			
  
		  | 
		
			
				| 18F452 Timer | 
			 
			
				 Posted: Tue Apr 04, 2006 4:09 pm     | 
				     | 
			 
			
				
  | 
			 
			
				  
 
I am trying to program a 40 Khz squarewave output on port B bit 2.  I am using the timer interupt and timer1.  The problem is that I can not get a frequence higher then around 31 KHz even when setting the 16 bit counter to start counting up from 0XFFE0.  There seams to be around 16 us of overhead .  I am using a 20 MHz clock and the timer should be incremented ever 200 ns.  The code is listed below.  Would be thankfull for any feed back.
 
 
 
 
#include <18f452.h>
 
 
#device *=16 adc=10 //icd=true
 
#use delay (clock=20000000)
 
#fuses HS,PUT,NOPROTECT,NOBROWNOUT,NOWDT,CCP2B3,NOSTVREN,NOLVP
 
 
 
 
int8  z=0;
 
 
// This interrupt is used to Drive LED at 40 KHz
 
// is automatically called ever 25us.
 
#INT_TIMER1
 
void wave_timer() {
 
   int i;
 
 
   set_timer1(0xFFF0);
 
 
 
   if(z){
 
      output_low(PIN_B2);
 
		z=0;
 
	}
 
 
   else {
 
      output_high(PIN_B2);
 
		z=1;
 
	}
 
 
}
 
 
 
 
main()   {
 
 
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
 
   enable_interrupts(INT_TIMER1);
 
   enable_interrupts(GLOBAL);
 
 
   while(true){
 
   }
 
 
} | 
			 
		  |