How to measure frequency?

General discussion on mikroPascal.
Post Reply
Author
Message
JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

How to measure frequency?

#1 Post by JDvorak » 05 Feb 2009 21:39

Do someone know how to measure frequency (approximately 500kHz) with PIC18? (PIC18F4550) Do someone have any sample code?
Thank you

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 05 Feb 2009 22:14

Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

#3 Post by JDvorak » 05 Feb 2009 22:18

Thank you, I have seen it before, but this is project for PIC16F and I have 18F. Unfortunately, I do not have any experience with Timers or CPP module :-( I need to measure number of pulses for every 10ms (not exactly, just about) and send this number by UART.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 05 Feb 2009 22:46

what is you MCU clock ?
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

#5 Post by JDvorak » 05 Feb 2009 22:56

I use PIC18F4550 (PIC with USB, I use HID) according an example - 8MHz external oscillator with PLL (I guess 48MHz)

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#6 Post by yo2lio » 05 Feb 2009 23:09

Input RC0, Q 48 MHz, PIC18F4550, Time base 10 ms, Max frequency 6.5535 Mhz, resolution 100 Hz.

Code: Select all

program frecv_test;

var frecv_buf : word;
    frecv_available : boolean;
    frecv : dword;
    count : byte;
    
procedure interrupt;
begin
  if TestBit(PIR1,TMR2IF) = 1 then      // 5 ms TMR2 Q=48 MHz
    begin
      PIR1.TMR2IF:=0;
      if inc(count) > 1 then            // 10 ms here
        begin
          T1CON.TMR1ON := 0; // Stop counter 1
          FSR2Ptr := @frecv_buf;  // save counter
          POSTINC2 := TMR1L;
          INDF2 := TMR1H;
          TMR1L := 0;   // clear counter
          TMR1H := 0;
          T1CON.TMR1ON := 1;  // Start counter 1
          count := 0;
          frecv_available := true;
        end;
    end;
end;

Procedure Init;
Begin
  frecv_available := false;
  T1CON := 2; // external clock source
  count := 0;
  TMR1L := 0;
  TMR1H := 0;
  PIE1 := %00000010;
  PR2 := 249;
  T2CON := %01110011;          // prescaler 16, poscaler 15
  TMR2 := 0;
  T2CON.TMR2ON := 1;           // start TMR2
  INTCON := %11000000;         // TMR2 interrupt at 5 ms , Q 48 MHz
End;

begin
  Init;
  while true do
    begin
      if frecv_available = true then
        begin
          frecv_available := false;
          frecv := frecv_buf * 100;
          // your code here ....
        end;
    end;
end.
Was made on the fly ...
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

#7 Post by JDvorak » 06 Feb 2009 16:18

WOW ... thank you milion times! I will try it in the evening, but it looks very very nice. Thank you again!

JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

#8 Post by JDvorak » 06 Feb 2009 21:12

I have one question ... I need to send the number via USB, and when I add your code into USB HID example, USB does not work
But, if I comment several lines (in code down), everythink around USB is OK. Do you know where is any mistake?

Code: Select all

procedure Init_Main;
begin
//  Pwm1_Init(5000);
//  Pwm1_Start;
//  nop;
//  Pwm1_Change_Duty(30);
  
  frecv_available := false;
  T1CON := 2; // external clock source
  count := 0;
//  TMR1L := 0;
//  TMR1H := 0;
//  PIE1 := %00000010;
//  PR2 := 249;
  T2CON := %01110011;          // prescaler 16, poscaler 15
  TMR2 := 0;
  T2CON.TMR2ON := 1;           // start TMR2
  INTCON := %11000000;         // TMR2 interrupt at 5 ms , Q 48 MHz
  
	INTCON2 := 0xF5;
	INTCON3 := 0xC0;
	RCON.IPEN := 0;                          // Disable Priority Levels on interrupts

	PIE2 := 0;
	PIR1 := 0;
	PIR2 := 0;

	ADCON1 := ADCON1 or 0x0F;                         // Configure all ports with analog function as digital
	//--------------------------------------
	// Ports Configuration
	//--------------------------------------
	TRISA := 0xFF;
	TRISB := 0xFF;
	TRISC := 0xFF;
	TRISD := 0;
	TRISE := 0x07;

	LATA := 0;
	LATB := 0;
	LATC := 0;
	LATD := 0;
	LATE := 0;
end;

JDvorak
Posts: 64
Joined: 21 Jan 2007 23:58
Location: Czech Republic, Europe
Contact:

#9 Post by JDvorak » 06 Feb 2009 23:33

And one more question - can I use one PWM simultaneously? I need measure frequency as feedback to set up the duty of PWM. Just - it is possible to use with PIC18F4550 USB+PWM+measure frequency?

piort
Posts: 1379
Joined: 28 Dec 2005 16:42
Location: Laval,Québec,Canada,Earth... :-)
Contact:

#10 Post by piort » 07 Feb 2009 01:45

hi, im not sure at 100% because i dont have time to pass trhough the datasheet but... your frequency counter use timer, pwm use timer and usb use timer so maybe you can have some timer use conflict....


hth a bit ;-)

Post Reply

Return to “mikroPascal General”