How can I count how long it takes for a pin to change states?
P!-Ro
Posts: 1,189
I tried getting it to count the time and display the value, but it just freezes when it hits the waitfreq command.
code:
repeat··
····· lcd.cls
······lcd.str(string("wait...", 13))
····· RCTemp := cnt
······waitpeq(%100000, 8, 0)
····· waitpeq(%000000, 8, 0)
····· RCTemp := cnt - RCTemp
···· lcd.cls
···· lcd.dec(RCTemp)
···· waitcnt(clkfreq/1000 *30 + cnt)
However, if I use this in it's place, it will tell me the value with no problem:
· repeat
·· waitcnt(clkfreq/1000 *30 + cnt)
·· lcd.cls
··· if ina[noparse][[/noparse]8]
···· lcd.str(string("1"))
··· else
···· lcd.str(string("0"))
What am I doing wrong?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
code:
repeat··
····· lcd.cls
······lcd.str(string("wait...", 13))
····· RCTemp := cnt
······waitpeq(%100000, 8, 0)
····· waitpeq(%000000, 8, 0)
····· RCTemp := cnt - RCTemp
···· lcd.cls
···· lcd.dec(RCTemp)
···· waitcnt(clkfreq/1000 *30 + cnt)
However, if I use this in it's place, it will tell me the value with no problem:
· repeat
·· waitcnt(clkfreq/1000 *30 + cnt)
·· lcd.cls
··· if ina[noparse][[/noparse]8]
···· lcd.str(string("1"))
··· else
···· lcd.str(string("0"))
What am I doing wrong?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
Comments
Hope this helps...
change your code to this:
...
waitpeq(%1_0000_0000, %1_0000_0000, 0) 'wait until Pin 8 high
waitpne(%1_0000_0000, %1_0000_0000, 0) 'wait until Pin 8 low
...
Andy
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
RCTemp := cnt
down 1 line
····· waitpeq(%1_0000_0000, %1_0000_0000, 0) 'wait until Pin 8 high
····· waitpne(%1_0000_0000, %1_0000_0000, 0) 'wait until Pin 8 low
····· RCTemp := cnt
····· waitpeq(%1_0000_0000, %1_0000_0000, 0) 'wait until Pin 8 high
····· RCTemp := cnt - RCTemp
It is meant to tell me what the time is between pulses of a spark plug, so that is why I wait for it to go high first, to improve accuracy. What I think part of the problem is, though, is when cnt restarts to zero, giving wrong results. Have any of you found a way to fix this?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
What your code is doing is just timing between the pin going low and back to high again - not the whole cycle.
waitpeq(|<8,|<8,0)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
You can try it with a counter to detect the pulses. This code uses the POSEDGE DETECTOR mode. Every time a pulse occures, the PHSA register will be incremented:
Andy
If CNT < RCTemp then skip this loop and reset RCTemp.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
It would be easier to help if we knew exactly what you're trying to accomplish. The latest code looks like you're trying to display the half-cycle time of pin 8, every 500 ms.
Need more info...
You have:
"counter" is the name of the routine you're in. Generally, it's not a good idea to call yourself unless you really know what you're doing.
It would be better to reverse the logic:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
Further, what protection do you have for the prop input pin (and the transistor for that matter)? I am concerned at what voltage the spike is. You need some form of voltage limiter here. Maybe a resistor (high value) from the input probe to the base of a transistor, and the emitter (presume npn) to ground on the pcb. Place a capacitor and a zener across the base - emitter. I suggest a 0.1uF and 5v zener (or 3v3). You may not even require the transistor at all.
One of the forum members may care to comment on this circuit as it is not my area. Beau - you there???
I actually stole this picture from parsko's post, but I have been using it to show basically how it is set up.
Besides what is on this picture, I have a 10k ohm resistor connected to 3.3v going to the pin allong with the output of the transistor.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
Post Edited (I LIKE PI) : 11/10/2008 4:43:28 AM GMT
So, presume your engine can do a max 6,000 rpm, that makes the pulses coming at max 100 rps = 10mS. Therefore, with an 80MHz xtal you should be counting a cycle maximum of 80,000,000 / 100 =800,000. If you have 1000 rpm then 800,000 / 6 = 120,000 clocks approx. This should help you work out what you expect to see.
Now for your code: As mentioned above, you are only counting half the cyce and probably the spike width is varying quite· deal (not very accurate). So try this..
Since there are obviously many problems with finding the exact time·between sparks, I would instead like to count them·over a certain amount of time.·I would like to do this by running two cogs, one of them counting the sparks, and a different one timing it then finding the number of sparks fired after the time is up. The only problem is I can't figure out how to do this over the cogs. I know to share variables you must place it in the pub line like this: :variable but other than that I really do not know what else to do. If you could clarify this and maybe give me suggestions for doing·this with the code I would appreciate it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pi Guy
PUB NameOfPubMethod : variablename
is a construction if you want a method to give back a value
As long as variables are in ONE objectfile (your mainfile or any other objectfile
the variables that were defined in the VAR-section are global even across COGs
below there is a short democode to demonstrate this
MyVar is defined in the VAR-section
then TWO other cogs where started with different PUB-methods of THE SAME *.SPIN-file
and they both can change the value of MyVar
If you want to use variables across OBJECTS (instead of cogs) you have to hand over
the RAM-adress as a pointer to the other-objects
you can do this by using the "@"-operator
VAR
long MyVar
PUB give_Other_Object_the_pointer (pointer)
call of this PUB
give_Other_Object_the_pointer (@MyVar)
access of this var
long[noparse][[/noparse]pointer] := ....
democode to show access of global variables across cogs
from this democode you should get an output to a serial-terminalsoftware like PST.EXE
that looks like this
by the way: the debugoutput included can make a lot of things clear whats going on in a program
it uses the same pins as your USB-cable to download the program to the prop. This means it needs
no additional hardware no LCD, no tv, no vga-monitor, no LED
best regards
Stefan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PG
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PG