| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				| Capture Mode Input Frequency Measurement |  
				|  Posted: Mon Sep 02, 2024 7:29 am |   |  
				| 
 |  
				| Hi everyone I wanna measure frequency by capture input mode(PING0 (eccp3)) but I do not know anything about input capture mode.I researched some web sites and examples but I could not understand anything.Please help me. 
 #include <18F67K22.h>
 #use delay(xtal=20MHz,clock=80MHz)
 
 
 #INT_CCP3
 void func1()
 {
 
 }
 
 void main()
 {
 
 
 
 
 setup_ccp3(ccp_capture_re);
 CCP_3_HIGH=0x00;CCP_3_LOW=0x00;clear_interrupt(INT_CCP3);
 
 
 
 
 enable_interrupts(INT_CCP3);
 
 enable_interrupts(GLOBAL);
 
 while(TRUE)
 { }
 
 }
 
 Last edited by ilker07 on Mon Sep 02, 2024 8:25 am; edited 1 time in total
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Sep 02, 2024 8:10 am |   |  
				| 
 |  
				| Let's start with a couple of basic things: 
 1) What is the maximum clock frequency the 67K22 supports?. Hint it is not
 80MHz. Not going to work.
 2) You can't use the same CCP, both for PWM, and capture. One peripheral,
 one job. Not going to work.
 3) To capture, the CCP has to use a timer. Timer 1 or Timer 3. The default
 is Timer 1. This needs to be setup, or there is nothing to capture.
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Sep 02, 2024 8:26 am |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | Let's start with a couple of basic things: 
 1) What is the maximum clock frequency the 67K22 supports?. Hint it is not
 80MHz. Not going to work.
 2) You can't use the same CCP, both for PWM, and capture. One peripheral,
 one job. Not going to work.
 3) To capture, the CCP has to use a timer. Timer 1 or Timer 3. The default
 is Timer 1. This needs to be setup, or there is nothing to capture.
 | 
 
 
 Sorry I edited option 2.I am using external crystal(20 mhz)
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Sep 02, 2024 8:27 am |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | Let's start with a couple of basic things: 
 1) What is the maximum clock frequency the 67K22 supports?. Hint it is not
 80MHz. Not going to work.
 2) You can't use the same CCP, both for PWM, and capture. One peripheral,
 one job. Not going to work.
 3) To capture, the CCP has to use a timer. Timer 1 or Timer 3. The default
 is Timer 1. This needs to be setup, or there is nothing to capture.
 | 
 
 
 
 How will the setting be? I dont know that part.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Sep 02, 2024 9:12 am |   |  
				| 
 |  
				| The point is the maximum speed that chip supports is 64MHz. With a 20MHz crystal, you can run at 20MHz (not 80MHz). With a 16MHz
 crystal you can run at 16MHz,, or 64MHz (using the 4* PLL).
 You cannot run this chip at 80MHz.
 
 Now on the settings, you need to say what you are actually trying to do?
 What is the frequency range you need to handle?. What resolution do you
 need?.
 Do you want a PWM as well?. If so this will have to be setup on another
 CCP.
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 2:04 am |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | The point is the maximum speed that chip supports is 64MHz. With a 20MHz crystal, you can run at 20MHz (not 80MHz). With a 16MHz
 crystal you can run at 16MHz,, or 64MHz (using the 4* PLL).
 You cannot run this chip at 80MHz.
 
 Now on the settings, you need to say what you are actually trying to do?
 What is the frequency range you need to handle?. What resolution do you
 need?.
 Do you want a PWM as well?. If so this will have to be setup on another
 CCP.
 | 
 
 But I set timer3 settings for 80 mhz ( setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);set_timer3(63036);  //it gives me 1 ms interrupt )and I wanna measure range in 50 hz-10000hz.I do not want PWM.I addet it accidently.
 
 Last edited by ilker07 on Tue Sep 03, 2024 4:48 am; edited 1 time in total
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 4:16 am |   |  
				| 
 |  
				| By thw way how can I make it 64 mhz by using 20mhz external osc? |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 4:34 am |   |  
				| 
 |  
				| Start with the most basic thing. READ THE CHIP's DATA SHEET. 
 Page 3. Top right. Special Microcontroller features. Third line down:
 
  	  | Quote: |  	  | Operating Speed up to 64 MHz
 
 | 
 
 Now if your chip is running at 80MHz (very unlikely), it'll probably kill itself
 in a few days. Power drawn is pretty much linearly related to speed. At
 80MHz, the chip will draw probably about 30mA, and will overheat quickly.
 Much more likely it is actually running at 20MHz. (the PLL generally will not
 work if asked to go much beyond it's rating). It's maximum input frequency
 is 16MHz. From the data sheet again. Section 3.1:
 
  	  | Quote: |  	  | There is also an option for running the 4xPLL on any of
 the clock sources in the input frequency range of 4 to
 16 MHz.
 
 | 
 4 to 16MHz......
 Generally if you try to run much beyond the rating, the chip will actually
 run off a division of what you are trying to do. That's why I think you are
 probably running at 20Mhz.
 
 What on earth makes you think that timer setting gives a 1mSec interrupt?.
 It won't. Even at 80MHz, that will not give 1mSec.
 You cannot set the timer to 65536. The timer value register is a 16bit
 register. Accepts values 0 to 65535.
 
 You need to start one step at a time. Run something basic like a flash an
 LED program. Get it working and at the right speed. Then single steps.
 You are trying to run, before you can even crawl.
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 4:39 am |   |  
				| 
 |  
				| #use delay(xtal=20MHz, clock=64MHz)  how can I use this?It is not allowed this way because it is not x4.How can I make it 64mhz by using 20 mhz ext.crystal? |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 4:42 am |   |  
				| 
 |  
				| #use delay(xtal=20MHz, clock=80MHz) I think this way I generated 20mhz clock speed and I think this part(clock=80MHz) is nonsense. 
 Last edited by ilker07 on Tue Sep 03, 2024 4:56 am; edited 1 time in total
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 5:02 am |   |  
				| 
 |  
				| #include <18F67K22.h> #use delay(xtal=20MHz,clock=80MHz)
 #FUSES NOWDT
 #FUSES WDT128
 #FUSES NOXINST
 #FUSES NOBROWNOUT
 #FUSES PROTECT
 #FUSES NOMCLR
 
 
 #define LED       pin_d1
 
 
 
 unsigned int16 timeCounter=0;
 #int_timer3
 void timer3_interrupt()
 {
 
 
 timeCounter++;
 if(timeCounter==1000){
 timeCounter=0;
 output_toggle(LED);
 }
 set_timer3(63036);  //1 ms
 }
 
 
 
 
 
 
 void main() {
 
 
 
 output_high(LED);
 setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
 set_timer3(63036);
 enable_interrupts(INT_TIMER3);
 
 
 enable_interrupts(GLOBAL);
 
 while(True) {
 
 }
 
 }
 
 That gives me 1 second blink.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 5:42 am |   |  
				| 
 |  
				| You are overclocking the chip by 25%. It will not be reliable long term or if the weather gets hot.
 Think again.
 You will almost certainly find that several of the peripherals will not work
 properly once you go beyond the ratings.
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 5:48 am |   |  
				| 
 |  
				|  	  | Ttelmah wrote: |  	  | You are overclocking the chip by 25%. It will not be reliable long term or if the weather gets hot.
 Think again.
 You will almost certainly find that several of the peripherals will not work
 properly once you go beyond the ratings.
 | 
 
 OK.What should I do? How can I make it bigger speed with 20 mhz?
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 6:42 am |   |  
				| 
 |  
				| Using the crystal you can't. You need a 16MHz crystal.
 You can run with a slightly reduced accuracy, using the internal oscillator
 instead of the crystal.
 |  | 
	
		|  | 
	
		| ilker07 
 
 
 Joined: 03 Jun 2022
 Posts: 74
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Tue Sep 03, 2024 6:46 am |   |  
				| 
 |  
				| OK then let s edit this way and get back to main topic 
 #include <18F67K22.h>
 #use delay(internal=16MHz,clock=64MHz)
 
 
 #INT_CCP3
 void func1()
 {
 
 }
 
 void main()
 {
 
 
 
 
 setup_ccp3(ccp_capture_re);
 CCP_3_HIGH=0x00;CCP_3_LOW=0x00;clear_interrupt(INT_CCP3);
 
 
 
 
 enable_interrupts(INT_CCP3);
 
 enable_interrupts(GLOBAL);
 
 while(TRUE)
 { }
 
 }
 
 How can I measure the frequency with this way?
 |  | 
	
		|  | 
	
		|  |