CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

50mhz Frequency counter

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ISAAC AIYANYO
Guest







50mhz Frequency counter
PostPosted: Wed Apr 16, 2003 7:29 am     Reply with quote

Hello Picstars
i need your help here.
i am trying to use the pic as a frequency counter using a 16F873 but i don't seems to have the example file
EX_FREQC.C even though its listed in the manual.
Would anyone be kind enough to post the program if you have it.
have anyone done anything like this before i need to measure the frequency of a square wave with a range of 100Hz to 10Khz
Any help rendered would be much appreciated

Isaac

___________________________
This message was ported from CCS's old forum
Original Post ID: 13708
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: 50mhz Frequency counter
PostPosted: Wed Apr 16, 2003 12:17 pm     Reply with quote

:=
:= Hello Picstars
:= i need your help here.
:= i am trying to use the pic as a frequency counter using a 16F873 but i don't seems to have the example file
:=EX_FREQC.C even though its listed in the manual.
:= Would anyone be kind enough to post the program if you have it.
:= have anyone done anything like this before i need to measure the frequency of a square wave with a range of 100Hz to 10Khz
:= Any help rendered would be much appreciated
:=
--------------------------------------------------------

By using Google I found it very quickly. <a href="http://www.google.com" TARGET="_blank">http://www.google.com</a>
Here are the files:
<a href="http://www.cc.puv.fi/~t0101190/projekti/source/Examples/" TARGET="_blank">http://www.cc.puv.fi/~t0101190/projekti/source/Examples/</a>
I notice that CCS doesn't have NOLVP in the #fuses statement.
They really should have it in there, because the average person
does not use LVP, and it causes a lot of problems if you
forget to set it for NOLVP.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13712
Hans Wedemeyer
Guest







Re: 50mhz Frequency counter
PostPosted: Wed Apr 16, 2003 1:38 pm     Reply with quote

without prescaler I the pic can't count at 50 mega Hertz.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13719
isaac aiyanyo
Guest







Re: 50mhz Frequency counter
PostPosted: Thu Apr 17, 2003 6:37 am     Reply with quote

Thank you every much for the link
its very much appreciated.
i have ran the program but i am still very confused
with the results i am getting as could understand why parts of the program were commented out?
i want to use it for the frequency range 100Hz to 10Khz
but i am getting the following results

AT 100Hz i get 11000Hz to 12000Hz
AT 1KHz i get about 13870Hz
AT 100KHz i get about 27000Hz

Could someone please tell me where i am going wrong
my program is below
Hope to hear from you soon
Isaac
#if defined(__PCM__)
#include <16F876.H>
#fuses HS,NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,RESTART_WDT,ERRORS)
#bit t1_overflow=0x0C.0


// #bit t1_overflow=0xF9E.0 (PIC18, Reminder)

void main() {
int cycles8, cycles;
int32 freq;
long freqc_high;
long freqc_low;

while (TRUE) {
cycles8=0;
cycles=0;
freqc_high=0;
t1_overflow=0;
set_timer1(0);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
/* ___ wait one second ___ */
while (cycles!=0xFF) { //true=3, false=4
cycles8=0; //1 cycle
//start inner loop
while (cycles8!=0xFF) { //true=3, false=4
if (t1_overflow)//true=2,false=3 //----|
{t1_overflow=0;freqc_high++;}//6 cycles // |
else // |-- 8 cycles
{delay_cycles(5);} //----|
delay_cycles(62); //x
cycles8++; //1
///2 cycles to jump to top
//math: end inner loop
//math: total inner loop=((3+8+x+1+2)*255 + 4)*255
//math: if x=62.87781 then inner loops takes 5mil instructions
//math: if x=62 then inner loop takes 4942920, have to fill 57080 cycles
}
delay_cycles(216); //y
cycles++; ///1 cycle
///2 cylces to jump to top
//math: outer=(3+1+y+1+2)*255+4=57080
//math: y=(57080-4)/255)-(3+1+0+0+1+2)
//math: if y=216.827450980392156862745098039216 then outer loop cylces is 57080
//math: if y=216 then outer loop cycles is off by 211 cycles. z=211
}
delay_cycles(211); //z
/* ___ end waiting 1 second ___ */
setup_timer_1(T1_DISABLED); //turn of counter to prevent corruption while grabbing value
if (t1_overflow) //check one last time for overflow
freqc_high++;
freqc_low=get_timer1(); //get timer1 value as the least sign. 16bits of freq counter
freq=make32(freqc_high,freqc_low); //use new make32 function to join lsb and msb
printf("\%LU Hz\r\n",freq); //and print frequency

}
}


___________________________
This message was ported from CCS's old forum
Original Post ID: 13757
Mike Broom
Guest







Re: 50mhz Frequency counter
PostPosted: Tue Apr 22, 2003 7:56 am     Reply with quote

Below is a simple interrupt driven frequency counter which will work between approx 1Hz to 65.5kHz

Mike

Code:

#include <16F876.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//====================================================
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)

int16   Count;
int16   Frequency;
int1    OneSecondGone = 0;

void main(void) ;

#int_timer2
void t2_isr(void)
{
    static int8 RollOver = 0;

    Count = get_timer1();
    RollOver++;
    if (Rollover == 250)
    {
        OneSecondGone = true;
        Rollover = 0;
        set_timer1(0);       
        Frequency = Count;
    }
}

void main(void)
{
    set_tris_a(0);
    set_tris_c(255);
    setup_timer_2(T2_DIV_BY_4,250,16);
    setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
    enable_interrupts(int_timer2);
    enable_interrupts(global);
    while(true)
    {
        if (OneSecondGone)
        {
            if (Frequency > 999)
            {
                printf("\rFequency = \%1.4f kHz       ",(float)Frequency / 1000);
            }
            else
            {
                printf("\rFequency = \%lu Hz          ",Frequency);
            }
            OneSecondGone = false;
        }
    }
}

:=
:= Hello Picstars
:= i need your help here.
:= i am trying to use the pic as a frequency counter using a 16F873 but i don't seems to have the example file
:=EX_FREQC.C even though its listed in the manual.
:= Would anyone be kind enough to post the program if you have it.
:= have anyone done anything like this before i need to measure the frequency of a square wave with a range of 100Hz to 10Khz
:= Any help rendered would be much appreciated
:=
:= Isaac
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 13915
inmelody



Joined: 14 May 2013
Posts: 1

View user's profile Send private message

Re: 50mhz Frequency counter
PostPosted: Tue May 14, 2013 12:57 pm     Reply with quote

hi can you send me proteus schematic for this frequency counter. I try but i cant do it :( ...


Mike Broom wrote:
Below is a simple interrupt driven frequency counter which will work between approx 1Hz to 65.5kHz

Mike


#include <16F876.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//====================================================
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)

int16 Count;
int16 Frequency;
int1 OneSecondGone = 0;

void main(void) ;

#int_timer2
void t2_isr(void)
{
static int8 RollOver = 0;

Count = get_timer1();
RollOver++;
if (Rollover == 250)
{
OneSecondGone = true;
Rollover = 0;
set_timer1(0);
Frequency = Count;
}
}

void main(void)
{
set_tris_a(0);
set_tris_c(255);
setup_timer_2(T2_DIV_BY_4,250,16);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1);
enable_interrupts(int_timer2);
enable_interrupts(global);
while(true)
{
if (OneSecondGone)
{
if (Frequency > 999)
{
printf("\rFequency = \%1.4f kHz ",(float)Frequency / 1000);
}
else
{
printf("\rFequency = \%lu Hz ",Frequency);
}
OneSecondGone = false;
}
}
}
temtronic



Joined: 01 Jul 2010
Posts: 9121
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 14, 2013 1:56 pm     Reply with quote

I seriously doubt anyone here will send you a Proteus schematic.
We all KNOW Proteus is full of bug,errors and faulty DRCs and as such is a complete waste of time.

Please read PIC101 thread...


Also note that ANY floating point math takes a LOT of time on PICs, so if your signal source is not stable, you'll see 'funny' numbers.

hth
jay
dyeatman



Joined: 06 Sep 2003
Posts: 1912
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue May 14, 2013 2:33 pm     Reply with quote

inmelody,
Do you realize this thread is right at 10 YEARS old? I doubt that user is still in the forum any more
_________________
Google and Forum Search are some of your best tools!!!!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue May 14, 2013 2:44 pm     Reply with quote

temtronic wrote:
Also note that ANY floating point math takes a LOT of time on PICs, so if your signal source is not stable, you'll see 'funny' numbers.
It's true that floating point is relative slow but here the calculation is only being done one time per second so not a real problem. Though I would recommend using the %w specifier on the integer value instead of using floats, much faster and less memory usage.

About the schematic: very easy, just connect your frequency signal to the Timer1 external input pin. I'm too lazy to read the data sheet for the pin number, you'll have to do that yourself.
Ttelmah



Joined: 11 Mar 2010
Posts: 19238

View user's profile Send private message

PostPosted: Wed May 15, 2013 1:11 am     Reply with quote

There is though the problem that the multi-byte 'frequency' value will change during the calculation. So abnormal results would be the 'norm' (if you see what I mean....).

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed May 15, 2013 1:31 pm     Reply with quote

Ttelmah wrote:
There is though the problem that the multi-byte 'frequency' value will change during the calculation. So abnormal results would be the 'norm' (if you see what I mean....).
Nope, I don't see what you mean.
Maybe I'm missing something, but I'm aware of the problems related to non-atomic actions in combination with interrupts.
As I understand it the variable 'frequency' is only updated once every second in the ISR and then immediately afterwards the float calculation happens in main. So I don't see how this being a multi-byte variable would pose a problem during the calculation? The flag OneSecondGone should cover all problems unless the calculations take more than 1 second which with the code shown is not going to happen.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group