Pulse doubling within Picaxe code, can it be done?

Changster

New Member
Hi All,

I have a circuit where i have an input of 0 to 300Hz variable frequency, 0-5v square wave going in to a 18M2 and I'm using the 'count' to count the number into a variable..

I need to increase the resolution of the count to 600Hz to help with better accuracy of the rest of the code.

I would like to increase the count number by doubling the pulse or counting the 'edges' of the square wave.

I can do it externally using a simple XOR gate with a RC to one of the inputs but I think if it can be done within the software it would make a much simpler and compact circuit.

I've tried setting up a counter within an external loop but it re triggers when the frequency is low and gives false readings.

I cant think of a suitable command within the picaxe commands that could help with this.

Any ideas??

regards
Mark
 

geoff07

Senior Member
How does counting both edges and getting twice the count value help? You could just multiply the count by 2 (what goes up must come down). Count is already counting the leading edges. Count itself has a resolution of 25kHz (or better at higher clock speeds) so you won't need to make it more accurate.

It would help if you were to clarify the problem you are trying to solve, as it isn't clear, at least to me.

If what you want is to reduce the time taken by the count command because it delays other things (I assume it is blocking, I don't use it myself), then consider an interrupt, a high clock speed, and do the counting yourself on the fly.
 

AllyCat

Senior Member
Hi Mark,

It rather depends which PICaxe you're using, how long is your count "window" and thus what accuracy you're looking for? Also whether the "square wave" has a completely constant (short-term) frequency and exactly 50% duty cycle? But often the best solution is to measure the PULSIN time(s) of a single cycle, and calculate the reciprocal to give the frequency. Then average (or accumulate) several measurements for higher accuracy if necessary.

Or, in principle, the PICaxe is just about fast enough to "count" each half-cycle within a software loop, but then you have the issue of defining when the "measurement window" finishes. An Interrupt, or Multitasking (two Starts: ) might work, but the 4 MHz clock is probably too restrictive. Another feature which might help is the on-chip S-R flip-flop, but IMHO measuring the period as above is likely the best method.

Cheers, Alan.
 
Last edited:

Changster

New Member
Hi All

Thanks for your replies.

This is my code for this project: http://www.picaxeforum.co.uk/showthread.php?26010-mechanical-speedo-motor-driver-from-an-ABS-wheel-sensor&highlight=speedo

Within the code there's a small table giving the current readout on the speedo for speed against the feedback pulses from the factory Ford speed sensor.

I would like to double the pulses from the speed sensor within the code if possible to increase the resolution of the analogue speedo.

The speedo feedback sensor is what is supplied in the car and would be very difficult to change as it goes between the speedo cable and my drive motor.

Please ignore the addspeed and subspeed as these are just test inputs to help with the setup. there is a 'rem'-ed 'count' command to count the wheel speed and calibrate the sensor to the speedo speed... if that makes sense!!??

As can be seen in the table, between setspeed 5 and 6 its difficult to be within a 30mph as it would jump from 30 to 37mph!

Code:
init:
    rem picaxe 18M2    
        setfreq m32
         
        symbol setspeed=w1 rem B2
        symbol feedback=w2 rem B4
        symbol speedout=w3 rem B6        
        
        pwmout b.3,100,0 ; set pwm duty
        
        speedout=60
        
        setspeed=12    
        
        rem setspeed=2   mph=10
        rem setspeed=3   mph=17
        rem setspeed=4   mph=22
        rem setspeed=5   mph=30
        rem setspeed=6   mph=37
        rem setspeed=7   mph=44
        rem setspeed=8   mph=50
        rem setspeed=9   mph=57
        rem setspeed=10  mph=64
        rem setspeed=11  mph=70
        rem setspeed=12  mph=77
        
        
        
    main:
    
        rem count c.1,500,setspeed
        
        
        if pinc.0=1 then gosub addspeed
                    
        if pinc.1=1 then gosub subspeed        
      
                        
        rem setspeed=15 ;Debug B2 temp speed input
        
        
        count c.2,550,feedback ;Debug B4 count feedback pulses
        
            
        if feedback>setspeed then gosub speeddown
        
        if setspeed>feedback then gosub speedup
                    
            
        pwmduty b.3,speedout ; set pwm duty Debug B6
        
        rem pause 30 ; pause 0.5 sec    
        
        
          debug
        
        
        goto main ; loop back to start
        
        
        
    speeddown:
        
        speedout=speedout-1 max 254 min 0
        
        return
        
    speedup:
        
        speedout=speedout+1 max 254 min 0
        
        return
        
        
        
        
    addspeed:
        
        setspeed=setspeed+1
        
        return
        
        
    subspeed:
        
          setspeed=setspeed-1

http://www.picaxeforum.co.uk/showthread.php?26010-mechanical-speedo-motor-driver-from-an-ABS-wheel-sensor&highlight=speedo
 
Last edited by a moderator:

RexLan

Senior Member
If the pulse is generated by the external part how would doubling or quadrupling it's output count artificially have any value at all?

If the pulse is say 60 then doubling it making it 120 has no meaning and 60 is still 60.

Maybe I am missing the big picture here but doubling the count will not accomplish anything.

I suspect that the pulse is more of a PWM signal and the duty cycle will vary a lot.

I agree with AlleyCat and that is the only solution that I see will give you much accuracy.

The only way you might get better resolution, IMO, is to take a rolling average with perhaps 6 samples. As each new count (preferably pulsin) comes in the average will update and give you a new value. This can give some very accurate results. Additionally taking a single sample and using that to "do something" will give a very poor and unstable readout.
 

AllyCat

Senior Member
Hi,

It appears that you're running the chip at 32 MHz (why?). That suggests that your "count c.2,550,feedback" is only measuring for about 70 ms. So the simple solution is to increase the sample time to 1100 units (140 ms) or longer. BTW, I don't think your remmed "pause 30 ; pause 0.5 sec" will ever give a half-second delay. :confused:

By chance (?) a PULSIN will timeout after about 70 ms (with a 32 MHz clock) which seems to correspond with a (potential) zero count at about 7 mph. So I would use something like (untested) :
Code:
getspeed:   
   speedout = 0
   PULSIN c.2,0,w4 
   PULSIN c.2,1,w5
   IF w4 = 0 then nospeed
   IF w5 = 0 then nospeed
   w4 = w4 / 100 
   w5 = w5 / 100 + w4
   speedout = CALIBRATIONCONSTANT / w5 
nospeed:
CALIBRATIONCONSTANT needs to be a large value (between about 10,000 and 65,000) to give the correct speedout. Personally, I would use a lower clock frequency, so I haven't calculated exactly what the calibration factor would be, but you might need to scale the two "100" divisors to get the correct range.

Cheers, Alan.

PS: But I do agree that "doubling" the count can help. Suppose there were 5.6 cycles in the period, then COUNT might produce 5 (or maybe 6), However, if counting edges, there would be 11, not 10 (or 12) which is a "better" result.
 

fernando_g

Senior Member
I've used a PLL frequency multiplier in the past.
But the requirements were very specific: I required 1/2 Hz resolution from a 60 Hz waveform, and my total counting window could not exceed 100 msecs.

Again, if your requirements are not that tight, the XOR solutions works very well.
 

hippy

Technical Support
Staff member
To get the accuracy you want I believe you are going to have to use PULSIN for both the pulses from the gearbox and from the speedo feedback sensor. In its most simplest of forms it is then ...

Code:
duty = 0
PwmOut SPEEDO, 100, duty
Do
  PulsIn GEARBOX,  0, gb
  PulsIn FEEDBACK, 0, fb
  Select Case fb
    Case > gb : duty = duty + 1 : PwmDuty SPEEDO, duty
    Case < gb : duty = duty - 1 : PwmDuty SPEEDO, duty
  End Select
Loop
In practice you probably want to measure both high and low pulses and combine them, multiply or divide the gearbox reading, handle PULSIN readings of zero and limit the changes to duty.
 
Top