Shop OBEX P1 Docs P2 Docs Learn Events
How do I count pulses with Javelin — Parallax Forums

How do I count pulses with Javelin

ChristianGChristianG Posts: 29
edited 2004-08-04 05:53 in General Discussion
I'm new to Javelin, so my problem may be basic.
I try to count pulses from an anenometer from Davis (7911), using the javelin Stamp demo board.
The Dalls 911 produces one pulse per revolution of the cups,·and·I need to count the pulses during a periode of 1,032 sec,·for getting the result directly in meter per sec. Measuring·on one particular pin,·I can se that the voltage is pulsing correctly between 5V and·0V.
I tried to make a very simple java program to return the pulses:
for(;[noparse];)[/noparse] {
·· int count = CPU.count(10320, CPU.pin15, false);
·· System.out.println("Wind in m/s is:" + count);
}
My first problem is that it seems that the count function is not waiting 1 sec for measuring (the timeout parameter sets the amount of time the Javelin Stamp will examine the pin in 100us units (10320*100 us = 1 sec), it works much faster, the loop is printing its results many times during 1 sec!

My second problem is that I'm not sure that the CPU.count-function is the correct funtion for returning 5V to 0V·pulses. The method counts transitions sensed on a spesified pin, is this similar with·the number of pulses? Should I use another method like CPU.pulseIn and how?

In advance thank·you for your help.

Christian

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-07-27 15:07
    I haven't used the CPU.count method myself, but a quick check of the current help file shows that the timeout parameter is in 8.68 microsecond units, with the maximum measurement period being 284.4 milliseconds. You may want to change your declaration to:

    · wind = CPU.count(28724, CPU.pin15, false);
    · wind *= 4;


    You have a small resolution penatly here, but you can correct that with additional math using the initial reading.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ChristianGChristianG Posts: 29
    edited 2004-07-28 22:49
    Thank you Jon.

    In fact it seems possible to double the period, using the shift operator:
    int p = 28724;
    int count = CPU.count(p<<1, CPU.pin15, false);
    int wind = count*2; //(in m/s)
    The resolution is still not good, because this way of calculating gives values like 2m/s 4m/s 6m/s 8m/s. Any suggestions to improve it?

    It also seems that I need to connect the i/o pin from the windsensor to a resistor (pullup resistor 2k), to get the counter work properly. Without resistor the counter returns huge values (I do not really understand why...).

    regards,

    Christian
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-07-29 04:14
    It seems like you might be able to measure the width of an input pulse and calculate windspeed directly from it. Is this not possible, or does the device give a constant width pulse where only the timing between pulses changes?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • ChristianGChristianG Posts: 29
    edited 2004-07-30 23:27
    Jon,

    The device is working as a switch, it switches off the voltage for 2/3 of the time the cups make one turn, so the periode the pin has 0V should be proportional with the wind speed.
    I’ve tried to measure the falling pulses with CPU.pulseIn. Unfortunately the return values make no sense: from 1-2 units to 32000 (in 8,68us)...

    I have some alternatives to try:

    1) Make my own counter method by converting the analog io-signal to digital, and then make loop that measure the voltage frequently, and detect pulses. But will the code work fast enough? I will give a try…
    2) Build an analog Tachometer as described at: http://www.emesystems.com/OL2wind.htm#Annemometer
    3) Use the Basic Stamp instead of javelin, witch seems to have this COUNT method that count pulses during a period of several seconds (se the example code at http://www.emesystems.com/OL2wind.htm#Annemometer):
    count wpin,2308,wind

    I thought that Javelin had the same methods as the Basic stamp. Are there many differences?

    Christian
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-07-30 23:52
    You could try to count the pulses yourself

    for example

    byte·newstate,change;

    int count = 0;

    ScaledTimer16 t = new ScaledTimer16(1000,10000); //·timeout in 100 secs

    //1000 times 10000*10usec

    t.mark();

    byte oldstate = CPU.readPort(CPU,PORTA);

    while (!t.timeout()) {

    · newstate = CPU.readPort(CPU.PORTA);

    · change = (oldstate ^ newstate) & oldstate & 0x01; //count on pin0, high->low transitions

    · if (change != 0) count++;

    · oldstate = newstate;

    }

    I don't know how fast this is.

    You should select the appropiate time unit used in ScaledTimer16 for your required timeperiod.



    regards peter
  • ChristianGChristianG Posts: 29
    edited 2004-08-03 21:20
    Peter!
    ·
    Thank you for you example, It’s exactly what I needed! And it's fast enough: In a loop the readPort function will be called 1460 times·per sec. I did not find the ScaledTimer16 you suggested, but made some similar code. It works fine!
    ·
    Christian
    ··
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-08-04 05:53
    Hi,
    You find the ScaledTimer16 class here:

    http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/core/


    regards peter


    Post Edited (Peter Verkaik) : 9/2/2009 3:12:04 PM GMT
Sign In or Register to comment.