Quote:
|
Originally Posted by Anthony Kesich
but couldnt you leave the turning=40 and the pwm01=pwm02=200 lines without brackets around them seeing as they are single line statements after a conditional? or are the brackets just to clean it up and make it more easily understandable?
|
It is syntactically correct to omit the braces (fyi brackets are [] and braces are {}) if the code following an if, for, while, etc is a single line. However it is bad practice to do so. Always using braces eliminates the chance that when you add a line later that you want to execute in the conditional, you forget to add the braces and introduce a bug. It may take some getting used to, and it may look sloppy in your eyes, but it will save you headaches in the long run, so always use braces.
Quote:
|
Originally Posted by Anthony Kesich
one more thing: pwm02 -= 254 does work. if I'm not mistaken. then that statement expands to pwm02=pwm02-254 whic will alwyas give you a negative value which does not work (or a really large value if pwm02 is unsigned). so souldn't it be redone to be pwm02=254-pwm02? but hey, i may be mistaken, correct me if i am for either part.
|
I really should know this, but I'll have to defer to someone with a difinitive answer. Assuming pwm02 is defined as a 'unsigned char' here are my two lines of thought.
- the processor does the calculation in terms of unsigned bytes and the subtraction rolls over from 0 to 255. You would get the correct result.
- since it's a 16 bit processor, it does the calculation in terms of words and the subtraction rolls over from 0 to 65535 then tries to store the result in a byte sized variable. Obviously this won't work.
My initial guess is that the second case is true, and a quick test using gcc tells me that's how my desktop behaves. Can someone confirm that this is how the controller behaves?