PropC confusion
Beavis3215
Posts: 229
[
This code works with format "while input(2) == 0", Why can't I substitute button2 in the above code?
/* 3 button calculator */ #include "simpletools.h" // Include simple tools int main() // Main function { set_directions(9,4,0b111111); // Output Lights set_directions(23,21,000); // Input Buttons static char a; int button1 = input(21); int button2 = input(22); int button3 = input(23); while(1) { while(button2 == 0) // Enter first operand { if (button1 == 1) { set_outputs(9,4,1 + get_outputs(9,4)); pause(300); } } a = get_outputs(9,4); set_outputs(9,4,0); while(button3 == 0) // Enter second operand { if (button1 == 1) { set_outputs(9,4,1 + get_outputs(9,4)); pause(300); } } set_outputs(9,4,a + get_outputs(9,4)); } }
This code works with format "while input(2) == 0", Why can't I substitute button2 in the above code?
Comments
This code works, the first example does not.
http://forums.parallax.com/discussion/165150/propeller-c-manual#latest
"How do I manipulate individual bits of a variable in C?"
Searching Google for that question brings you here, and the first hit is quite thorough (aka, LONG). So, I'll summarize:
Here's a short and concise article showing the bit operations: https://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
Why do you use "1 << (x - 1)"? Shouldn't it be "1 << x" if you number pins start at zero?
Because in the first example, I'm not numbering the pins starting at zero. I should have explicitly brought attention to that, so thanks for bringing it up. @Beavis3215, most programmers will index at zero, so it would be better to do Pin 0 = bit 0. Then you don't have to do "x - 1" in code and whatnot.
This program is supposed to brighten an LED connected to pin 9 when button connected to pin 21 is pressed, and dim LED when button is pressed again. The first program works. Incrementally I added Dira statments in program 2, but adding outa statments give a strange result. Any Ideas?
What you probably want is: You have to use OR (|) to set bits, and AND (&) to clear bits. The same mistake shows up in a bunch of places in that code.
I suggest you look again carefully at the references above. They are quite helpful.
This is a bit of an odd question the way you worded it. It sounds like you might be trying to ask whether or not the Simple library is very fast at runtime? If that's the case, the answer is a reasonably emphatic no. But there's also the question of, "Does it matter?" For many cases, no. For many other cases, yes. The Simple library was built by Parallax to be easy for beginners, and it certainly achieves that. However, they gave up performance (and sometimes code size, and almost always flexibility) to achieve that.
Unfortunately you don't have quite the range of library choices in the PropGCC world as you do in the Spin world. To my knowledge, the following libraries exist: Simple, PropWare, libpropeller, libArduino (previously libpropelleruino), and OmniaCreator. Normally this is where I get on my pedestal and tout the wonders of PropWare... but it was never intended to be used by first-time C programmers. There are some aspects, particularly language syntax features, that could be difficult for a beginner to keep straight.
So, with that being said, I'd recommend you stick with the Simple library for the time being. If you are able to identify a specific spot in code that running too slow, work on optimizing only that one spot. Feel free to come back to the forums and ask about that one spot and how it might be optimized.
There is some advantage to using the simpletools functions because it provides a level of abstraction to setting and clearing bits. It might make the code easier to read as well. The simpletools function names convey the meaning of the operation that is being performed, whereas using the & and | operators on DIRA and OUTA may not be as clear. However, you could define your own macros that do the same thing as the simpletools function, and they would be just as efficient as using & and | explicitly in the code.
I ran some tests to see how long it took to switch a pin from high to low using different languages and techniques. The circuit and purpose to measure the overhead times for RC timing based on the tutorials in the Propeller Education Kit.
The results are shown here: https://forums.parallax.com/discussion/comment/1375740/#Comment_1375740
The setup and method are discussed at the top of the thread.
The results show the specific code to make the transition.
As a baseline PASM took only 6 clocks. Spin took from 592 to 643 clocks depending on method.
C using CMM memory model (compact code) and the SimpleTools functions took the longest, 689 or 707 clocks. Using propeller.h the time was cut to 145 or 163 clocks (depending on specific method).
Using LMM memory model, the timing was 129 or 147 clocks for SimpleTools, and 17 to 35 clocks for propeller.h
There is a big time difference between the SimpleTools and propeller.h libraries for the reasons Dave mentioned. For fastest execution, use propeller.h with LMM (if the code fits in memory) or even with CMM. The difference is that with propeller.h, it will often be necessary to do more bit manipulations.
It can also be seen why in other threads there have been contradictory statements comparing the speed of SPIN and Propeller C, some saying C is slower; others that it is faster
(They are both right depending on specific usage.)
Tom
From here:
So normally, to cycle an LED you might write:
And that's all fine and dandy. But you can keep 100% of your legibility and have very fast execution time if you write your own functions. You do not need to invoke DIRA and OUTA directly everywhere (like Dave Hein mentioned... except I'd rather let PropGCC optimize for me then use preprocessor macros)
In propgcc, is 544 still the number of ticks to run t = -cnt and t = cnt -544 as it is in spin?
This is a very strange snippet of code. What are you trying to accomplish with this? I can see line two, you are waiting for the input on pin "crank" to go high. Are you trying to time how long it takes for the crank pin to go high?
No C is faster. How much faster depends on the Memory Model and the Optimization, so no constant value to subtract.
Your example code will not work as is, I think you meant: t += CNT - 544;
To get the clocks to subtract just put no code between the begin and end of the time measure:
Andy