IF condition for array
krims
Posts: 8
hi,
I·am using array to store 4databits.·I·have 2 approaches· to check the data bits using·IF...Else.. condition.
Approahc 1:
t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t=%1010 THEN
LOW 10· ' ON the RED LED
HIGH 11· ' OFF the Green LED
ELSE
LOW 11· 'ON the Green LED
HIGH 10· 'OFF the Red LED
ENDIF
Approach 2:
t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t(0)=%1 THEN
· IF t(1)=%0 THEN
··· IF t(2)=%1 THEN
····· IF t(3)=%0 THEN
········· LOW 10
········· HIGH 11
····· ELSE
········· LOW 11
········· HIGH 10
···· ENDIF
·· ENDIF
·ENDIF
ENDIF
out of the·above 2 approaches, Approach 2 is working but 1 is not. I would like approach 1 to be use in my application, i was wondering if any one could help me out.
·Thanks· in advance.
·
I·am using array to store 4databits.·I·have 2 approaches· to check the data bits using·IF...Else.. condition.
Approahc 1:
t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t=%1010 THEN
LOW 10· ' ON the RED LED
HIGH 11· ' OFF the Green LED
ELSE
LOW 11· 'ON the Green LED
HIGH 10· 'OFF the Red LED
ENDIF
Approach 2:
t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t(0)=%1 THEN
· IF t(1)=%0 THEN
··· IF t(2)=%1 THEN
····· IF t(3)=%0 THEN
········· LOW 10
········· HIGH 11
····· ELSE
········· LOW 11
········· HIGH 10
···· ENDIF
·· ENDIF
·ENDIF
ENDIF
out of the·above 2 approaches, Approach 2 is working but 1 is not. I would like approach 1 to be use in my application, i was wondering if any one could help me out.
·Thanks· in advance.
·
Comments
if it is comming from a switch on the board, then please provide schematics.
might be able to help....
Loop 4
input <<= input
input += IN2
PAUSE 75
I don't think you can access an array as the smashed together four bits (pretend nib?)
will read whatever is on Pin2 for 225 milliseconds, and report it. The chances are VERY good that it will NOT match %1010*. There are two solutions for this: one is to use what is called a START bit to notify the Stamp that data is about to start arriving; the other is to use a separate CLOCK signal to inform the Stamp that there is a valid DATA bit on the PIN.
I suggest you look around here, then elsewhere on the web, for information on Serial Communications.
--Rich
* Even if the data line has a 6.67 Hz square wave on it, chances are 50-50 that you will start reading at the wrong level.
for this my code,
' {$STAMP BS2e}
' {$PBASIC 2.5}
'
'Asynchronous Communication between PROTOBOARD and BASIC STAMP
'
t VAR Bit(4) 't declared as array of 4-bits
i VAR Nib
INPUT 2 'PIN 2 of Basic stamp is configured as INPUT
OUTPUT 3 'PIN 3 of Basic stamp is configured as OUTPUT
tloop:
IF IN2=1 THEN 'check for SYNCH pulse
PAUSE 100 'time period of the SYNCH pulse (ON+OFF)
IF IN2=1 THEN ' check for START Bit
PAUSE 150 ' time period of the START bit (ON+OFF)
FOR i= 0 TO 3 'Start of the Command(data bits)
OUT3=IN2
t(i)=IN2
PAUSE 75 'time period of the each databit (75ms)
NEXT
ENDIF
ENDIF
'check for the condition whether the command is recieved or not
IF t=%1010 THEN
LOW 10
HIGH 11
ELSE
LOW 11
HIGH 10
ENDIF
GOTO tloop
at the end i had IF..Else... condition i got problem with this only. Already i mentioned my 2 approaches.
......
FOR i= 1 TO 4 'Start of the Command(data bits)
t=t<<1
t=t+IN2
PAUSE 75 'time period of the each databit (75ms)
NEXT
.......
You may run into few problems with your approach.
You are checking only for start / beginning of both synch and start pulses / conditions.
Same with your data, only one snapshot of the input pin after 75ms pause.
I would suggest investigating SERIN.
Run SERIN to detect the synch pulse (using timeout),
than run it again to detect START pulse.
Than run SERIN to detect the data. You should be able to run it as 8 bit serial data and than mask off the unwanted bits.
Your 150 ms start pulse could also be programmed as two start bits and the bit time of 75 ms should correspond to appropriate baud rate. (See SERIN description for rate calculation details).
Also would like to know how do you detect when synch pulse stops· and start pulse begins.
Cheers Vaclav
·
·