1

I'm newbie in ARM experiment and I have a problem with concept of data in this processor. I ran into trouble. there is code to check timer interval:

// get the current timer 0 count
unsigned long Timer0_GetTimestamp(void) 
{
 return T0TC;
}

// check to see if a timestamp is in the past
// returns 1 if in the past, 0 if not
int Timer0_TimestampExpiredCk(unsigned long timestamp) 
{
 unsigned long now = T0TC;

if (now > timestamp)
{
 if ((now - timestamp) < 0x80000000)
  return 1;
 else
  return 0;
}
else
{
if ((timestamp - now) >= 0x80000000)
  return 1;
else
  return 0;
}
}

// pause for a specific number of milliseconds
void Timer0_Delay(unsigned long milliseconds) {
 unsigned long timestamp = Timer0_GetTimestamp() + milliseconds;
 while (!Timer0_TimestampExpiredCk(timestamp));
}

I have a problem with the number "0x80000000". Should we consider this number as a 2's complement or just binary ? it is supposed, when the difference between two variable is Zero we change our flag. Correct me if I wrong.

Thank you

1
  • 4
    Look to me like normal C/C++ code... Anything special about ARM in it? (adding C/C++ tags as it looks like basic C question) Jul 24, 2012 at 1:43

2 Answers 2

2

(your question has nothing to do with ARM, it is a language question).

what do you want that number to be? you can specify it 0x8000000UL will make it an unsigned long yes?

BTW if you are wanting 0x80000000 as an unsigned long then you can also just look at the msbit of the now-timestamp result

if((now-timestamp)&0x80000000)
   return 0;
else
   return 1;

and there is no ambiguity there.

or

return (~(now-timestamp))>>31; 

Might need an &1 on the end of that if unsigned long is 64 bits, not if unsigned long is 32.

3
  • Thanks dwelch for your reply. Jul 24, 2012 at 2:14
  • If your ARM is 32bits then 0x80000000u should also work AFAIK. Jul 24, 2012 at 2:17
  • 1
    thank you guys. I have seen this piece of code in timer library (time delay). maybe I have problem with the code, would you please tell me the logic of the code (Actually I can't understand why we compare the condition with 0x80000000)? Jul 24, 2012 at 2:39
0

In C, the constant 0x8000000 would be represented as an unsigned int as long as int is 32 bits or greater. This because the compiler must choose the first type from the following list in which the constant can be represented:

  • int
  • unsigned int
  • long
  • unsigned long
  • long long
  • unsigned long long

(C99 standard section 6.4.4.1)

And 0x80000000 can't be represented as a 32 bit int, at least not in 2's complement.

Both of the variables being compared are unsigned int. I can't see any purpose in this comparison with 0x80000000. Surely to check if a timestamp is in the past you just need to do the comparison

return (now > timeStamp);

Your code returns true if the time stamp is in the past unless it is a very long way in the past i.e. more than 0x80000000 milliseconds. It also returns true if the timestamp is a long way in the future.

What's T0TC by the way? If it's a constant, your timer will never expire.

4
  • I'd assume that T0TC is a macro to access the timer in the ARM (such things are quite usual in μC programming) Jul 24, 2012 at 9:18
  • Thanks jeremy& jonas. Your answers was really helpfull, however I saw this code in IAR IDE example for creating delays. I have a same opinion about the condition and I can't understand why they compare the result with this number!!!! Jul 24, 2012 at 17:39
  • @MohammadYousefi if the two variables were signed, it would be necessary to correct the overflow problem because large values (i.e. values >= 0x8000000) of the timer would appear to be negative.
    – JeremyP
    Jul 27, 2012 at 12:37
  • @JeremyP Thanks for your reply. I think I've found the answer. in this code we never have negative number because our variables are unsigned and we check(now and timestamp) to avoid negative numbers. programmer just wanted to check transition time and base on this decision he/she considers the timer reaches the desire point and passes it, so we are free of checking overflow.the number 0x8000000 is used as a reference number. Aug 1, 2012 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.