C: Does This Look Right?
idbruce
Posts: 6,197
Bit masking and shifting ARRRGGG... Does this look right?
EDIT: I already found one mistake
void x_axis_driver(void *par) { int32_t x_counter; // Set DIRA as an output. DIRA |= (1 << X_DIRECTION); do { if(app_move_now == true) { app_move_now = false; // Set the output pin for Counter A. CTRA = X_STEP; // Set up the CTRMODE of Counter A for NCO/PWM single-ended. CTRA += 0b00100 << 26; // Set the value to be added to PHSA with every clock cycle. FRQA = 1; // Set DIRA as an output. DIRA |= (1 << X_STEP); // Set the OUTA register to match the desired direction of rotation. if(current_gcode.x_dir == 0) { OUTA &= (~X_DIRECTION); } else { OUTA |= X_DIRECTION; }
EDIT: I already found one mistake
Comments
Since I already had this copied, I am going to paste it before trying your code. The code shown below does not work and this is breaking it down to the minimum.
Your code got a real high pitch from my motor, which is more than I got all day LOL
By the way, I am not laughing at your help, just my situation
For one thing my defines are not correct. I hard coded the spots where the defines went and I got movement!!!!
The following code is what I ended up with. Those darn defines caused me a lot of grief. I knew better than to not enclose them in parenthesis.
@anyone
The following code will setup a counter in C for NCO/PWM single-ended and drive a stepper motor that requires a 1uS high pulse width.
With the incorrect defines... It almost makes me want to test the Teacup code one more time.
"4 << 26 | X_STEP" is a bit dangerous unless you know for sure that "<<" has higher precedence than "|", which it does. However, "4 << 26 + X_STEP" would not produce the desired result since "+" has higher precedence than "<<". It's safer to use "(4 << 26) | X_STEP".
Definitely a good point. And personally, I'd rather see "4 << 26" as a constant. Either a preprocessor macro (#define) or global constant variable or enum. Enum would be my top choice, since that's what they're made for really (it'd be cool if such an enum were provided in PropGCC)
Thanks Dave, I will change those items per your recommendations. I have also seen the following:
I currently have this:
Should these be changed also?
I currently have this:
Should these be changed also?[/QUOTE]
I wouldn't change them. The equals operator (or any of its siblings, like |=, &=, ^=) will always be evaluated last - that's a given in any language.
Yea, those defines really had me going in circles yesterday, but I knew better... My own stupidity, caused me grief. If it was not for the memory size issue, I would probably go back and try Teacup with my altered defines, but I do not see much point in it.
I just feel like I am trying to reinvent the wheel, but then again, we really need CNC and 3D printer firmware for the Propeller. I just don't believe that I am smart enough to pull it off all by myself. LOOKAHEAD will be a critical and very difficult component as well as other algorithms that will need to be figured out.
I can surely handle the simple stuff, to make a good skeleton app, but it will definitely need some smart thinkers to make it desirable firmware.
If this is your inspiration, a belief in a need, why not work with davidsaunders to port his code base to C++? His Spin examples could be converted to C++ running in CMM mode (equivalent size and speed) and his DAT sections remain as assembly drivers (.S files). I would think this would likely result in a single, better piece of firmware than either of you two can produce by yourselves.