Please comment on my first attempt
djh82uk
Posts: 193
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.
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
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.
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
>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.
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!"
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!"
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
DJH
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
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.
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.
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.
http://forums.parallax.com/showthread.php?p=647650
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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!"