Shop OBEX P1 Docs P2 Docs Learn Events
Please comment on my first attempt — Parallax Forums

Please comment on my first attempt

djh82ukdjh82uk Posts: 193
edited 2007-06-11 22:12 in Propeller 1
HI all

I have recently switched over to the propeller, here is the first code I have written, it is more to test that it works before I expand on it, it basically looks to see if a button is pressed and then toggles between 2 pins, while one is on the other is off.

My problem is that it will not always be looking to see if the button is pressed and so may miss it, how can I change this? normally with other chips I would use an interupt.

Please comment, I know it could have probably been coded a lot better, Im just not sure how.

Heres the code.
Code said...

CON
'
High = 1
Low = 0
'
PUB Start
chkbtn1 'goto chkbtn routine
OutA[noparse][[/noparse]Pin1] := High 'set pin 1 High initiall

'
PRI chkbtn1 | Pin0, Pin1, Pin2, btn1
Pin0 := 0 'sets pin variables
Pin1 := 1
Pin2 := 2
DirA[noparse][[/noparse]Pin1] := %1 'sets pins 1 and 2 as outputs
DirA[noparse][[/noparse]Pin2] := %1
btn1 := Ina[noparse][[/noparse]0] 'check to see if button is pressed (grounded)
If btn1 := 0
chngmode1 'if so, goto chngmode1 routine

chkbtn1 'otherwise go back to the start of chkbtn1 routine


Pri chngmode1 | Pin1, Pin2

!OutA[noparse][[/noparse]Pin1] 'toggles pins
!OutA[noparse][[/noparse]Pin2]
chkbtn1 'goes back to check if the button has been pressed again

Comments

  • KaioKaio Posts: 253
    edited 2007-06-09 23:01
    Welcome in the Propeller forum. Welcome in the world without interrupts. wink.gif

    I have made some changes on your code that it should be work as described. I have included comments to give advice in learning Spin. I have the code not tested but I think it should work properly.
    CON
    '
      HIGH = 1
      LOW = 0
    
      PIN_BUTTON1 = 0
      PIN1 = 1        'use speaking word of using instead PINx 
      PIN2 = 2        'use speaking word of using instead PINx
    '
    PUB Start | btn1state
      DirA[noparse][[/noparse]PIN1] := %1 'sets pins 1 and 2 as outputs
      DirA[noparse][[/noparse]PIN2] := %1
      OutA[noparse][[/noparse]PIN1] := HIGH 'set pin 1 High initiall
      OutA[noparse][[/noparse]PIN2] := LOW
      btn1state  := HIGH 
      
      repeat    'repeat for ever
        btn1state := chkbtn1(btn1state) 'call chkbtn routine
    
    '
    PRI chkbtn1(lastState) : btn1 'return current button state
      btn1 := Ina[noparse][[/noparse]PIN_BUTTON1] 'check to see if button is pressed (grounded)
      if lastState <> LOW AND btn1 == LOW 'toggle pins only once a time 
        chngmode1 'if so, call chngmode1 routine
    
      'chkbtn1 'don't use a recursive call for this, it ends in a stack overflow 
    
    Pri chngmode1
      'toggles pins
      !OutA[noparse][[/noparse]PIN1]   
      !OutA[noparse][[/noparse]PIN2]
      'chkbtn1 'don't use a recursive call for this, it ends in a stack overflow
               'because both method would be calling alternately itself 
    
    
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-10 00:31
    Thanks for that, I don't understand whats going on here with the operators:

    PRI chkbtn1(lastState) : btn1 'return current button state
    btn1 := Ina[noparse][[/noparse]PIN_BUTTON1] 'check to see if button is pressed (grounded)
    if lastState <> LOW AND btn1 == LOW 'toggle pins only once a time
    chngmode1 'if so, call chngmode1 routine


    Also, at the end of chngmode1, will it just stop? Or will it run start as it was repeating?


    Also, how did you get away with not needing to put hte pin1, pin2 etc after the pipe in any of the methods?

    Thanks

    DJH
  • KaioKaio Posts: 253
    edited 2007-06-10 20:38
    DJH,

    >PRI chkbtn1(lastState) : btn1 'return current button state
    Here you declare a method where you pass the parameter lastState of the button. The variale btn1 after : declares the return variable. It is a normal local variable but the last assigned value is returned by this method to the caller.

    >btn1 := Ina[noparse][[/noparse]PIN_BUTTON1] 'check to see if button is pressed (grounded)
    This is a normal assignment where you get the current state of your button.

    >if lastState <> LOW AND btn1 == LOW 'toggle pins only once a time
    Here we check if the button was not pressed before (lastState) and is currently pressed (btn1). Only then the pins will be toggled by calling the method chngmode1. You must release the button before you can toggle the pins again. If you press the button again the pins will be toggled again.

    >Also, at the end of chngmode1, will it just stop? Or will it run start as it was repeating?
    Calling a method is like calling a subroutine. Spin compiles implicit a return statement at the end of each method. There is no difference if you would write a return statement at the end of the method. But you can use a return at any other location in the method to leave the method.
    In your code the method chngmode1 will be called from method chkbtn1 if the button was pressed. If the end of method chngmode1 is reached the program returns to the caller method chkbtn1 and there is also no further statement so is returns to the caller which is method Start. And there will be processed the assignment of the return value from the call and then it loops again while there is only one line after the repeat statement.

    >Also, how did you get away with not needing to put hte pin1, pin2 etc after the pipe in any of the methods?
    I have declared the pins used on DIRA, INA and OUTA as constants in the CON section. These are no variables, it is used like a placeholder in the code and the compiler will replace the constant name with its value when the compilation starts. So you can easy change the value of constants if you have another configuration of your board, like HYDRA and Demo board.
  • RinksCustomsRinksCustoms Posts: 531
    edited 2007-06-11 01:34
    in any case, like me, your best bet is to try and forget "serial" programing and just walk through the manual and tutorials, in my case going on the fifth time through, but getting the hang of object oriented prog'in.

    The forums are a BIG help! as long as you show you are trying. I thought i couldnever prog a microcontroler (starting off with a zilog) until i found the BS2, it was a god send until the Propeller came out, i'm learning just what the prop is capable of and there doesn't seem to be a limit!!

    anybody want to buy a BOE BS2, an OEM BS2, and an SX Video module? lol

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Definetly a E3 (Electronics Engineer Extrodinare!)
    "I laugh in the face of imposible,... not because i know it all, ... but because I don't know well enough!"
  • RinksCustomsRinksCustoms Posts: 531
    edited 2007-06-11 01:36
    it's a better attempt than my first application, lol

    in any case, like me, your best bet is to try and forget "serial" programing and just walk through the manual and tutorials, in my case going on the fifth time through, but getting the hang of object oriented prog'in. life is so much easier with objects!

    The forums are a BIG help! as long as you show you are trying. I thought i couldnever prog a microcontroler (starting off with a zilog) until i found the BS2, it was a god send until the Propeller came out, i'm learning just what the prop is capable of and there doesn't seem to be a limit!!

    anybody want to buy a BOE BS2, an OEM BS2, and an SX Video module? lol

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Definetly a E3 (Electronics Engineer Extrodinare!)
    "I laugh in the face of imposible,... not because i know it all, ... but because I don't know well enough!"
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-11 14:25
    thats great thanks so much for your help.

    I now understand how your code fixed mine, the auto return after a method has finished confused me, he he, I knew I had programmed it serially on one cog, so could not see how it could go back and keep checking the button, but now I can see that it keeps going up a level until it finds that "start" has a repeat command to keep it going until the button is pressed.

    I am trying to learn as much as I can, I am having trouble making the change from basic, and even then the maths side of things messed me up, I managed to make some little apps with other mcu's such as psu temp reader with lcd display and auto cycle through diff psu temps + warning alarm, nothing too complex and was very easy to write the code, but spin is just making me feel stupid at the moment, but I guess it will come in time.

    with this current code, there will be 2 chkbuttons, and two chngmodes, would it be most efficient to use two cogs? OR could I just check both buttons at the same time, then is btn 1 pressed goto method a and of button 2 pressed goto method B?

    If I do use two cogs would it be best to write the code as two objects and have one cog run each?

    I want to get that part working and then eventually use an EL1887 video overlay chip to write on the screen when a mode change is initiated and what the current mode is etc.

    Nothing rediculously complex, and should be a nice challenge for me.

    Thanks

    DJH
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-11 15:18
    Hmm, the 3.3v out from th i/o is not enough to trigger the 5v circuit, and it actually ended up with 5V going through the IO pins, just tested them and they are ok, so im thinking npn transistors and see how that goes, can npn transistors be triggered by 3.3v?

    DJH
  • Mike GreenMike Green Posts: 23,101
    edited 2007-06-11 15:37
    Yes, NPN transistors will trigger with base voltages above 0.6V. They amplify current rather than voltage and you need an appropriate series resistor to limit the amount of base current. Figure on roughly 10ma. The logic high voltage isn't really 3.3V, more like 3.0V, so 3.0V-0.6V = 2.4V, so a 220 ohm series resistor is about right.
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-11 15:43
    Thanks mike, so the base is the pin of the transistor going to the prop i/o pin correct? And so the 220ohm resistor should go between said base leg and the I/O pin?

    oh btw, I atatched an led to see if the code was working ok, and the led flashed very very quickly if I pressed the button nothing would happen, but if I held it then either the led went off or it stayed on, there was more chance that it would go off tho.

    Any Ideas?

    Could it be the setting pin 1 high and pin 2 low in the pub start? tho I would have thought it would have just left the led on if I did not press the button, unless is there anyway the input pin could be counted as low if not connected? (i.e. push button not pressed) Also I just check and is deffo a push to make switch.


    Thanks

    DJH
  • KaioKaio Posts: 253
    edited 2007-06-11 16:55
    DJH,

    the start setting of output pins is not involved by this problem.

    Did you was using a pull-up resistor on the pin with the button? Otherwise the pin is floating when the button is open.

    You can check if the code would be working properly if you make the following temporary change.
    '  btn1 := Ina[noparse][[/noparse]PIN_BUTTON1] 'check to see if button is pressed (grounded)
      btn1 := HIGH 'simulates the button is not pressed
    
    



    If then the LED is not flashing, the code is fine and you can revert the modification.

    Another problem which can occur is bouncing of the button. The code performs no debouncing.
  • djh82ukdjh82uk Posts: 193
    edited 2007-06-11 17:26
    ok, not using pull up resistor, what value would you reccomend? I guess the floating pin could be triggering the switch each time by returning a "low" value?

    I think I should maybe look into various basic circuit interfaces and start a thread perhaps, would help people like me, for instance different ways to run 5/12v aplications, pull up/down resistors, things like that, could be useful.

    That way people could post up various bits of info etc.
  • KaioKaio Posts: 253
    edited 2007-06-11 17:55
    Please have a look at this thread. There is also a recommendation for a circuitry.
    http://forums.parallax.com/showthread.php?p=647650
  • RinksCustomsRinksCustoms Posts: 531
    edited 2007-06-11 22:12
    www.discovercircuits.com or .org is an awesome source for circuits

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Definetly a E3 (Electronics Engineer Extrodinare!)
    "I laugh in the face of imposible,... not because i know it all, ... but because I don't know well enough!"
Sign In or Register to comment.