Simultaneous count command

Captain Haddock

Senior Member
Hi All
Complete newbie here, I would like to count input pulses from two inputs in a 60 second span simultaneously and do some maths with the results, I can do it with one no problem but of course the program waits for the minute to pass before moving to the next input count, how can I do the two at the same time?
Am currently using an 18x on the T4 training board, have to admit being a bit drunk isn't helping but is xmas day :D
Any help greatly appreciated.
 

hippy

Technical Support
Staff member
Welcome to the PICAXE forum and Merry Christmas.

You can't run two COUNT commands simultaneously but you might be able to do it by polling depending upon how wide the pulses are and what frequency they arrive at. If higher frequencies you could measure for much shorter times so there's better interleaving.

Do you need the pulse counts or are they an intermediate step to determine a frequency or something else ?
 

Captain Haddock

Senior Member
Thanks for the welcome, I've never played with these things before.
I'm wanting to compare the throughput of 2 electric meters (one solar PV, one mains grid) and work out what is going spare by using the pulses that drive the led to switch things on when free power is spare.
Measureing pulses seems the easiest way to do it.
Frequency could be as low as 4 pulses a minute.
 

eclectic

Moderator
Captain.

What's the highest frequency likely to be.

Secondly, two counters?
Use two Picaxes "talking" to each other.
They're cheap enough.

e
 

Captain Haddock

Senior Member
Was thinking about using a couple of 08m2's for the counting and another to do the work, leccy meters put out 1000 pulses per Kwh so I recon the lowest will be 3 pulses per minute and highest 42 pulses per minute, may try counting time between pulses instead of the pulses themselves to make the reaction time quicker.
 

eclectic

Moderator
Sounds straightforward enough.

Manual 2 p. 50 and p.160 for background.

Pulsin or Count

The Picaxes could run at 4MHz or 2MHz

Plenty of time to "catch" three pulses.

e
 

lbenson

Senior Member
One picaxe should be able to count the pulses by polling in a tight loop unless the pulses are extraordinarily short.

Something like this.

Code:
#picaxe 08M2
symbol meter1Pin = pinc.3
symbol meter2Pin = pinc.4

symbol lastMeter1Level = bit0  ' bit0-7 constitute b0, part of w0
symbol lastMeter2Level = bit1

symbol meter1Count = b3
symbol meter2Count = b4
symbol loopCount = w6     ' made up of b12 & b13

  loopCount = 0

main:
  do
    if meter1Pin <> lastMeter1Level then ' if there's a change
      lastMeter1Level = meter1Pin 
      if lastMeter1Level = 1 then
        inc meter1Count
      endif
    endif
    if meter2Pin <> lastMeter2Level then
      lastMeter2Level = meter2Pin 
      if lastMeter2Level = 1 then
        inc meter2Count
      endif
    endif
    inc loopCount
    pause 100
    if loopCount = 600 then ' approximately 1 minute; time & adjust
      sertxd("meter1: ", #meter1Count, "meter2: ", #meter2Count,cr,lf)
      loopCount = 0
      meter1Count = 0
      meter2Count = 0
    endif
  loop
This should work unless the pulses are less than 100ms. If they are, the pulses can be stretched with hardware, or the pause can be reduced and loopCount maximum increased (e.g., to 10 and 6000 respectively). If you can time the loop, you can adjust the loopCount maximum or the pause time so that you more closely approximate one minute for a complete measurement cycle.

You can run this in the simulator to see how it would work. Toggle pins c.3 and c.4 to increase the counts. You might want to shorten the loopCount maximum for simulation so that the output is displayed in a timely manner (simulating less than a minute).
 

VK5MW

New Member
Because the input frequency is so low it should be straight forward. I recently ran a data collection project counting the transmit time in a large radio rack. It was counting how many seconds of each minute each of the 10 transmitters were active. A complete loop of the 10 inputs took less than a micro second and then we just added a "pause 999" to make up the rest of the second. Then sent the data back to the office via LAN after each minute.

For your project you need to consider a few things. Exactly what is the duty cycle and amplitude of the input signal. In other words what voltage is the high and low. How long is the high and low. A quick look on a CRO will give you this. If the high voltage is enough to trigger an input pin to a high state and below the supply voltage for the IC. Then you're home and hosed. Just a simple loop is needed to read the 2 inputs and if they are high, increment a bite variable for each. Its hard for to say if this will work for your situation without knowing exactly what the situation is. It did work for my needs anyway.

Good luck.

Happy New Year
 

Captain Haddock

Senior Member
Thanks for that lbenson, I have it running on the 18x with the serial out command commented out and debug on b3&4 (also with a b3-b4 variable) and it seems to be doing what I'm after, just have to go through line by line now so I understand what I'm using:eek:
 

lbenson

Senior Member
If you had really short pulses, and you needed to be very accurate with your one-minute timing, you could hack a pound store watch to get the circuitry which provides a pulse every second (search the forum for examples, e.g., from hippy), remove the pause, and add another "IF" block to count toggles of the "seconds" pin (for instance, on pinc.2).

With this you could probably catch pulses of about 2-3 milliseconds when running at 4mHz, shorter at higher speeds (based on instruction rates of 2-4 instructions per second at 4mHz).
 

inglewoodpete

Senior Member
..... A complete loop of the 10 inputs took less than a micro second and then we just added a "pause 999" to make up the rest of the second. .....
I think you mean 1 millisecond. Unless you're using a prototype of the X3s (I'm imagining), there isn't a PICAXE that can do a loop in a microsecond.
 
Top