Shop OBEX P1 Docs P2 Docs Learn Events
Zone compare range check small demo program... — Parallax Forums

Zone compare range check small demo program...

OzStampOzStamp Posts: 377
edited 2008-02-26 23:42 in Propeller 1
Hi Fellow Proppers..

Customer support for a local here in OZ.
Wanted a simple but fast range compare .. if value is between 2 values..output on..
Attache a quick knock up..in asm.
See file attached.

Dare to share if you can do it better..or if you have any suggestions .. please fire away.

Cheers· Ron Mel OZ

Comments

  • Shane De CataniaShane De Catania Posts: 67
    edited 2008-02-25 04:10
    Nice work Ron... good to see some Aussie representation here. deSilva will be checking your code as we speak wink.gif
    Cheers,
    Shane. (Perth WA)
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-25 04:50
    Do you have any idea what's o'clock here ????

    But... well done, Ron! The very first result from an unexperienced machine code programmer smile.gif But it works!
    If this is what you took from my tutorial I am really proud of you - but now comes the second level...

    I inserted comments with some hints for improvement:
    DAT
                 org 0
    go1
                 mov       dira,pins             'set pin 0..3 to output
    ' deSilva: It will do no harm to repeat this MOV as well, so saving a label
     
    loop
                  mov     store,#0               'reload store with zero
                  mov     temp,ina               'get value from ina
                  and     temp,#%11110000        'only interested in bits 7..4
    ' deSilva: Now this is between 16 and 240, with increments of 16
    ' deSilva: Very unconvenient, so one generally shifts it
    '             SHR     temp, #4
    ' deSilva: value in TEMP is now between 0 and 15    
                  cmp     lo,temp    wc          'compare write C only if lo is < set C
    ' deSilva: It is good practice to comment such check as 'formula'
    ' deSilva: i.e.: "lo < temp sets C" ; you did this nearly that way!
      IF_C        add     store,#1               'add 1 to store if C set from above
                  cmp     temp,hi    wc          'do another compare on hi value
    ' deSilva: i.e.: "temp < hi sets C" 
      IF_C        add     store,#1               'check for C flag again add 1 again to store
    ' deSilva: Accumulating "knowledge" is good idea...
    ' deSilva: .. however "advanced programmers" try to use the FLAGs only
    ' deSilva: See alternative in follow-up posting.
    
                  cmp     equal,store wz         'now compare again to see if both satisfied (in range)
    ' deSilva: using a variable "equal" obfuscates the idea behind "2"
    '             CMP     store, #2  WZ
    ' deSilva will look much better
      IF_Z        or      outa,#1                'turn output P0 on  if value is 2
    ' deSilva: OR is very defensive, but not always needed..
    ' deSilva: See, you have guarded it by DIRA, and you are in your own COG
    
      IF_Z        jmp     #loop                  'go again if Z was set (values equal)
                  and     outa,#%11110000        'or else turn outputs off ( 4 leds on P3..P0)
    ' deSilva: Generally you "Invert" this logic; above two instructions can be
    ' deSilva: simplified to:
    '             IF_NZ ANDN OUTA, PINS
    ' deSilva: See what I've done here? Main thing is I re-used PINS for consistency!
    ' deSilva: ANDN is VERY handy - it's the opposite of OR!
    
                  jmp     #loop                  'jmp loop   go again
                 
    
    pins         long  %10000000_00000000_00000000_00001111  '1>output I have 4leds on P3..P0
    hi           long  143             'some arbitrary setpoint  high level
    lo           long  15              'some arb level  setpoint low..
    ' deSilva: change the above to... OOPS! Those values were no good idea at all!!!
    
    equal        long  2               'equal means value 2 in store
    temp         res   1               'temp register ..
    store        res   1
    

    Post Edited (deSilva) : 2/25/2008 4:57:51 AM GMT
  • OzStampOzStamp Posts: 377
    edited 2008-02-25 05:02
    Hi DeSliva.
    Thanks for the pointers..

    I can get most things happening in asm but it takes some effort.
    Spent the last 30 years of my life programming PLC's so the logic part of it is easy
    just getting used to all the instructions is the biggest issue with me..
    Thats why I try and disect snippets /blocks of code to see what happens..

    I will type your code in and disect .. do you ever sleep ?.. it must be early morning now..
    It is school/uni holiday time at the moment ??

    The value by the way can be and will be totally variable so the shift method you indicated
    may well work for certain value's but probably won't satisfy it totally...
    The values are most likey in the 1000-4000 range (high speed A/D chip read/compare)

    Anyway as alway's thnks for the pointers and take care.

    Ron Mel Oz
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-25 05:08
    This is deSilva's version - unchecked, so probably a typo in it somewhere smile.gif
    Edit: And yes! It was!! And a very common mistake!! See NO/YES below!
    CON
       firstInputPin = 4
       firstOutputPin = 0
    DAT
                 org 0
    go1
                  mov     dira, oPins            'set pin 0..3 to output
                  mov     temp, ina              'get value from ina
                  SHR     temp, #firstInputPin
                  and     temp, #%1111           'only interested in bits 7..4
    ' C will now be used to indicate a "good" situation 
                  cmp     lo,   temp  wc         'lo < temp sets C
      IF_C        cmp     temp, hi    wc         'temp < hi confirms C
    
      'NO!  IF_C        or      outa, #firstOutputPin  'turn output P0 on  if in range
     IF_C        or      outa, firstOutputPinMask  ' YES!
     IF_NC       ANDN    OUTA, oPins
                  jmp     #go1                   'jmp loop   go again
                 
    firstOutputPinMask long  |< firstOutputPin|
    oPins        long  %1111 << firstOutputPin               '1>output I have 4leds on P3..P0
    hi           long  6               'some arbitrary setpoint  high level
    lo           long  2               'some arb level  setpoint low..
    
    temp         res   1               'temp register ..                                              '
                 Fit   496
    

    Post Edited (deSilva) : 2/25/2008 2:55:38 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-25 05:11
    OzStamp said...
    The value by the way can be and will be totally variable so the shift method you indicated
    may well work for certain value's but probably won't satisfy it totally...
    The values are most likey in the 1000-4000 range (high speed A/D chip read/compare)

    Then your program had been faulty!
    You should understand that my shifting did not change anything within your logic! It has just made it cleaner!
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-25 10:46
    OzStamp said...
    just getting used to all the instructions is the biggest issue with me..[noparse][[/noparse]quote]
    The propeller doesn't actually have all that many, only 28 or 30. My first micro was an AVR ATMega8 and that had around a hundred (although I did most stuff with that in C). So the prop was easy. smile.gif Having said that, some of the instructions on the propeller are a lot different and take a bit more getting used to.

    deSilva, could you replace this bit
    IF_C        or      outa, #firstOutputPin  'turn output P0 on  if in range
      IF_NC       ANDN    OUTA, oPins
    


    with a muxc?
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-25 10:48
    Shane, so that makes four of us aussies on the forums smile.gif

    Maybe we'll have to start our own forum smile.gif
  • Shane De CataniaShane De Catania Posts: 67
    edited 2008-02-25 14:07
    No worries Steve! There must be more than four of us shurely?? I like the idea of a local forum - or maybe a thread on this forum in order to swap Aus specific parts info etc... Though I must say that the international nature and general friendliness of this forum is something special and definitely worth encouraging and contributing too. I use Keil C and Silicon labs micros in my day to day work, and I get very depressed when I read some of the dodgy responses to genuine questions on the Keil forums... many genuinely helpful people there too of course, but a few others far too self important to be civil!

    Sorry (to the rest of the world)... off topic and only (possibly) of interest to four of us here!

    Cheers,
    Shane.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-25 14:43
    stevenmess2004 said...
    deSilva, could you replace this bit
    IF_C        or      outa, #firstOutputPin  'turn output P0 on  if in range
      IF_NC       ANDN    OUTA, oPins
    


    with a muxc?
    Yes, I can! But you can, too smile.gif
    This is very cool idea. MUX is generally not a necessary instruction,
    as you can always replace it by something as the above two instructions, but it will
    shorten the program!!!


    Edit: deSilva is still blushing...
    The OR must be with #|<firstOutputPin or course

    But everything will be mended by the MUX!!

    As an exercise, here is a use of MUX to simulate a SaveFlags and a RestoreFlags operation,
    which do not exist on the Prop
    ' Save Flags
    MUXNZ flags,#2
    MUXC  flags,#1
    
    ' Restore Flags
    SHR flags,#1 WC, WZ, NR
    

    Post Edited (deSilva) : 2/25/2008 3:30:04 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-25 14:48
    Version 3: A co-operation of Australia and Germany!
    go1
                  mov     dira, oPins            'set pin 0..3 to output
                  mov     temp, ina              'get value from ina
                  SHR     temp, #firstInputPin
                  and     temp, #%1111           'only interested in bits 7..4
    ' C will now be used to indicate a "good" situation 
                  cmp     lo,   temp  wc         'lo < temp sets C
      IF_C        cmp     temp, hi    wc         'temp < hi confirms C
    
                  MUXC     outa, firstOutputPinMask  'turn output P0 on  if in range
                  jmp     #go1                   'go again
    

    Post Edited (deSilva) : 2/25/2008 3:33:33 PM GMT
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-26 05:51
    deSilva said...
    Yes, I can! But you can, too
    Yes, I could have but it was bed time smile.gif

    but... we wouldn't have seen the other bit of code about saving the z and c flags smile.gif
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-26 11:11
    Gudday
    Steve I make five, but currently in Pula Penang Malaysia ATM, been here two years and will for another year at this stage.
  • sevssevs Posts: 50
    edited 2008-02-26 23:42
    Another Aussie here!

    Dont ask for any help though... I'm just starting out [noparse]:)[/noparse]



    Sevs

    Perth(WA)
Sign In or Register to comment.