CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Unbelievable result of an subtraction

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
wedilo



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

Unbelievable result of an subtraction
PostPosted: Mon Mar 22, 2004 2:29 am     Reply with quote

Hello folks,
Please help me by this strange problem. I can't find my mistake...
The following code is to determine if an pulse found or not. If not then show a message. For the present...

For info, all variables lg... are long and global

Code:
if (lgCounter - lgLastPulse >= lgTimer_Calls_Per_Sec * 3)
{
   bgSignal_OK = False;
   printf("No signal detected - %lu-%lu >= %lu * 3 --> %lu >= %lu\r\n"_
   , lgCounter, lgLastPulse, lgTimer_Calls_Per_Sec, lgCounter - lgLastPulse, lgTimer_Calls_Per_Sec * 3);
   output_b(~0x02);
}

Most of the time it works, but after some minutes I get a message like this:
'No signal detected - 2048-2044 >= 20 * 3 --> 4 >= 60'
I'm sure there is no overflow or anything else.

I CAN'T believe, that the subtraction of '2048-2044' is greater than '20 * 3'
I'm driving crazy... Please help

73 Sven
Pete Smith



Joined: 17 Sep 2003
Posts: 55
Location: Chester, UK

View user's profile Send private message Visit poster's website MSN Messenger ICQ Number

Re: Unbelievable result of an subtraction
PostPosted: Mon Mar 22, 2004 3:35 am     Reply with quote

wedilo wrote:
Hello folks,
Please help me by this strange problem. I can't find my mistake...
The following code is to determine if an pulse found or not. If not then show a message. For the present...

For info, all variables lg... are long and global

Code:
if (lgCounter - lgLastPulse >= lgTimer_Calls_Per_Sec * 3)
{
   bgSignal_OK = False;
   printf("No signal detected - %lu-%lu >= %lu * 3 --> %lu >= %lu\r\n"_
   , lgCounter, lgLastPulse, lgTimer_Calls_Per_Sec, lgCounter - lgLastPulse, lgTimer_Calls_Per_Sec * 3);
   output_b(~0x02);
}

Most of the time it works, but after some minutes I get a message like this:
'No signal detected - 2048-2044 >= 20 * 3 --> 4 >= 60'
I'm sure there is no overflow or anything else.

I CAN'T believe, that the subtraction of '2048-2044' is greater than '20 * 3'
I'm driving crazy... Please help

73 Sven


Print out (before the if statement) the values of each of the terms, and the results of the maths.

Something along the lines of

printf("%lu %lu %lu %lu",lgCounter,lgLastPulse ,lgTimer_Calls_Per_Sec * 3, (lgCounter-lgLastPulse));

Also, check that all your variables really are long. It could even be that by doing *3, you're casting the lgTimer_Calls_per_sec to an int.

Pete.
Ttelmah
Guest







Re: Unbelievable result of an subtraction
PostPosted: Mon Mar 22, 2004 6:02 am     Reply with quote

wedilo wrote:
Hello folks,
Please help me by this strange problem. I can't find my mistake...
The following code is to determine if an pulse found or not. If not then show a message. For the present...

For info, all variables lg... are long and global

Code:
if (lgCounter - lgLastPulse >= lgTimer_Calls_Per_Sec * 3)
{
   bgSignal_OK = False;
   printf("No signal detected - %lu-%lu >= %lu * 3 --> %lu >= %lu\r\n"_
   , lgCounter, lgLastPulse, lgTimer_Calls_Per_Sec, lgCounter - lgLastPulse, lgTimer_Calls_Per_Sec * 3);
   output_b(~0x02);
}

Most of the time it works, but after some minutes I get a message like this:
'No signal detected - 2048-2044 >= 20 * 3 --> 4 >= 60'
I'm sure there is no overflow or anything else.

I CAN'T believe, that the subtraction of '2048-2044' is greater than '20 * 3'
I'm driving crazy... Please help

73 Sven

My suspicion is that lgCounter is being changed in an interrupt driven event?. If so, then the problem is that you are encountering an interrupt between reading the first and second byte in the comparison. Note that the value after the comparison, is an even binary value (2048). Hence the counter will have jumped from 07FF to 0800 at the last interrupt. At the moment of the comparison, assume that the LSB had been read, and then the interrupt occurred before the MSB was read. The comparison would then be using the value 08FF, which would result in a 'true' test.
When comparing 16 bit values, that are changed in an interrupt, you need to either disable the interrupt for the duration of the comparison, or copy the value into a temporary register, with the interrupts disabled for the copy.

Best Wishes
wedilo



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

PostPosted: Mon Mar 22, 2004 6:57 am     Reply with quote

Hello Pete, hello Ttelmah,
Yeah, I think that's the problem. The interupt occur during the read of high and low byte.
But I thought until yet, something like this is not possible. Oops!
I will try to backup lgCounter before the duration of the comparison.

Thank you very MUCH...

73 Sven
Ttelmah
Guest







PostPosted: Mon Mar 22, 2004 7:07 am     Reply with quote

wedilo wrote:
Hello Pete, hello Ttelmah,
Yeah, I think that's the problem. The interupt occur during the read of high and low byte.
But I thought until yet, something like this is not possible. Oops!
I will try to backup lgCounter before the duration of the comparison.

Thank you very MUCH...

73 Sven

Remember to disable the interrupts when you save the copy of the counter, or the same problem can still 'catch'!...

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group