Shop OBEX P1 Docs P2 Docs Learn Events
Bit test or -flag test in spin — Parallax Forums

Bit test or -flag test in spin

gwkaubgwkaub Posts: 10
edited 2014-04-28 19:28 in Propeller 1
Ok this has me lost; working in spin can I check a bit or negative flag ?
I'm trying to send out long & short pulses (IR code) I call dash & dot.
Sending out high bit first 16 bits. I first need to check 1 or 0, being high bit I could check -flag.
This is the code giving me problem:

pub aux
t1 := 1600 'dash 1.6 ms
t2 := 750 'dot 750 µs
strobe(46500) 'sync pulse 9.3 ms
waitcnt(22000 + cnt) '4.4 MS
strobe(3500) '700 µs
waitcnt(2000 + cnt) '400 µs
start := $08ff
repeat 16
if start and $8000 'high bit 1
dash
else
dot
strobe(2500)
start <<= 1 'shift left for next bit
repeat 16
if code and $8000 'high bit 1
dash
else
dot
strobe(2500)
code <<= 1 'shift left for next bit

Comments

  • T ChapT Chap Posts: 4,223
    edited 2014-04-28 17:42
    Hello and welcome! You can present your code a little better if you add [ code ] to the top of the code and [ / code ] at the bottom. Remove the spaces.

    As an example, are you trying to say

    If X & Y == 1 ?
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-28 17:44
    You should use the [ code ] [ /code ] tags (witout the spaces) -- will make your code easier to read.

    You can test a bit by using a mask.
    if (testvalue & (1 << 7))
        ' bit7 of testvalue is set
    


    Make sure you're using & to test bits, not and (which is a logical test)
  • gwkaubgwkaub Posts: 10
    edited 2014-04-28 18:58
    Ok I should have said this is my object called aux . I first send $08ff "start" 16 bits, this is same all keys.
    Then I send 16 bit code for key pressed.

    pub aux
    t1 := 1600 'dash 1.6 ms
    t2 := 750 'dot 750 µs
    strobe(46500) 'sync paulse 9.3 ms
    waitcnt(22000 + cnt) '4.4 MS
    strobe(3500) '700 µs
    waitcnt(2000 + cnt) '400 µs
    start := $08ff
    repeat 16
    if start & $8000 'high bit 1
    dash
    else
    dot
    strobe(2500)
    start <<= 1 'shift left for next bit
    repeat 16
    if code & $8000 'high bit 1
    dash
    else
    dot
    strobe(2500)
    code <<= 1 'shift left for next bit
  • T ChapT Chap Posts: 4,223
    edited 2014-04-28 19:02
    Can you please format the code with the [ code ] at the beginning of your code and [ /code ] at the bottom of the code so that is appears with the correct indentation? Remove the spaces from the brackets.
  • gwkaubgwkaub Posts: 10
    edited 2014-04-28 19:15
    New to this; now I get it !
        
    pub aux
      t1 := 1600                    'dash 1.6 ms
      t2 := 750                     'dot 750 µs
      strobe(46500)                 'sync pulse 9.3 ms
      waitcnt(22000 + cnt)          '4.4 MS
      strobe(3500)                  '700 µs
      waitcnt(2000 + cnt)           '400 µs
      start := $08ff
      repeat 16
        if start and $8000          'high bit 1
         dash
        else 
         dot
        strobe(2500)                 
        start <<= 1                 'shift left for next bit
      repeat 16
        if code and $8000           'high bit 1
         dash
        else 
         dot
        strobe(2500)                 
        code <<= 1                 'shift left for next bit
    
  • T ChapT Chap Posts: 4,223
    edited 2014-04-28 19:17
    if start and $8000 must be if start & $8000

    if code and $8000 must be if code & $8000

    AND is used in a case where you want to test two or more things:

    If X > Y AND if Y > Z then
    do something.

    The manual will have the examples for AND and &.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-04-28 19:28
    There is a difference between bitwise and (&) and logical and (and) -- you are using the wrong one.

    You have redundant elements in your code; you can use this method to send any 16-bit value (start or the code).
    pub sendit(value)
    
      repeat 16
        if (value & $8000)
          dash
        else
          dot
        strobe(2500)
        value <<= 1
    


    Note the use of bitwise and (&) with the mask so the if expression will only evaluate as true when value.bit15 is one. When using logical and, any non-zero value is promoted to true; this is not what you want when testing specific bits.
Sign In or Register to comment.