Shop OBEX P1 Docs P2 Docs Learn Events
PASM, PPDB and Mysteriously lit LED?- solved — Parallax Forums

PASM, PPDB and Mysteriously lit LED?- solved

Tony B.Tony B. Posts: 356
edited 2011-07-19 05:56 in Propeller 1
I am teaching myself PASM and working with individual components learning to make them work. In this program I am working with a push button and one LEDon the PPDB . When I run the attached code the LED is lit dimly. This is the error. I don't understand why it is lit? When I press the push button the LED lights at full strength, just as I would expect. I release the button and it returns to the dimly lit state. I have tried different pins and LEDs with the same result. Though I have tried to discover the reason I have been unsuccessful this weekend. Insight into the cause would be most appreciated.
Tony.

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-07-17 22:11
    does your button have a pull-up or a pull-down-resistor?

    if not an electrical "free floating" input works as an antenna and will pick up noise from the air.
    A wire of 10 cm is enough to make the input toggle randomly. So be sure to have a pull-up or a pull-down-resistor.
    This resistor ties the input to plus 3.3V (pull-up) or 0V ground (pull-down) that the electrical conditions are constant even if the button is not pressed and the button-contact is opened.

    keep the questions coming
    best regards

    Stefan
  • marzec309marzec309 Posts: 146
    edited 2011-07-17 22:30
    Tony B. wrote: »
    I am teaching myself PASM and working with individual components learning to make them work. In this program I am working with a push button and one LEDon the PPDB . When I run the attached code the LED is lit dimly. This is the error. I don't understand why it is lit? When I press the push button the LED lights at full strength, just as I would expect. I release the button and it returns to the dimly lit state. I have tried different pins and LEDs with the same result. Though I have tried to discover the reason I have been unsuccessful this weekend. Insight into the cause would be most appreciated.
    Tony.


    try this code:
    CON
    
      _clkmode = xtal1 + pll16x      ' Enable External Crystal and PLL X 16
      _xinfreq = 5_000_000           ' External Crystal Frequency 5Mhz
    
    PUB Main
    
      {Launch cog to run Propeller Assembly code}
    
      cognew(@entry, 0) 'Launch new cog
    
    
    DAT
    
            org       0                             'Begin at Cog RAM address 0
                  
    entry   or        dira, LED                     'Set pin 16 to output
            andn      dira, Button                  'Make sure button (pin 5) is an input
    
    loop    waitpeq   Button, Button                'Wait untill button is pressed
            andn      outa, LED                     'Turn on LED
            waitpne   Button, Button                'Wait untill button is released
            xor       outa, LED                     'Turn off LED
            jmp       #loop                         'Loop
            
    
    
    
    LED       long    1 <<  16                      ' shift 1 left 16 places
    Button    long    1 <<  5                       ' shift 1 left 5 places
    
    
    LED       res   1                               ' reserve a long for led
    Button    res   1                               ' reserve a long for Button
    
    
    its untested but you should see a few thing you forgot to add. I also changed how you handled you variables. this assumes the led is on pin 16 and your button on pin 5. don't forgrt that in pasm you need to reserve longs for your variables.
  • Tony B.Tony B. Posts: 356
    edited 2011-07-18 05:46
    Thank you for your replies!

    Stefan - I double checked the Propeller Professional Development Board's schematics and the push buttons are active low using a pull-up resistor to 3.3v.

    Marzec309 - I will have to study your changes and try the code a little later today.

    Thanks,
    Tony
  • Tony B.Tony B. Posts: 356
    edited 2011-07-18 09:53
    After much testing, changing of code, and reading and rereading the propeller manual I figured out where my error was. It was in the waitpeq. What was happening is that the mask I was using was not correct and so the code was looping through at full speed thus turning the LED on and off as fast as the code was running. In other words, it was acting as if I was running a PWM loop. When I pressed the push button the code paused in the on state. When I released the push button it went back into of PWM loop.

    Thanks to those above who helped encourage me so I could get this figured out.

    Edit: Marzec309 your code worked fine, but I had to remove the LED and Button res section first.

    Now on to a serial LCD and Ping))) sensor.

    Tony
  • marzec309marzec309 Posts: 146
    edited 2011-07-18 22:11
    I'm glad I was of some help. PASM can get a little confusing for me at times too.

    One thing I did notice in your new code that I wanted to point out. To turn on the LED you used the "mov" instruction. That does work fine in your simple code. But if you were trying to control other pins in that same piece of code, the "mov" instruction affects every pin not just the led pin, your setting the led pin high and all others low. A better way is to use "andn" as i did in my code. Doing so you will only affect the LED pin.
  • Tony B.Tony B. Posts: 356
    edited 2011-07-19 05:56
    Marzec309 you were a great help. Working with and studying your code example is what helped me see my mistake. I really like trying to solve my own problems, helps me learn more and helps it stick, but it sure is nice having the help of this forum when you just can't see the problem.

    You're so right about the "mov" instruction. Just yesterday I was wondering about having two buttons and LEDS and realized "mov" wasn't going to work if one was on and you press a second button. I'm going to have to get comfortable with "or, and, andn and xor".

    Thanks again for all the help and encouragement!

    Tony
Sign In or Register to comment.