PDA

View Full Version : BS2 Code Question -INPUT

KatyBri
12-02-2006, 12:59 PM
The following is from the V2.2.6 help file-
-----------------------------------------------------------------
Setup:
· INPUT 4

Hold:
· IF (IN4 = 0) THEN Hold······················· ' Stay here until P4 is 1

------------------------------------------------------------------
1. Will the following also work-
TEST PIN 4

Hold:
·If (TEST·= 0)·THEN Hold······················ '·Stay here until P4 is 1

------------------------------------------------------------------
Let's now change the help file example to-
Setup:
· INPUT 4

Hold:
· IF (IN4 = 1) THEN Hold······················· ' Stay here until P4 is 0
------------------------------------------------------------------
2. Will the following also work-
TEST PIN 4

Hold:
·If·TEST·THEN Hold······················ '·Stay here until P4 is 0
------------------------------------------------------------------
3. Finally, can you "scan" the states of the pins-

MAIN:
For index = 0 to 4
·INPUT IN(index)
·IF IN(index) THEN SUB(index) ' If IN(index)=1 then goto corresponding sub
NEXT
GOTO MAIN
SUB(index)
'code to do something
GOTO MAIN

Thanks
·

Chris Savage
12-02-2006, 01:14 PM
KatyBri,
·
·· You can define a PIN constant and the IDE will know whether the pin is input or output.· You don’t need to declare a pin as an input if it has never been made an output.· By default at startup all pins are inputs.· You can also use the example code below.

TEST PIN 4

DO : LOOP WHILE TEST ' Wait until P4 goes LOW

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support

Chris Savage
12-02-2006, 01:22 PM
KatyBri,
·
·· As for ‘scanning’ the I/O pins, you can read them all with one command.· If you’re trying to index the individual pins into an array that can be done by referencing the pins from IN0.· In other words, IN0(index) where index is a value from 0 through 15 referring to one of the 16 I/O pins.· Oh, almost forgot, reading all I/O pins, or 8 or 4…I hope this helps.· Take care.

ioData = INS ' This reads all 16 pins at once, ioData must be Word
OUTS = ioData ' This writes all 16 I/O registers at once
' DIRS must be output to appear on pins

ioData = INL ' Read P0 - P7 (ioData should be a BYTE)

ioData = INA ' Read P0 - P3 (ioData should be Nib)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support

TechnoRobbo
12-02-2006, 10:58 PM
KatyBri said...

For index = 0 to 4
·INPUT IN(index)
·IF IN(index) THEN SUB(index) ' If IN(index)=1 then goto corresponding sub
NEXT
Here's a suggestion. Please note inputs are continuous, if you need them as one-shots I can post a code for that.

Main:

DO
For index = 0 to 4 '5 consecutive pins scanned
··IF IN0(index) THEN
······· ON index GOSUB sub_0, sub_1, sub_2, sub_3, sub_4
· ENDIF
NEXT
LOOP
'subroutines----------
sub_0:
'..code goes here
Return

sub_1:
'..code goes here
Return

sub_2:
'..code goes here
Return

sub_3:
'..code goes here
Return

sub_4:
'..code goes here
Return

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun

TR

KatyBri
12-03-2006, 09:22 AM
Chris and TechnoRobbo (http://forums.parallax.com/member.php?u=46920)- thanks very much. Your comments were just what I needed.