PDA

View Full Version : Strange Waitcnt Problem



crgwbr
01-29-2007, 09:09 PM
This problem has stumped me for about a week, I finally decided to post it here to see if you can figure it out.и I'm in the middle of implementing a PINK module with the Propeller.и My program reads an encoder, does some calculations with the reading, then assigns it to a variable in the PINK.и This is the Display Loop protion of my code:

PUB Display_Loopиииииииииииииииииииииииииииииииииииии ииииии иииииии ''Display_Loop
waitcnt(80_000_000 + cnt)ииииииииииииииииииииииииииии иииииииии 'Wait 1 Second
Work_Var[5] := FM.FDiv(Enc1, 108)иииииииииииииииииииииииииии 'Divide Encoder Reading by 108
Work_Var[6] := FS.FloatToString(Work_Var[5])и ииииииииии 'Convert Answer to String
и
if Enc1 < 0иииииииииииииииииииииииииииииииииииииииииииииииии ииииииииииии 'If Encoder Reading is less than Zero
и Serial.Str(string("!NB0W01:Tool Above Zero Plane"))ииии 'Assign Pink Variable 01: Tool Above Zero Plane
и Serial.tx(CLS)ииииииииииииииииииииииииииииииииииии ииииииииииииииииии и'Finalize Variable Change
ии
elseииииииииииииииииииииииииииииииииииииииииииииии ииииииииииииииииииииииии 'If Encoder Reading is 0 or Greater
и Serial.Str(string("!NB0W01:"))ииииииииииииииииииииииииииииииииии 'Assign Pink Variable 01: Work_Var[6]
и Serial.Str(Work_Var[6])ииииииииииииииииииииииииииии
и Serial.tx(CLS)ииииииииииииииииииииииииииииииииииии иииииииииииииииииии 'Finalize Variable Change
Display_Loopииииииииииииииииииииииииииииииииииииии иииииииииииииииииииии'Loop Back to Beginning

This Code should wait 1 second, do some math, make a desision, assign a variable, then repeat.и But for some reason, it isn't waiting at waitcnt like it should be.и This is causeing the pink to be blasted with serial information so fast that it can not make sense of it.и Anyone have An Idea of why it's doing this?

Thanks,
crgwbr

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

bambino
01-29-2007, 09:54 PM
crgwbr,

I don't see a repeat command anywhere to initiate a loop!

I have never tried calling a routine from within the routine I'm calling, I don't know if there is a trick here that will work.

crgwbr
01-29-2007, 10:25 PM
I've tried it with a repeat command instead routine call; it does the same thing.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

bambino
01-29-2007, 10:35 PM
After a week I suppose you have tried moving the waitcnt around in the loop a bit so that it is called at different times!

Last night I was having a go at a terminal program in VB. I was haveing the prop send a 4096 byte array to the pc and was getting clobbered data. I keep incrementing the wait between reads and was getting 4 or 5 bytes. It was not until I gave up and decreased the wait that I got any data through. It was trial and mostly error that finally got it to working. Don't give up!!!

Luis Digital
01-29-2007, 10:43 PM
Above all I recommend to use Waitcnt(clkfreq + cnt)

inservi
01-29-2007, 10:51 PM
Hello,

I tried that (with the demo board) and it is working well. The led is blinking with 1 sec timing. The problem is probably bound to the Serial think.
I can not try more because i cannot simulate more.

------------------------------------------------------
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


PUB Display_Loop
waitcnt(80_000_000 + cnt) 'Wait 1 Second
dira[16]~~
!outa[16]

' your code as comment
...
...
...
Display_Loop

---------------------------------------
Best regards,
dro

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
in medio virtus

Post Edited (inservi) : 1/29/2007 2:56:36 PM GMT

SailerMan
01-29-2007, 11:14 PM
I noticed this line



Work_Var[5] := FM.FDiv(Enc1, 108)

Does this work properly?

I would change the 108 to 108.0 (A floating Point Value)

Regards,

Eric

inservi
01-29-2007, 11:16 PM
Hello,

After some more thinking, I advice you to use good programing style and to avoid the call of a routine by itself. This is usable when a compiler support recursivity and if you need this effect.
I not read that spin is !

use a more clean structure as this:



CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

PUB main
repeat
waitcnt(80_000_000 + cnt) 'Wait 1 Second
Display_Loop


PUB Display_Loop
Work_Var := FM.FDiv(Enc1, 108) 'Divide Encoder Reading by 108
Work_Var[6] := FS.FloatToString(Work_Var) 'Convert Answer to String

if Enc1 < 0 'If Encoder Reading is less than Zero
Serial.Str(string("!NB0W01:Tool Above Zero Plane")) 'Assign Pink Variable 01: Tool Above Zero Plane
Serial.tx(CLS) 'Finalize Variable Change

else 'If Encoder Reading is 0 or Greater
Serial.Str(string("!NB0W01:")) 'Assign Pink Variable 01: Work_Var[6]
Serial.Str(Work_Var[6])
Serial.tx(CLS) 'Finalize Variable Change





Take careful to your programing style because spin is a 'structured' language and Basic is not. Banish all that look like a 'goto'.

Best regards,
dro

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
in medio virtus

Post Edited (inservi) : 1/29/2007 3:29:56 PM GMT

crgwbr
01-30-2007, 12:09 AM
Thanks Guys, I'll try this. Hopefully I'll get by the end of today, if it doesn't I'm going to abandone the pink, and just go with a USB interface.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NerdMaster
For
Life

Stan671
01-30-2007, 05:35 AM
I cannot see why the waitcnt is not working either, unless it may be because you don't have the two clock setting CONstants in the front of the program?и Maybe because it is not indented under the PUB line?и I also feel that it is poor form and possibly a real problem having a method call itself as a way of repeating forever.и Anyway, there is always many ways to do something, here is another example.


CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

PUB Display_Loop
repeat
waitcnt(80_000_000 + cnt) 'Wait 1 Second
Work_Var := FM.FDiv(Enc1, 108.0) 'Divide Encoder Reading by 108

Serial.Str(string( "!NB0W01:" ) 'Begin to Assign Pink Variable 01:


if Enc1 < 0 'If Encoder Reading is less than Zero
Serial.Str( string( "Tool Above Zero Plane" ) ) 'then Assign to Tool Above Zero Plane
else 'else Encoder Reading is 0 or Greater
Serial.Str( FS.FloatToString(Work_Var) ) 'assign Answer converted to String


Serial.tx(CLS) 'Finalize Variable Change


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Stan Dobrowski