Count maybe misunderstanding

Svejk

Senior Member
Can someone please explain _slowly_ how the count command works?

How [and why] the duty cycle afect it? And why if I'm outputing a 10,000 Hz pwm at 50% duty on one picaxe pin, the count is showing me ~5000 on another? Do I miss something?

S
 

hippy

Ex-Staff (retired)
One simplistic view is, COUNT will count the number of low-to-high transitions of a signal during the time period specified.

I presume you are counting on one PICAXE a signal put out from another PICAXE using PWMOUT. Getting half the count of the frequency suggests not counting for a full second or an operating speed mismatch of PICAXE, say the PICAXE doing PWMOUT at 4MHz, that counting at 8MHz.

Which PICAXE's are you using ?
 

Mycroft2152

Senior Member
One simplistic view is, COUNT will count the number of low-to-high transitions of a signal during the time period specified.

I presume you are counting on one PICAXE a signal put out from another PICAXE using PWMOUT. Getting half the count of the frequency suggests not counting for a full second or an operating speed mismatch of PICAXE, say the PICAXE doing PWMOUT at 4MHz, that counting at 8MHz.
Hippy,

Will you explain the reasons behind the count difference at different processor speeds.

Myc
 

hippy

Ex-Staff (retired)
SetFreq M4 : Count 0, 1000, w1
SetFreq M8 : Count 0, 1000, w2

In the first case the 1000 counts over 1000ms, in the second the time's speeded up two-fold so the time counted for is just 500ms. The following are all equivalents ...

SetFreq M2 : Count 0, 500, w0
SetFreq M4 : Count 0, 1000, w1
SetFreq M8 : Count 0, 2000, w2
 

Svejk

Senior Member
Thanks, that was my understanding too.

I'm using one Picaxe 8M with pwmout on pin2, and counting it on pin3. I have to go to work, I'll check again in the afternoon.

PS: how accurate is the count? would the accuracy be improved by using a 28x2 with a resonator?
 

hippy

Ex-Staff (retired)
Accuracy should be improved using X1's or X2's and crystals. You need them at both ends to get close to accurate results. There are still potential inaccuracies in that the count period is not perfect.

#Picaxe 08M
#Terminal 4800

PwmOut 2, 99, 200 ' 10kHz
Do
Count 3, 1000, w0 ' 1s sample
SerTxd( #w0, " ")
Loop

Gave me ...

10010 10010 10009 10009 10009 10009 10009 10010 10010 10010
10009 10009 10009 10009 10009 10010 10010 10010 10009 10009
10009 10009 10009 10009 10010 10010 10010 10009 10009 10009

Running with a 28X2 at 32MHz from crystal and PLL, 40kHz ...

#Picaxe 28X2
#Terminal 38400

SetFreq EM32
PwmOut C.1, 199, 400 ' 40kHz
Do
Count C.0, 8000, w0 ' 1s sample
SerTxd( #w0, " ")
Loop

Gave ...

40040 40040 40040 40040 40040 40040 40040 40040 40040 40039
40039 40039 40040 40040 40040 40040 40040 40040 40040 40040
40040 40040 40040 40040 40040 40040 40040 40040 40040 40040
 

hippy

Ex-Staff (retired)
More than adequate in most cases I would guess. The variance of one is because the sample period cannot be perfect and thus starting between pulses or on a pulse can give off by one results. The overall discrepancy likely arises because time is really a 16-bit countdown on an 8-bit processor with varying instruction timing on conditional jumps, thus the time counting down stretches by a few microseconds occasionally and the sample period becomes slightly too long. That's not easy to overcome because adding instructions to compensate reduces the maximum frequency that can be counted quite drastically.
 

Svejk

Senior Member
Thanks for help, all sorted now: I was runing an amplifier on a side of the breadboard and that oscilated at about the same pwmout that I tried to count. A misplaced jumper wire is the culprit for my headache last night.
 

MPep

Senior Member
Ahhhh..... and that is the reason for oscillation mentioned in the other thread. Don't ya hate it when that happens.
 

Dippy

Moderator
Well spotted.

But at least you got some useful background info and hippy didn't waste his time :)

Remember to treble-check before your next post ;)

PS. We've all been guilty.
 

Svejk

Senior Member
Learning from mistakes

Following my mishap with count I figured that I can make a capacitor meter using a simple op amp oscilator and counting the frequency :)

Attached is a photo of a 27pF measured. I'll tidy up the code and schematic, maybe extend the range and post it in finished projects.
 

Attachments

Top