Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I use a volatile variable as a boolean?

6 views
Skip to first unread message

mahdert

unread,
Mar 23, 2010, 1:49:15 PM3/23/10
to
Hello,
I am doing imbedded programming in C and I was wondering how to use a
volatile variable as a Boolean. In this case, I am reading some data
and my variable is 'delta_X’ which is a floating point. I would like
the unit to power down if this value does not change, say for 1
minute. How do I do this?

if (delta_X does not change for one minute) {powerdown();}

Eric Sosman

unread,
Mar 23, 2010, 3:45:36 PM3/23/10
to

Assuming delta_X is the volatile variable, and that its
value changes "all by itself" with no other notifications or
side-effects, I think the best you can do is read delta_X
frequently, and note the time whenever its new value is unequal
to the last-read value:

void call_me_often(void) {
static float old_delta_X = SOME_IMPOSSIBLE_VALUE;
static time_t last_change_time;
float new_delta_X;
time_t current_time;

new_delta_x = delta_X;
current_time = time(NULL);
if (new_delta_x == old_delta_x) {
if (difftime(current_time, last_change_time) >= 60.0)
powerdown();
}
else {
old_delta_X = new_delta_X;
last_change_time = current_time;
}
}

(I'm not sure `float' is correct; adjust as needed. Also, a
free-standing implementation might not have time_t, time(), and
difftime(); substitute whatever timekeeping facilities are
available.)

A drawback of this approach is that if delta_X changes from
42 to 24 and then back to 42 between two call_me_often() calls,
you'll think there was no change. Unless there's more going on
than you've told us, I see no way to avoid the problem. Even
if your environment supports multiple threads of execution and
you can devote a thread to doing nothing but call_me_often()
over and over again, the change-and-change-back vulnerability
will still be there. If you have some knowledge of how fast
delta_X can change, and can poll faster, the problem goes
away -- but if delta_X can change faster than you can poll,
I think you'll just have to hope for the best.

--
Eric Sosman
eso...@ieee-dot-org.invalid

mahdert

unread,
Mar 24, 2010, 3:11:17 PM3/24/10
to
> esos...@ieee-dot-org.invalid

Thanks Eric,
I like your approach and I have used it before. However, I was asking
if the was a simple command in C that does this automatically.

Eric Sosman

unread,
Mar 24, 2010, 4:49:22 PM3/24/10
to
On 3/24/2010 3:11 PM, mahdert wrote:
> On Mar 23, 3:45 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>> On 3/23/2010 1:49 PM, mahdert wrote:
>> [...]
>> --
>> Eric Sosman
>> esos...@ieee-dot-org.invalid

Please don't quote signatures.

> Thanks Eric,
> I like your approach and I have used it before. However, I was asking
> if the was a simple command in C that does this automatically.

There is not.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Kenneth Brody

unread,
Mar 25, 2010, 1:59:43 PM3/25/10
to
On 3/24/2010 3:11 PM, mahdert wrote:
> On Mar 23, 3:45 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>> On 3/23/2010 1:49 PM, mahdert wrote:
>>
>>> Hello,
>>> I am doing imbedded programming in C and I was wondering how to use a
>>> volatile variable as a Boolean. In this case, I am reading some data
>>> and my variable is 'delta_X’ which is a floating point. I would like
>>> the unit to power down if this value does not change, say for 1
>>> minute. How do I do this?
[... snip code sample ...]

>> A drawback of this approach is that if delta_X changes from
>> 42 to 24 and then back to 42 between two call_me_often() calls,
>> you'll think there was no change.

It also depends on whether or not "delta_X contains 42, and the external
source wrote 42 to delta_X again" counts as "change".

[...]


> I like your approach and I have used it before. However, I was asking
> if the was a simple command in C that does this automatically.

Perhaps the hardware you're on supports hardware breakpoints on memory
accesses? If so, there may be some platform-specific way of saying "has
address X been written to" or "call this function when address X is written to".

But, lacking that, there is nothing available in standard C to
"automatically" do what you are asking.

--
Kenneth Brody

0 new messages