Shop OBEX P1 Docs P2 Docs Learn Events
High output from propeller — Parallax Forums

High output from propeller

anita1984anita1984 Posts: 23
edited 2009-10-14 19:48 in Propeller 1
Hi forum,
How i can make a pin 3·output high in Spin?
is true like this ? or their is more inteligent way .
PUB PinHigh
  dira[noparse][[/noparse]3]~~
  repeat
    !outa[noparse][[/noparse]3]
    waitcnt(cnt)


Thank you in advance,
Anita

Comments

  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-10-14 13:52
    Anita, you have made the pin an output but your infinite repeat loop is toggling the pin high and low with an incorrect waitcnt as you need to add some offset to cnt. At 80MHz clock freq an offset of 80,000,000 will wait one second.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
  • AJMAJM Posts: 171
    edited 2009-10-14 13:54
    Hi Anita

    This program will make pin 3 high, wait for ~54 seconds and then make pin 3 low. After another 54 seconds it will repeat itself.

    This is all you need to make and keep a pin high, where x is your pin:

    PUB PinHigh
      dira[noparse][[/noparse]x] := outa[noparse][[/noparse]x] := 1
      repeat
    
  • BaggersBaggers Posts: 3,019
    edited 2009-10-14 14:00
    Also, beware Anita if you use AJM's code don't cut and paste, as the forum has removed [noparse][[/noparse] 3 ] from after dira and outa
    also be aware that if you want to do something after setting PinHigh, it will not return, as it has a infinite repeat loop in there

    PUB start
    ' initialisation code here
      PinHigh
    ' 
      Repeat
    '     other stuff here indented after the repeat, for your main loop.
    
    PUB PinHigh(pin)
      dira|=1<<pin
      outa|=1<<pin
    
    PUB PinLow(pin)
      outa&=(1-(1<<pin))
    
    
    



    Hope this helps,
    Baggers.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-10-14 14:03
    Well why bother with a function then when you can simply say:
      dira[noparse][[/noparse]x]~~          ' make a pin an output
    
      outa[noparse][[/noparse]x]~~         ' make the pin high 
    
      outa[noparse][[/noparse]x]~           ' make the pin low
    
    



    Of course your demo code would have a repeat somewhere otherwise the cog will go to sleep and the pin will float.
    P.S. substitute your pin number for x

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
  • AJMAJM Posts: 171
    edited 2009-10-14 15:18
    For my own benefit,

    Would not waitcnt(cnt) stall the program for about 54 seconds assuming a clock frequency of 80MHz?

    My understanding is that cnt, at some arbitrary value, would have to come full circle before the same value showed up. There are 2^32 cnt values. The propeller, running at 80MHz, would take just under 54 seconds to run through these values.

    After approximately 54 seconds, the value of cnt is equal to the value stored in waitcnt(cnt) and the loop continues.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2009-10-14 15:22
    That's right AJM as it's waiting for a match to cnt which by the time it checks a bit after it is too late so it has to wait till the counter wraps-around. It just looked like a typo in the original so I didn't mention this long timeout as it's not really useful anyway.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
  • AJMAJM Posts: 171
    edited 2009-10-14 15:38
    Thanks Peter, I see where you are coming from.
  • mikedivmikediv Posts: 825
    edited 2009-10-14 17:23
    Guys can I ask I am brand new to programing period and basically starting with Spin .,,,,,,If anyone asked me this question I would have just done this.

    Pub Pin3High
    dira[noparse][[/noparse] 3 ]:= 1 'Set pin 3 to be an output
    outa[noparse][[/noparse] 3 ]:= 1 ' set pin 3 to logic high

    repeat ' just repeat so prop does not go to sleep


    Is this ok and why would I want to make it more fancy ????
    Thanks

    Post Edited (mikediv) : 10/14/2009 5:44:53 PM GMT
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-14 17:33
    hello mikediv,
    this forum-software eats up square-brackets as change fontsize of you don't insert spaces after each opening squarebracket

    and please use the (code) (/code) commands (with square-brackets instead of round braclets to create this style
      dira [noparse][[/noparse] 3 ] := 1 'set pin to output
      outa [noparse][[/noparse] 3 ] := 1 'set pin high 
    
    



    that's OK and that way that is easiest to understand

    best regards

    Stefan
  • mikedivmikediv Posts: 825
    edited 2009-10-14 17:49
    Thanks Stefan I did not realize that .. Stefan I am just trying to understand I do know about the ~~,,, ~ and the not operator ! and can use them in a simple programs but should I get into the habit of using one way over the other??

    Of course if I had to turn on more than 1 output high or low I would use the [noparse][[/noparse] 1..8 ] or even the ~~ but for very basic stuff is it ok to use the most basic method?
    I watch how you pros are doing it even the simple stuff and to be honest its far more complex than I would have thought to use so in progressing to learn Spin should I adapt the way you guys are programing I notice that a lot of you
    would use somehting like

    Pub Pin
    dira[noparse][[/noparse] pin ]
    then use the Pin var instead of the actual pin number is it the proper way I/We should be learning?
    .

    Thanks again
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-14 18:42
    Hello mikediv,

    Yes of course use the basic method. It is easier to read and understand. Once you have practised coding outa [noparse][[/noparse] 3 ]~~ some hundred times it will be easy for YOU to understand it
    but not for other newbees.

    The special methods are only needed if you need maximum speed or maybe squeezing some bytes of RAM out of the code if you run out of RAM
    in all other cases it's a personal decision to use outa [noparse][[/noparse] 3]~~ or something like that.

    One rule about using methods is every PUB should do ONE thing and have a SELF-EXPLAINING name.

    For setting up a few IO-pins I would say it is personal taste if you write it is own method for it.

    The sense of methods is to save codelines if you need code in multiple places with only slight changes and these changes can be handled by parameters

    Example start a serial driver
    ser.start(Rx-Pin, Tx-Pin, mode, baudrate)

    serial connections are often needed but for different Tx/Tx-pins, and baudrate. So Rx/Tx-pin baudrate are PARAMETERS

    or
    ser.str(@MyString1)

    ser.str(@MyString5) etc. etc.

    some professionals like to code by the maxime "save as much time as possible and type as less keystrokes as possible "
    I would like to add "and loose lot's of time for decoding your super-compact-encrypted code a half year later !"

    SPIN is a structured language and my opinion about REAL professionalism is: "code it as clear and easy to understand as possible".

    Let's assume you have some special kind of LCDisplay which needs a seven-byte sequence to set the cursor to line x columm y

    by direct coding you would have to code

      ser.tx(27)
      ser.tx(104)
      ser.tx(248)
      ser.tx(21)
      ser.tx(254)
      ser.tx(X)
      ser.tx(Y)  
    
    



    So with direct coding you would have to code ALL these seven lines again and again

    That is a case where coding a method would be good

    PUB SetCursorXY (X,Y)
      ser.tx(27)
      ser.tx(104)
      ser.tx(248)
      ser.tx(21)
      ser.tx(254)
      ser.tx(X)
      ser.tx(Y)  
    
    



    then at all places where you want to set the cursor you code just ONE single line with a self-explaining command

      SetCursorXY (2,10) 
    
    



    If you have additional questions about how coding - whatever - can be done elegant AND easy to understand ask it ! That's what this forum is for

    best regards

    Stefan
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-10-14 19:16
    Of course, you could always do:

      ser.str([b]string[/b](27, 104, 248, 21, 254))
      ser.tx(X)
      ser.tx(Y)
    
    
    


    -Phil
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-14 19:48
    good variant how a bytesequence can be coded compact Phil,

    but still you have to code three lines of code and they are not self-explaining. (half a year later you scratch your head asking what the heck was the bytesequence 27,104,248,21,254 ???
    was it clearing the display ? or cursor-blink-mode ? ... hm or overwrite mode ? ...

    You could add a comment but I prefer a Pub with a name that is self-commenting self-explaining

    SetCursorXY(2,10) says it all without an additional comment which might be left out because of lazyness

    best regards

    Stefan
Sign In or Register to comment.