Menu

DIR command obligatory?

Help
2017-11-22
2017-11-24
  • JohanDenRidder

    JohanDenRidder - 2017-11-22

    As a new user I will probably ask some very basic questions. I examined the help files however before asking.

    What is the reason why the compiler accepts 'portA.2 = 1' without hesitation,
    while 'portA.2 = not portA.2' requires 'DIR portA.2 out' to be programmed before?

    Johan

     
  • Hugh Considine

    Hugh Considine - 2017-11-23

    It's related to how the automatic pin direction setting code works.

    Any time that the code reads a pin or port, the compiler records that. Any time that the code writes to a pin or entire port, the compiler also records that. Once all input code has been compiled, the compiler looks at the list of reads and writes.

    If a pin is only ever written to, the compiler makes it an output. If a pin is only ever read, the compiler doesn't know if you wanted to read the latch or an input value, so it sets that pin to be an input. If the compiler sees a pin being read and written to, the compiler doesn't know if you are using a pin for some sort of bidirectional communication, or if you're just reading the latch. To avoid making incorrect assumptions, the compiler will expect you to set the pin direction manually.

    If you use "portA.2 = 1", you've only written to the pin, so the compiler knows it must be an output. If you use "portA.2 = not portA.2", the compiler sees that you're reading and writing to the pin, and will ask you to set the direction instead of trying to guess what you're doing.

    The compiler also records any use of the Dir command, and won't do any automatic direction setting on a pin if Dir has been used on that pin anywhere in the program.

    Hope that helps, and basic questions are always welcome if an answer isn't easy to find!

     
    • Anobium

      Anobium - 2017-11-23

      I have moved these wise words to the Help.

      See https://github.com/Anobium/Great-Cow-BASIC-Help/blob/master/source/dir.adoc

      Me. I tend to set the DIRection.

       

      Last edit: Anobium 2017-11-23
  • JohanDenRidder

    JohanDenRidder - 2017-11-23

    Ok it makes good sense, thanks.

     
  • Chris Roper

    Chris Roper - 2017-11-24

    It is always good housekeeping and good programming practice to initialise all your variables and to set the direction of all your pins.

    1. It costs you nothing and takes up no more space, GCBASIC will optimize it anyway.
    2. It makes your code easier to understand when you look at it again 3 years later and try to guess what you were thinking.
    3. It eliminates several sources of errors that could trip you up whilst debugging your code.

    Keep the questions coming though, we all learn from the replies.

    Cheers
    Chris

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.