Pulse Counter


Closed Thread
Results 1 to 14 of 14

Thread: Pulse Counter

  1. #1
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92

    Post Pulse Counter

    Hi Friends,

    some time ago, Tim Box sent me an example of a Pulse Counter developped to PB Plus and was published by him on Pronton+ forum.

    I did some changes in order to compile it on PBP.
    It is working fine on a small on board computer I´ve built for my opel corsa.

    I don't know if the code can be improved or changed to improve the performance. I did some tests and the code showed good results.

    It's good to remember that the code was written by Tim and I just did some changes.

    Comments are welcome.

    my best regards for all.

    ==============================================


    Define LCD_DREG PORTA
    Define LCD_DBIT 0
    Define LCD_RSREG PORTB
    Define LCD_RSBIT 4
    Define LCD_EREG PORTB
    Define LCD_EBIT 5

    REM Variables
    TMR0_POSTCOUNT1 var WORD ' POSTSCALER COUNTERS
    COUNT_RESULT var WORD ' THE RESULT OF THE COUNT
    REFRESHED var BIT ' FLAG TO SAY COUNT REFRESHED

    '-------------------------'
    ON INTERRUPT GOTO PULSE_COUNT ' DEFINE WHERE TO JUMP TO ON AN INTERRUPT
    '-------------------------'

    GOTO INITIALIZE ' JUMP ALL INT CODE

    '----- INTERRUPT CODE -------------------
    Disable
    PULSE_COUNT:
    TMR0 = TMR0 + 7 ' RELOAD THE TIMER
    TMR0_POSTCOUNT1 = TMR0_POSTCOUNT1 - 1
    IF TMR0_POSTCOUNT1 = 0 THEN
    TMR0_POSTCOUNT1 = 500 ' 1/4 sec (may need some adjust)
    T1CON.0 = 0 ' TURN OFF THE TMR1
    COUNT_RESULT.lowbyte = TMR1L ' GET THE COUNT
    COUNT_RESULT.Highbyte = TMR1H ' GET THE COUNT
    TMR1L = 0
    TMR1H = 0
    T1CON.0 = 1
    REFRESHED = 1 ' SET FLAG TO SAY COUNT READ
    ENDIF
    INTCON.2 = 0 ' CLEAR INTTERUPT FLAG
    Resume
    enable ' RELOAD THE SYSTEM VARS
    '------ END OF INTERRUPTS CODE ----------

    '------ INTIALIzE ALL REGS --------------
    INITIALIZE:

    TMR0_POSTCOUNT1 = 500 ' INITIALZE THE POST SCALERS
    OPTION_REG.0 = 0 ' SET THE PRESCALER TO 1:1
    OPTION_REG.1 = 0
    OPTION_REG.2 = 0
    OPTION_REG.5 = 0 ' TMR0 CLOCK FROM MAIN OSC
    OPTION_REG.3 = 0 ' IN THIS INSTANCE PRESCALER IS ASSINED TO TMR0
    INTCON.2 = 0 ' CLEAR INTERRUPT FLAG
    T1CON = %00000110
    T1CON.0 = 1 ' TURN TIMER 1 ON
    TMR0 = 7 ' LOAD THE TIMER
    INTCON.5 = 1 ' ENABLE TMR0 INTERRUPTS
    REFRESHED = 0 ' DONT MAKE FIRST READ UNTIL MAIN INT HAS OCCURED
    INTCON.7 = 1 ' START THE WHOLE THING RUNNING
    COUNT_RESULT = 0
    pause 500
    lcdout $FE,1



    '------ MAIN CODE BELOW HERE -------------

    LOOP:
    IF REFRESHED = 1 THEN
    lcdout $fe,1,$fe,2, "Freq = ", dec (COUNT_RESULT * 4) ' Show results in pulses / Sec
    REFRESHED = 0 ' PREVENT A RE-READ UNTIL NEXT MAIN INTERRUPT
    ENDIF
    GOTO LOOP

    END

  2. #2
    Join Date
    Jul 2009
    Location
    Worcester South Africa
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Hi Srspinho, can I use this code for a revolution counter, revs per minute? I see your display is counts per second. Can I just multiply the COUNT_result with 240, that should give me pulses per min, yes?

  3. #3
    Join Date
    Nov 2008
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    I must be missing something,but what pin is input?
    Regards
    CharlieM
    Using PBP3
    MCSPX

  4. #4
    Join Date
    Jul 2009
    Location
    Worcester South Africa
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Yeah, good question, C_Moore

  5. #5
    Join Date
    Nov 2008
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    I don't see where any tris registers are initialized.
    Regards
    CharlieM
    Using PBP3
    MCSPX

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Hi,
    The code is "generic", ie you need to "fit" it to the device you're using.
    First time I've seen it but as far as I can see it uses TMR1 as a counter and TMR0 as the timebase. So the pulses should go to the T1CKI-pin, whichever that is on the particular PIC you're using - RB6 on the 16F628, RC0 on the 16F877 etc.

    /Henrik.

  7. #7
    Join Date
    Nov 2008
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Thanks Henrik.
    Regards
    CharlieM
    Using PBP3
    MCSPX

  8. #8
    Join Date
    Nov 2003
    Location
    Sao Paulo - Brazil
    Posts
    92


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Hi c_moore,

    sorry for my loooong delay

    Henrik is right. BTW, thank you Henrik !

    bye

  9. #9
    Join Date
    May 2012
    Posts
    20


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    how can i use this code for pic16f84a.
    can anyone send me the hex or asm code thank you

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Quote Originally Posted by daydream View Post
    how can i use this code for pic16f84a.
    can anyone send me the hex or asm code thank you
    16F84 has only 1 timer so it won't work, it should work in a 16f628 if you turn off the comparators
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11
    Join Date
    May 2012
    Posts
    20


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    hi archangel,i have pic16f628a.what are comparators?how do I turn them off?with this CMCON=7?
    and if i connect my lcd and use a 4mhz crystal and pin RB6 for input,will lcd show the freq?
    thank you so much.

  12. #12
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    I am probably confused as to what the goal is here and PBP has a command called "Count" (Chapter 5.13, page 129). Best, Ed

  13. #13
    Join Date
    Nov 2008
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    PBP as well as other compilers that have the COUNT command use a software loop to count pulses. which means that it's a blocking command and nothing else can happen while counting pulses. the code above uses hardware timers that can work in the background.
    Regards
    CharlieM
    Using PBP3
    MCSPX

  14. #14
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Pulse Counter

    Thanks Charlie, My knowledge is very limited and I guess when I read that you are specifying a lenght of time for "count" to run it was not any different than his "pause 500"? It just seems if you are pausing for half a second then you could be using this same amount of time for your time duration? Or did I miss something?

    Best, Ed

Similar Threads

  1. Pulse Capture and byte building
    By boroko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st July 2009, 02:59
  2. Need help with a pulse counter project PIC18F2620
    By blackrider in forum Schematics
    Replies: 1
    Last Post: - 29th April 2009, 01:08
  3. Replies: 3
    Last Post: - 13th September 2008, 18:40
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 18:27
  5. Pulse Counter in Assembly
    By orlandoG in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st March 2007, 02:23

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