CCS C pulse counting

Discussion in 'Programmer's Corner' started by raffter, Aug 10, 2008.

  1. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    hello all,

    HOW do I count pulses (few Hz)??


    -Ralph
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    One answer is to use a frequency counter. What is the hardware you have? What is the nature of the signal?
     
  3. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    I will use 16F84 chip... this has no ADC or comparator built-in.... I want to know HOW it will read low freq "pulses"... I have read about using the RB0 as input(to read freq).. is there a simpler way?? I mean I wish NOT to use this ext_int but would like other port/s for input... I hope im clear..freq is a few Hz.. then value will be assinged as an integer...
     
    Last edited: Aug 11, 2008
  4. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    Its not necessary to use an interrupt. You can use any digital input and when your signal is high on this input then you will count a pulse and so on. See the code below for example


    while (true)
    {

    if (input_state(pin_A5)
    {
    count++;
    }

    }

    I use a general while state to be always true as the program wont end unless the power is off. Then i use the if state and if it is true (high input signal on the input) then it increments 'count' by one, thus it counts pulses.

    The name of the pin i used in the if state is arbitrary, you can use whatever pin you like. Also, you have to define 'count' as an integer or long or long long according to how many pulses you need to count.

    define 'count' like this for example:

    long count=0;

    you have to put it equal zero to be sure it will start count from zero
     
  5. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    ok mike...

    thanks :)

    I will try that one out..

    another question..what STATE(logic 1 or 0) will it count?? is "pin_a5" pulled to Vdd(w/resistor)?

    EDIT:

    ooopppsss it "triggers" on HIGH... :)
     
  6. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    It will count at logic 1. If you put if (!input_state(pin_a5) then it will count at logic 0
     
  7. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    mik3,

    here is my code ...
    ============================
    void sense()
    {
    nokia_gotoxy(8,1);
    printf(nokia_printchar," TEST ");

    while(true)
    {
    long int a;

    if(input(pin_a0))
    {

    nokia_gotoxy(1,3);
    printf(nokia_printchar," dec: %ld",a);
    delay_ms(25);
    a++;

    }
    }
    }
    ========================

    I have added delay(debounce)...

    how do I make this a "simple frequency counter" ? :)

    EDIT:

    I was thinking "counting" the pulses in 100mS(then multiply by 10) so I get updated every 100mS...

    EDIT2:

    TOCK/RA4??
     
    Last edited: Aug 11, 2008
  8. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    I think a more accurate solution is to start counting the time just before to start counting the pulses. Then count ten pulses and just after you finish counting the pulses stop the time counting. Finally, find this formula by this equation:

    F=10/timecounted
     
  9. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    ok mik3,

    I will take note of that...

    btw, I found this ASM code on the net.. a simple tacho

    -----------------------------
    DEVICE 16F84
    DECLARE XTAL 4
    DECLARE LCD_TYPE 0
    DECLARE LCD_DTPIN PORTB.4
    DECLARE LCD_ENPIN PORTB.3
    DECLARE LCD_RSPIN PORTB.2
    DECLARE LCD_INTERFACE 4
    DECLARE LCD_LINES 1
    PRINT $FE,128
    W0 VAR WORD

    START:
    COUNT porta.0,1000,W0 ' COUNT FOR 1 SEC
    W0=(W0*10)/2
    CLS
    PRINT DEC W0*6," RPM "
    GOTO START
    -----------------------

    COUNT porta.0,1000,W0 <-- I cant translate this to C ..the rest of the statements, I can understand BUT the one I mentioned, Im having a hard time translating it ...
     
  10. Abdulo majeed

    New Member

    Aug 20, 2008
    1
    0
    I need to make a dimmer for stabiliser
    where the input is from 100Volt ac to 350 ac and out put 100 volt for auto transformer,
    i preffer to use 16f676 because it adc built in.
    Thank you
    Abdul Majeed
     
  11. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Caveat: I know a fair amount of x86 assembly, but not for the 16F84.
    I looked at the data sheet for the 16F84 and learned that there are 37 instructions, none of which is a COUNT instruction. Could COUNT be some sort of macro that's defined elsewhere than in the code you showed?
     
  12. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Don't know what language that snippet of code is written in but assembler it most certainly ain't!!

    Could be one of the many flavours of BASIC.
     
  13. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Now that you mention it, it does look like BASIC. The OP threw me off when he described it as ASM (assembly) code, which caused my brain to start thinking in those terms.
     
  14. raffter

    Thread Starter Active Member

    Feb 28, 2008
    113
    0
    sorry for that...

    been searching a LOT on the net ..about freq counters.. still no answer....
     
  15. zereshki

    New Member

    Feb 1, 2009
    1
    0
    ok.how we can count pulses in defined pin in a defined period of time.
    for example there is a command COUNT in PICBASIC that can do it.how it is possible in C?
     
Loading...