BS2 Spin Object help
jbalat
Posts: 96
Hi I am new to the propeller but I have programmed the BS2
I need to use the PULSIN to read a frequency from a square pulse and write the value to memory.. This will run in a continuous loop
On a seperate cog.. I want to read this value and multiply it by a factor and do a PULSOUT in a continuous loop
I understand if the PULSIN gets a zero then it may freeze up the 1st cog. So I think I need a watchdog timer (huh?) on another cog to reset Cog 1 if it freezes..?? Is that right ?
Any tips on how to implement this code using the BS2 object ?
obex.parallax.com/objects/30/
Thanks in advance
JB
I need to use the PULSIN to read a frequency from a square pulse and write the value to memory.. This will run in a continuous loop
On a seperate cog.. I want to read this value and multiply it by a factor and do a PULSOUT in a continuous loop
I understand if the PULSIN gets a zero then it may freeze up the 1st cog. So I think I need a watchdog timer (huh?) on another cog to reset Cog 1 if it freezes..?? Is that right ?
Any tips on how to implement this code using the BS2 object ?
obex.parallax.com/objects/30/
Thanks in advance
JB
Comments
The signal ranges from 25Hz to 300Hz so should be easy to measure and output assuming I can do these operations on seperate cogs
Please post some spin code either way, I need all the help I can get !
Hint: The internal counters are really nice tools for measuring a pulse width; see the App Note (in the Propeller downloads section) for details. I've used them for this purpose many times, but all of my code for that is in PASM, not Spin.
its not that I want to use the bs2 object..
I just bought the prop to do one specific task in my car..
Im not fussed how it works whether I use a propeller or a black box, but when its working it will be permanently mounted in the car and I may or may not buy another one for playing around.
I am reading the spin manual and Im sure I will come across the commands I want sooner or later..
The problem with the PULSIN() function causing the cog to hang is that it uses the PULSIN_Clk() method which in turn
makes use of the WaitPne() and WaitPeq() functions.
These will wait FOREVER for the required state and if the state never comes then the cog seems to hang.
What you need is to replace the functions above with a REPEAT loop with a time out condition.
The function PULSIN_Clk2() below does this. It times out after 1 second. You can change this to more or less by changing the
line just above each·repeat loop which says T := ClkFreq+cnt to T:=ClkFreq*F+cnt where F is the number of seconds you need
or to T := ClkFreq/F+cnt· where F is the fraction of seconds you need.
Notice that the PULSIN_Clk2() function returns 0 if the first two wait loops time out and it returns ClkFreq if the last loop times out.
Again you can change this as you need.
So in your object for your needs just change the call to PULSIN_Clk()·in the PULSIN() function to make it call the one below.
You also may want to check for the 0 condition·if you·need to do some other action in the case of time outs.
The function below will not cause any hangs and if there is no pulse on the Pin it will return a 0 and·when·pulsing does
start on the pin it will start reporting the correct PULSE duration which then you can use·in the other actions you mentioned in
your post.
I hope this helps.
Samuel
·
[noparse][[/noparse]Edit] I've attached a demo that launches a PASM cog to measure a positive pulse on the desired pin. The result is written to a global (hub) variable to be picked up by another cog -- this prevents blocking problems. The foreground demo code launches the "background" cog, then creates a simple pulse and displays the result on the terminal; the difference between the value in the waitcnt() instruction and what is displayed is the overhead for two Spin instructions.
Post Edited (JonnyMac) : 11/27/2009 6:21:53 PM GMT
I have read the manual and still cant work out exactly how your program works. The text in propterminal is also coming out a bit messy..
-Do I input my external pulse into Pin 16 ?
-I have the proto board not the demo board
Note: That I have emulated a pulse using my stamp and I can feed it into the propeller via a 2.2K resistor..
Sam...
I have used the BS2 object with your fix with PULSIn and PULSOUT and this worked. Howevever because I was running both commands in the one procedure I dont get a proper square wave on the PULSOUT.. Any tips on how to launch this on another COg is appreciated. Do I need to lock the Global variable when writing it with the PULSIN incase the pulsout tries to read it at the same time ?
I have made some progress modifying your program to read a signal on Pin8 and output modified signal on pin0.. Only the MFactor calculation is not working below. Later I want to get MFactor by reading a Pot..
Pls Help.. If Mfactor is 2 then it works, but at 1.2 it doesnt work.. What the ???
Post Edited (jbalat) : 11/30/2009 12:53:00 PM GMT
Next step is to be able to change the value of MFactor with a Pot running on a seperate cog
Any tips appreciated.
Modified code is attached if anyone would like to help or comment
Propeller-Education-Kit Labs fundamental manual page 122ff
best regards
Stefan
This is my updated program
I had to ignore signals that are out of bounds since I must be getting a lot of noise ?
I also dumped the PASM code because I couldnt figure out how it worked.