I am trying to do a straight forward subtraction and am getting the wrong result every time. The code is as simple as
if(SetPoint>PulseCountLast) { error = SetPoint-PulseCountLast; }
All three variables are defined as uint16.
As you can see I ensured that I dont get a negative error value. Although I did try using error as signed number but still the faced the same issue. In any case the issue is when debugging is that SetPoint has a value of 0x0064 and PulseCountLast has a value of 0x0000 so I would expect an error value of 0x0065, however, the code always results in an error value of 0x0001 no matter what value the operands have.
I am really confused at this point what it might be...I would appreciate some help.
Maybe I miss something, but isn't it true that 0x64 - 0 = 0x64 ? Why do you expect 0x65?
I don't think you have given us all code relevant to help you.
But anyway. If you do 0x0064 - 0x0000, you can't really expect the answer to be 0x0065.
Another thing. Are you sure that your _real_ code doesn't do: error = SetPoint > PulseCountLast, in which case error will be zero or one since it will be assigned with the result of a comparison and not the result of a subtraction?
Are you sure that your _real_ code doesn't do: error = SetPoint > PulseCountLast
That must be it!
The 0x0065 was a typo I meant I should be seeing 0x0064. However, it was not the point anyways the 0x0001 that I am seeing as a result is pretty far off of 0x0064 or even 0x0065.
And my real code does not do error = SetPoint > PulseCountLast. I'm not sure if that type of syntax would even compile right.
"The 0x0065 was a typo I meant I should be seeing 0x0064"
You also didn't notice the clear instructions on how to post source code:
www.danlhenry.com/.../keil_code.png
So maybe there are some similar little "details" that you have overlooked in your code.
Of course, if you don't post your real code - nobody can comment on your real code!
"I'm not sure if that type of syntax would even compile"
Yes, it is perfectly valid - check your 'C' textbook!
I'm sorry I'm not as perfect as everybody else. I guess posting that one liner according to the instructions would have made a world of difference. When I posted my question I was not asking for a lesson in subtraction. Jumping on a typo to be like "gotcha" isnt particularly what I was looking for. Also making assumptions about my code is another thing. Whether it compiles or not you can clearly see that its not part of the code that I posted. I'm glad to see how helpful this experience has been.
In any case I figured the issue out.
You figured the issue out, you say.
So, was the problem part of what you posted? Or something you didn't post?
Remember that you can't really complain about the answer you get, if the quality of the answers is highly affected by the quality of the question.
I guess posting that one liner according to the instructions would have made a world of difference.
A world? Not really, no. But since you didn't really give us other clues, you didn't leave people much of a choice but to make assumptions based on what you did do: you failed to follow some pretty simple instructions, and made a pretty blatant error in some trivial maths at the core of your problem statement.
When I posted my question I was not asking for a lesson in subtraction. Jumping on a typo to be like "gotcha" isnt particularly what I was looking for
Well, by asking a question you give up the privilege of not having to listen to answers you don't like.
So, since you've pretty much failed to impress us thus far, why don't you try to demonstrate how clever you are by explaining what the real issue actually was? It's kind of impolite to claim you have the solution and not tell anyone about it. Mathematicians have been mad as hell at that Fermat fellow for centuries because of a stunt like that.
It's kind of impolite to claim you have the solution and not tell anyone about it. Mathematicians have been mad as hell at that Fermat fellow for centuries because of a stunt like that.
:-)
Just a general comment: so many messages on this forum emanate from people who seem to be so careless about details. They make typos. They "forget" important facts. It's like they got out of class, posted something and went to eat a sandwich...
and went to eat a sandwich...
Good thing we don't have to deal with digital grease stains on the postings then. ;)