If Then Array Oddity


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Posts
    10

    Question If Then Array Oddity

    I have a small piece of code That acts very unusual, I got from Bruce, I Believe. Anyway if you look at the label "ThisDoesnt" and compare it to the Label "ThisWorks" you may be able to spot the problem. However, I cant find any reason why this shouldnn't work. But it will not work for me.
    I'm using PBP 2.46 and a lovely 18f2620.


    DEFINE OSC 4
    DEFINE HSER_BAUD 19200
    DEFINE HSER_CLROERR 1 ' Automatically clear over-run errors
    DEFINE HSER_RCSTA 90h ' Enable USART receive
    DEFINE HSER_TXSTA 24h ' TXSTA=%00100100. TX enable, BRGH=1 for high-speed

    GP VAR BYTE ' GP variable
    BytesIn var byte[5] ' 0 thru 4 bytes
    ByteCnt var byte ' Indicates number of bytes in buffer

    OERR VAR RCSTA.1 ' Alias USART over-run bit
    CREN VAR RCSTA.4 ' Alias USART continuous receive enable bit
    RCIF VAR PIR1.5 ' Alias USART received character interrupt flag bit
    INTCON = 0 ' Disable interrupts
    ByteCnt = 0 ' Zero counter
    ADCON1 = 7 ' A/D off, all digital
    OSCCON=%01100000 ' Set internal osc to 4MHZ

    Main:
    IF oerr then
    cren=0
    cren=1
    endif
    IF RCIF THEN ' If RCIF=1 there's a new character in RCREG
    BytesIn[ByteCnt]=RCREG ' Yes. Then store it in array
    IF ByteCnt = 4 THEN ThisWorks ' IF BytesIn[ByteCnt]=EOM THEN OutMsg
    ByteCnt=ByteCnt+1 ' Increment array index pointer
    ENDIF
    GOTO Main

    ThisWorks:

    if bytesin[0]= $80 then Weird
    Weird:
    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]
    FOR GP=0 TO ByteCnt ' Clear array bytes 0 to ByteCnt
    BytesIn[GP]=0 '
    NEXT gp '
    ByteCnt=0 ' Reset index pointer back to first element
    WHILE RCIF ' Keep reading until RCIF is clear to flush buffer
    GP=RCREG ' after EOM is received
    WEND
    GOTO Main

    ThisDoesnt:

    if bytesin[0]= $80 then

    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]
    endif

    FOR GP=0 TO ByteCnt ' Clear array bytes 0 to ByteCnt
    BytesIn[GP]=0 '
    NEXT gp '
    ByteCnt=0 ' Reset index pointer back to first element
    WHILE RCIF ' Keep reading until RCIF is clear to flush buffer
    GP=RCREG ' after EOM is received
    WEND
    GOTO Main

    Also I have replaced this line (if bytesin[0]= $80 then) with a "> 7F and <81" and it works fine. But as soon as I use the "=" to i get nothing.
    Could some someone please explain this to me?

    thanks ash

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    This will work no matter what bytesin[0] is.

    ThisWorks:

    if bytesin[0]= $80 then Weird
    Weird: '<--- fall through to here regardless
    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]

    Change it to something like;

    if bytesin[0]= $80 then Weird
    Goto SomeWhereElse ' Jump over weird if bytesin[0] not = $80

    Weird:
    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Jun 2005
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    Thanks Bruce, But what i need to understand is why this statement below doesn't produce a result.
    ________________________________________
    if bytesin[0]= $80 then
    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]
    endif
    ________________________________________

    I can use (if bytesin[0]> $7f then) and (if bytesin[0]< $81 then)
    in any combination of nested (IF THEN) statements and it works as long as i dont ask it to "=". See code Below, This it what i need to work but doesn't.
    If I send the same code from my serial display to my PC I can see that bytesin[0] is infact = to $80.
    I also got this code to work using a select case statement.
    Basically what im saying is my IF THEN doesn't like "=" if using an array for comparison. But will Compare if using Greater than or less than.
    I KNOW, its not logical, thats why i really need your help.

    thanks ash

    DEFINE OSC 4
    DEFINE HSER_BAUD 19200
    DEFINE HSER_CLROERR 1 ' Automatically clear over-run errors
    DEFINE HSER_RCSTA 90h ' Enable USART receive
    DEFINE HSER_TXSTA 24h ' TXSTA=%00100100. TX enable, BRGH=1 for high-speed

    GP VAR BYTE BytesIn var byte[5]
    ByteCnt var byte
    OERR VAR RCSTA.1
    CREN VAR RCSTA.4 RCIF VAR PIR1.5 INTCON = 0
    ByteCnt = 0
    ADCON1 = 7 '
    OSCCON=%01100000

    Main:
    IF oerr then
    cren=0
    cren=1
    endif
    IF RCIF THEN
    BytesIn[ByteCnt]=RCREG
    IF ByteCnt = 4 THEN MsgOut
    ByteCnt=ByteCnt+1
    ENDIF
    GOTO Main

    MsgOut:

    if bytesin[0]= $80 then
    HSEROUT ["up",str BytesIn\ByteCnt ,13,10]
    endif

    FOR GP=0 TO ByteCnt
    BytesIn[GP]=0
    NEXT gp
    ByteCnt=0
    WHILE RCIF
    GP=RCREG
    WEND
    GOTO Main

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    My guess would be it never = $80. Standard ASCII is from $00 to $7F. Extended ASCII starts at $80.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Jun 2005
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    That was what i thought as well, However I have used 3 different Communication capture programs and am able to determine that it is infact $80 Even PBP will say its $80 as long as I use a different way to compare than the one shown. again if its > $7f and < $81 - it will work. Or i can use select case and that works fine as well
    But it doesn't work if I compare with the = sign with an array in my If Then condition.
    ash
    Last edited by atillotson; - 30th June 2005 at 20:34. Reason: Typo

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    And what happens if you do something like this?
    Code:
    Main:
        hserin 5000,Waiting,[hex serdata[0],hex serdata[1]]
        IF serdata[0] = $80 then
         hserout ["Got ",ihex serdata[0]," ",ihex serdata[1],13,10]
        ENDIF
        goto Main   
        
    Waiting:
        hserout ["Waiting",13,10]
        Goto Main
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. Manipulation of string array
    By Benny007 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd April 2008, 20:50
  4. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 05:02
  5. Reading Array values into variables
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 21st March 2005, 10:30

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts