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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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!
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
Ok it makes good sense, thanks.
It is always good housekeeping and good programming practice to initialise all your variables and to set the direction of all your pins.
Keep the questions coming though, we all learn from the replies.
Cheers
Chris