Frequency Counter using PIC and PicBasic


Closed Thread
Results 1 to 32 of 32
  1. #1
    PICtron's Avatar
    PICtron Guest

    Default Frequency Counter using PIC and PicBasic

    Hello to everybody !

    I'm new to this forum and have a question to start with.

    I would like to build a frequency counter using a PIC like like 16F628 (or other if necessary) and a small LCD-Display.

    I have already setup the PIC and the 16x2 LCD on a piece of breadboard. The PIC is running on a 20MHz Crystal.

    So far I have managed to print characters to the LCD.

    My goal is to count frequencies from 0Hz up to 20MHz or whatever is possible using a PIC.

    The resolution should be as good as 1Hz (at least in the lower ranges).

    I have tried the COUNT statement but it didn't give the expected range and resolution.

    I have found some examples on the web, but thy're all written in ASM, and I don't know ASM.
    At least did these examples show that my goal should be achievable. (even up to 50MHz)

    Could one of the experts on the board give mesome hints how to solve the problem using PICbasic?

    many thanks !

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hi PICtron,

    Have a look at those thread here...

    http://www.picbasic.co.uk/forum/show...&threadid=1044

    http://www.picbasic.co.uk/forum/show...=&threadid=550

    Those could be good to start
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    Hi mister_e

    thanks for your response.

    I thought that I would have to use timers.

    but all this timer stuff with all those options and registers confuses me completely.

    What I have read so far is that TMR0 is not the best choice since it shares some ressources.

    Could someone please explain to me how those timers work.
    What registers are do be dealt with?

    what would be the highest frequency I could count and what would be the resolution?

    I have not used timers before but it looks like this is something i've got to learn now.

    Any help is appreciated.

  4. #4
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    mister_e,

    srspinho's code example is even more confusing since it uses interrupts.

    something more I've got to learn.

    Could someone provide a code snippet just for the Timer and Interrupt part with some comments of what it does?

    At the moment I'm completely puzzled.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    i just post one that use Timer1 interrupt on another thread

    Here

    Maybe not too handy in your case but to start... i'll do one applied to your need. Later

    BTW i read your specification and i know where to begin to do some example.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    Many thanks in advance Steve !

  7. #7
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default 16F628 and frequency counter

    Dear Pictron,

    I have written a pbp program for the frequency counter by using 16F628. I have used TMR1 which is working as 16 bit.
    The first trick is the TMR1 which is working on the back I mean it is not related with your program. The second trick is to use fixed periods such as 1000 ms - 100 ms - 10 ms - 1ms for counting and making sampling during these periods. The only problem with PBP is the timing. I have used pause command but I have seend that it is not enough precision to use that command and I decided to use a timing section written in ASM language.
    I made a very good frequency counter which is working also as auto range. There are 4 different measuring mode on counting the frequencies between 0 and 40 MHz.
    - 0- 65 KHz, Mode 0 , 1000 ms sampling period
    - 65KHz-650 KHz , Mode 1, sampling period 100 ms.
    - 650KHz - 6,5 MHz Mode 2, sampling period 10 ms.
    - 6,5 MHz - 40 MHz, mode 3, sampling periode 1 ms.
    I used also one divider (U664) for counting frequencies between 40 MHz up to 1200 MHz and one switch was selecting the inputs as LF and HF.

    If you have enough knowledge you can write your own program with these instructions.
    ERO

  8. #8
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    ERO,

    what you have already built is exactly what I'm trying build.

    would you mind to post your code?

  9. #9
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default

    Pictron,

    I am sorry I can not give you all the code. But I can help you to write your code. I can give you only the tricks. Because there is a little bit commercial side in this job.

    Anyhow, let start to work now. You have to learn firstly how to use the TMR1 in 16F628 chip.

    look these codes;

    bcf STATUS,RP0 'change bank
    bcf STATUS,RP1
    clrf TMR1L ' clear TMR1 low
    clrf TMR1H ' CLEAR tmr1 High
    movlw 7
    movwf T1CON 'give 7 to T1CON Register

    same code in PBP;

    TMR1=0
    T1CON=7
    when you give number 7 to T1CON register the TMR1 start to count the frequency connected to PortB.6 pin which is the fixed input pin for external signal sources. Meanwhile you have to wait 1000 ms for first sampling mode. In a simple way
    you can try the following

    GetFrq:
    TMR1=0
    T1CON=7
    Pause 985 'we do not wait 1000 ms because there are also some delay on the program steps)
    Freq=TMR1
    Count=freq/1000:Total=(freq//1000)
    Lcdout $FE,$80,"0-65 KHz : MOD=0"
    Lcdout $FE,$C0,"Fre:",#count,".",dec3 Total," Hz "

    'now you have the frequency in your hand.
    depending of the frequency which you measure you have to change the sampling period as 100 ms, 10 ms, 1 ms.
    As I told you before it was not so easy to control the right delay time as 1000 ms - 100 ms - 10 ms - 1 ms. For this reason these command should be given in asm language. It is the sample for the delay of 1 ms;

    movlw 0xf2
    movwf _SureL
    movlw 0x02
    movwf _SureH

    Delay_3
    decfsz _SureL, f
    goto $+2
    decfsz _SureH, f
    goto Delay_3
    goto $+1

    I hope you have undertood somethings.

    ERO

  10. #10
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    ERO,

    many thanks for your help.

    The BASIC part is "readable" for me.

    To be honest, I do not understand the ASM part.

    I will start experimenting with the BASIC stuff and let you know how it works.

    What I understand so far is:
    It should be possible without having to use ASM,
    but the challenge is to finetune the execution time of the program loops to get an acceptable accuracy.

  11. #11
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default

    Pictron,

    Ofcourse you can do the counter without using asm but consider the followings;
    - it is not so easy to arrange 1000 ms delay by using pbp codes. Ofcourse if you write PAUSE 1000 the program will wait 1000 ms (with 4 MHz cyristal). The program commands creates also some delays and you have to take out that delays from your main 1000 ms delay. I mean, if you want to make a precision counting you open the TMR1 first and immediatly after you have to wait exactly 1000 ms without doing anything or doing something but knowing the period exactly.
    But you can do some trial counting. Let say you found one frequency source ( I was using TCXO - temperature compensated xstal oscillator) and make some measurements. Compare the result with the frequency of source and if not close change the your PAUSE xxx ms figure until you found closest value.
    This is my suggestion.

    On the other hand, the most difficult part is the pre-amplificator of the frequency counter. The good ones having the following specifications;
    - input signal level 5 - 10 mv. for the signals up to 1 MHz.
    - input signal level 30 mv. for the signals between 1 Mhz 6 Mhz.
    - signal level should be arround 50 mv after 6 MHz.
    The pic can measure only if the signal level is TTL it means about 0-1,6 V for LOW level, and 1,8V -5 V for HIGH level.

    I made also my own preampli. If you want I can give you the schema of the circuit later.
    If you want to make a good frequency counter I give you my program (as Hex file) and the circuit PCB everything. JUst let me know your e-mail.

    I upload all necessary files to the following link and will be available there for 5 days. Pls. be quick to load them.

    http://s11.yousendit.com/d.aspx?id=2...M1XGZOED2LGO45


    ERO

    Last edited by ero; - 26th January 2005 at 13:41.

  12. #12
    Barry Johnson's Avatar
    Barry Johnson Guest


    Did you find this post helpful? Yes | No

    Default

    The COUNT command works very well for this function if used correctly. I have used it to measure frequencies up to 54 MHz using a 16F818 with a 16 MHz osc.

    The trick is to use a divide by N chip external to the PIC. This gets the input freq into an acceptable range. Remember that the highest count rate the PIC can make is about 100 KHz with the 16 MHz osc. The freq resolution is then simply determined by how long you measure (and the stability of the system).

    COUNT has sample rate limitations as mention. As others have statted, you can go direct faster if you write a dedicated program ... beware of aliasing.

  13. #13
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Lightbulb

    hi, Pictron

    I remember i have a 50 Mhz LCD freq counter in my archives, using a single 16f84, a 74 hc 132 and a bf 246 or J310 ...

    but, program is not picbasic written ... but assembler !!!

    want pcb, scheme and program ???

    Alain

  14. #14
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Talking

    Hehe,

    You guys are very generous... seems i don't need to work on now
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  15. #15
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Talking

    Originally posted by ero
    Pictron,

    I am sorry I can not give you all the code. But I can help you to write your code. I can give you only the tricks. Because there is a little bit commercial side in this job.
    does it ? LOL!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  16. #16
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking

    Hi, steve

    seems better to deal with "HF" ( lol ! ) using assembler than PicBasic ...

    don't you think ???

    allez, j'te fais une baise de France ...pour la nouvelle année.

    Alain

    PS: question: How many professionals here, and how many hobbyists ... quite surprising answer !!!

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Hi Alain,

    There's so many way to do one thing! i'll not play devil's advocate here. If it's working... it's working. That's it!

    allez, j'te fais une baise de France ...pour la nouvelle année
    Maudit francais LOL! yeah happy new year to you also. All the best for you guy!

    So far, use this http://rocky.digikey.com/WebLib/Micr...Data/TC820.pdf can also work.
    Last edited by mister_e; - 26th January 2005 at 15:19.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  18. #18
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking

    Hi, Steve

    Here is the beast !!!

    nominal freq is 41.020 ...

    yeah !!!
    Attached Images Attached Images  

  19. #19
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    I haven't got much useful input yet,

    I thought this is a picbasic forum, but all examples I have got so far involve assembler.

    But at least I might have made some of you smile or even laugh.

  20. #20
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Angry

    Hi, Pictron

    So, THE correct answer you want is located in 3.04 manual ...see PULSIN, RCTIME and COUNT functions.
    But 20 Mhz +/- 1Hz is a bit funny, then ...

    Remember PicBasic uses "only" 16 bits ...

    read you next time.
    Alain

  21. #21
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default

    Hi Pictron,

    I give you one program which is working very well written in full pbp. I hope you understand how is working. In case if you have any problem pls. ask me.

    [email protected]

    ero



    ps. I made a small changing on the program pls. re-load it!!!!!.

    Attached Files Attached Files
    Last edited by ero; - 27th January 2005 at 12:57.

  22. #22
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Question

    I'm i wrong but by using this code that imply you must use an 14MHZ crystal?

    Perhaps it has to! Your sampling time in mode 0 is set to 1.4 Sec with a 10MHZ crystal setting. So if we want to count for 1 second we have a 1.4 ratio. I assume it's for accuracy?
    Last edited by mister_e; - 27th January 2005 at 14:21.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  23. #23
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    or it's for getting 1/2 HZ... another way!!!!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  24. #24
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default

    @steve,

    Lets make a simple calculation;
    - The osc frequency is 10.000.000 Hz.
    - The pic working frequency is 10.000.000/4 = 2.500.000 Hz.
    - The period of one command cycle is : 1.000.000/2.500.000=0,4 us. means 400 ns.
    - max. frequency which will be measured 50.000.000 Hz.
    - the quantity of puls in one second is 50.000.000
    - in one ms = 50.000
    - in one us = 50 (needs 125 command cycle for counting it).
    As you have seen everything are in the range and there is no problem. According to these calculation the max. frequency which will be counted is 65.535 .000 Hz.
    I checked a lot of time to count this frequency also. If you write a simple program as very few lines it is counting that frequency.
    My program can count up to 50 Mhz.

    Pls. do not consider the delays given on the program. These are the delays for pbp. I used asm section for the delays my real program which is more precision.
    Normal periods should be as follows;
    Mode 0: 1000 ms.
    Mode 1: 100 ms.
    Mode 2: 10 ms.
    Mode 3: 1 ms.
    If you want to count 50.000.000 Hz in one ms. you obtain 50.000 and you have only 16 bit TMR1 variable which can be max. 65.535 which is your max. counting figure in one ms. By the way the max. frequency will be 65.535.000 Hz.
    My real program is interesting. It is having auto range function. Before writing this program I made some frequencymeters with pic. All of them was using a button to change the resolution and also the sampling time (mode). I decided to write my program it is working automatic on selceting the mode. There is no any button on my frq_meter. If you interest I can give you also the codes.

    Regards
    Ero


    Ero
    Last edited by ero; - 27th January 2005 at 14:51.

  25. #25
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Thumbs up

    Great

    I build many Frequency meter stuff as now too using PBP.

    With your code, many user will be able to implement the "Auto-Range" feature by removing some line and testing TIMER1 overflow.

    Last edited by mister_e; - 27th January 2005 at 15:01.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  26. #26
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Exclamation

    Yeah, good ...but...

    as your result is +/- 1 count on 65535 ... ( Timer is 16 bits ...no more )

    precision is not really what you expected at first thread !!! we are very far from 20 Mhz +/- 1 hz.

    sorry for the cold shower ...

    Alain

  27. #27
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    ERO,

    many thanks for the PBP code.

    This is something I can read ;-)

    But help me to understand somethig:

    Using this concept (without "autorange") would I need to use a timer at all?

    a simple COUNT would do the same job, wouldn't it ?

    or am I missing something here?

    B.T.W.

    Since I have never used timers before, I would be interrested in your "autorange" Code.

  28. #28
    Join Date
    Aug 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Default

    Dear PICtron,

    I have not yet used the Count command and I do not know how to use it. Could you pls explain how ?.

    Auto range is only one idea. As a simple explanation you take about 1 ms. sampling period and check the frequency. You select the mode according to the result . It is very easy. It is simple idea but working very well.

    Ero

  29. #29
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Here's the basic of TIMER1

    Timer1 is act as a back ground counter on RB6 pin on the PIC16F628. It store the result in TMR1L and TMR1H register.

    So you need to:
    • 1. set TMR1L and TMR1H to 0, to make the counter begin count at 0.
      2. start the counter By using T1CON=7

    The above set timer to:
    • 1. No sychro from the internal to the internal clock [bit 2]
      2. Use external RB6 as clock source [bit 1]
      3. Enable timer1 [bit 0]

    then after you just have to wait for the sampling time you want to use. Once it's finish. stop the timer with T1CON=0

    Now your result is store into internal register TMR1L and TMR1H in two Byte format. Place them into one WORD variable and display your count

    MyCount var word

    MyCount.lowbyte=TMR1L
    MyCount.HighByte=TMR1H

    lcdout $FE,1,"MyCount",#Mycount

    you can test if the TIMER1 has overflow by testing PIR1.0 If 0 then there's no overflow. If 1... TIMER1 has overflow.

    If it has overflow, you must clear it by software PIR1.0=0
    Last edited by mister_e; - 27th January 2005 at 18:47.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  30. #30
    PICtron's Avatar
    PICtron Guest


    Did you find this post helpful? Yes | No

    Default

    Steve,

    if I understand this correctly
    the advantage of using TIMER1 is:
    I don't need to change the sampling time for different ranges.

    I would always sample 1000ms and check the overflow bit in a loop.
    for every overflow detected a separate counter is incremented and the overflow bit is reset.

    with this technique I should be able to get a resolution of 1Hz even at 50MHZ.

    Well, it could be a challenge to deal with numbers that big
    having only WORD variables.

    is this correct?

  31. #31
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    you can do something like that too. BUT you must consider the time you get out of counting and do the math with your variable + return to the counting stuff and such. And more if you're using PAUSE 1000, it will never interrupt before the PAUSE statement has finish. That come bigger and bigger then?!?!

    To do that, in software, you'll need at least 1 timer and 1 counter. Let's say we gonna use TIMER1 as 1 sec time-base and TIMER0 as counter. See datasheet for that.

    OR More simple, use a external 1sec time-base and do the count untill you're time-base tell you to stop.

    IMO, the easyest way is to begin with a sampling time of 1ms, checking if there's any result, if not, redo with a higher sampling time, testing the overflow and result if it's not what you expect, redo with a higher sampling time, testing the overflow and result .... utill you have 1Sec sampling time or expected result.

    so it could be as propose 1ms, 10ms, 100ms, 1Sec

    don't forget timer will gives count a max of 65536. This is why if your sampling time is 1 sec = max frequency 65.536 KHz.

    frequency = pulses / secondes

    where 65.536 KHZ is 65536 pulses / secondes.
    Last edited by mister_e; - 27th January 2005 at 20:48.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  32. #32
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Talking

    Hi, Pictron

    Seem possible That Way ...

    Have a look to Mel's post about the DIV32 dummy variable loading and you will find how to.

    Using DIV 32 will then permit to display correctly from Megahertzs to millihertzs !!!

    Héhé ...

    Alain

Similar Threads

  1. using AND as an IF statement
    By dw_pic in forum mel PIC BASIC
    Replies: 27
    Last Post: - 8th June 2006, 18:05
  2. Need help deciding = BASCOM 0r PICBASIC
    By Chris DeHut in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th January 2006, 16:38
  3. Replies: 22
    Last Post: - 12th July 2005, 17:39
  4. Pic To Pic Serial Communication?
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 5th February 2005, 00:59
  5. PicBasic Fundamentals
    By Billyc in forum General
    Replies: 9
    Last Post: - 4th May 2004, 10:04

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts