Shop OBEX P1 Docs P2 Docs Learn Events
comparing input signals in assembly — Parallax Forums

comparing input signals in assembly

latoftelatofte Posts: 9
edited 2011-05-06 20:10 in Propeller 1
Howdy again, I hit another small roadblock
I am trying to make my own design to learn flags/input voltage stuff.
So far I have it set to have only pins 14 and 15 as inputs and pin 16 is connected to an led
Pin 14 is connected directly to 3.3v and pin 15 is connected to a switch which is also connected to 3.3v

However even with the button pushed it thinks both pins are not equal
I am unsure if simply equal voltage is indeed all you need to do to compare/test (tried both) pins.
once I figure out the neat/clean way to put up code I will have it up
(also working on learning functionality before I start to learn to clean up my code >.> sorry)
PUB Main
cognew(@Button, 0) 



DAT


ORG 0 'Begin at Cog RAM addr 0
Button
mov dira, output 'sets only 14 and 15 to input
mov Time1, cnt 'Calculate delay time
add Time1, #$100'Set initial delay here

:waiting
        waitcnt Time1, Delay1 'Wait
        mov outa, zero
        waitcnt Time1, Delay2 'Wait
        test Ina, pushed
        If_E            jmp #:LED
        If_NE           jmp #:LED2
        {If both pins are high it should jump to faster blinking LED}
                        
:LED
{blink faster}

        or outa, Pin16
waitcnt Time1, Delay1 
        xor outa, Pin16
waitcnt Time1, Delay1 
        xor outa, Pin16
waitcnt Time1, Delay1 
        xor outa, Pin16
waitcnt Time1, Delay1 
        xor outa, Pin16
waitcnt Time1, Delay1 
        xor outa, Pin16
waitcnt Time1, Delay1 
        jmp #:waiting
:LED2
{slow blink}

        or outa, Pin16
waitcnt Time1, Delay2 
        xor outa, Pin16
waitcnt Time1, Delay2 
        xor outa, Pin16
waitcnt Time1, Delay2 
        jmp #:waiting 

output   long %11111111_11111111_00111111_11111111  
Zero     long %00000000_00000000_00000000_00000000
Pushed   long %00000000_00000000_11000000_00000000
Pin16    long %00000000_00000001_00000000_00000000
Delay1   long 100_000
Delay2   long 10_000_000
Time1 res 1 'System Counter Workspac

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-05-06 12:52
    Do you have a pull-down on P15? If not, releasing the button may not cause the pin to change state. You must pull any pin used as an input to a known state (to ground for active-high inputs, to Vcc for active-low inputs). A 10K resistor to ground of Vcc is fine (one resistor per pin).
  • kwinnkwinn Posts: 8,697
    edited 2011-05-06 12:53
    Don't forget that in assembly (PASM) when you read the pins you get all 32 bits. You have to mask off the unwanted pins with an AND instruction.
  • latoftelatofte Posts: 9
    edited 2011-05-06 13:25
    Hmm, I put a pull-Down on both 14 and 15, then made the output from the switch also go to 14 and 15 so when the switch is flipped they have the same input. But it still does not seem to like something?

    I also added Bits res 1 at the end and changed the code to
    ....
    Mov Bits, Ina
    And Bits, Pushed
    Cmp Bits, Pushed
    IF.....

    But still not accepting that the two are equal :(
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-05-06 13:28
    I think he is already using a mask called Pushed with his bitwize test instruction.
    but 01 and 10 and 11 will both produce Z=0

    1st you need a pull-down resistor as stray electricity make keep the input in a unkown state
    as it's pretty much is a antenna if not connected to 3v or ground.
    basics4.gif

    And your code can be simplified alot, think statemachine.
  • AribaAriba Posts: 2,690
    edited 2011-05-06 13:29
    test Ina, pushed
            If_E            jmp #:LED
            If_NE           jmp #:LED2
            {If both pins are high it should jump to faster blinking LED}
    
    This has several errors:
    1) ina must be the source register, otherwise you read the shadow ram
    2) you nead to specify the flags that you will have affected with wc, wz
    3) TEST does an AND of the INA and the Mask in "pushed". pushed masks both pins, so you can only detect if both pins are low or not. But Pin14 is always high so you will never get the zero flag set:
    Pin 14 is connected directly to 3.3v and pin 15 is connected to a switch which is also connected to 3.3v

    If you want compare two input pins, the best way is to read one pin into the Carry flag and the second pin into the Zero flag, then you can use the IF_C_EQ_Z and IF_C_NE_Z conditions:
    test firstPinMask, ina  wc
                 test scndPinMask, ina   wz
      if_c_ne_z  jmp #LED   'Equ
      if_c_eq_z  jmp #LED2  'notEqu
    
    Be aware the the Zero flag is set if the pin is low and vice versa,but the Carry flag is set if the pin is high.

    Andy
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2011-05-06 13:37
    The trouble is that binary 010_0000_0000_0000 is not equal to binary 100_0000_0000_0000, these are the values for the two pins as active.
  • latoftelatofte Posts: 9
    edited 2011-05-06 13:45
    Awesome thanks guys!
    I was trying to figure out how to use those flags, now hopefully I can get a few more practice problems under my belt before I come begging you guys again
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-05-06 13:50
    Do you plan to have two buttons, and fast blink if only both pins are high?
    Or fast blink if both buttons are the same 0=0 or 1=1 ?

    Not forgetting wz or wc with the opcode test, is a must.

    Inverting the switches, by using pull-up resistor and switch to ground.
    and you can get away with the single: test mask, ina wz
    as z=1 only if all pins in mask=0, if you want that both switches need to be pushed.
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-06 17:16
    Ariba wrote: »
    If you want compare two input pins, the best way is to read one pin into the Carry flag and the second pin into the Zero flag, then you can use the IF_C_EQ_Z and IF_C_NE_Z conditions.
    Not sure about best but test reflects the parity of the result which means you do one test and look at the carry flag.
  • AribaAriba Posts: 2,690
    edited 2011-05-06 19:24
    kuroneko

    Nice solution!
    I need to be more careful with adjectives like "best", if there is a possibility that you read the thread ;)


    Andy
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-06 19:35
    Ariba wrote: »
    I need to be more careful with adjectives like "best", ...
    Admittedly best could also mean easy to understand for a beginner(?) so in that case I'd go with your version which makes it obvious where each bit goes and what's done with it (conditional execution).
  • kuronekokuroneko Posts: 3,623
    edited 2011-05-06 20:10
    latofte wrote: »
    Button
    mov dira, output 'sets only 14 and 15 to input
    ...
    output   long %[COLOR="red"]11111111_1111111[/COLOR]1_00[COLOR="red"]111111_11111111[/COLOR]  
    
    I wouldn't do that. Only ever enable outputs for the pins you are going to drive (e.g. LED on pin 16). There could be h/w connected to the other pins which disagrees with the level you're driving those pins with (especially 28..31). So in your case a mov dira, Pin16 is all you need.
Sign In or Register to comment.