Do someone know how to measure frequency (approximately 500kHz) with PIC18? (PIC18F4550) Do someone have any sample code?
Thank you
How to measure frequency?
Input RC0, Q 48 MHz, PIC18F4550, Time base 10 ms, Max frequency 6.5535 Mhz, resolution 100 Hz.
Was made on the fly ...
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.
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
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
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?
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;