473,382 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Help w/ bit testing

Hello all...

I'm new to the world of C programming, and am working with an embedded
microcontroller as a hobby. I have a reasonably simple program
running now, but I'm having trouble figuring out how to test for a
specific bit when an interrupt is triggered.

Basically when the interrupt is set, the program reads a register to
determine what caused the interrupt. It could be one or both of 2
values - bit 2 and/or biit 4. Depending on which or both it is, I
want to do different things.

If the hex result of the interrupt register is 0x08, bit 4 is set -
OK, likewise 0x02 would mean bit 2 - again, no problem. 0x10 would
mean both were set.

My question is more of style, and more complicated situations - is the
best way to handle this just with IF statements and test for the 3
different conditions? Likewise, if I want to use some of the other
interrupt conditions, how would I handle circumstances with 5 or 6
different interrupts, and a ton of different possibilities?

Thanks up front for any help from an old guy that is trying to catch
up!
Nov 15 '05 #1
3 1580

Shawn wrote:
Hello all...

I'm new to the world of C programming, and am working with an embedded
microcontroller as a hobby. I have a reasonably simple program
running now, but I'm having trouble figuring out how to test for a
specific bit when an interrupt is triggered.

Basically when the interrupt is set, the program reads a register to
determine what caused the interrupt. It could be one or both of 2
values - bit 2 and/or biit 4. Depending on which or both it is, I
want to do different things.

If the hex result of the interrupt register is 0x08, bit 4 is set -
OK, likewise 0x02 would mean bit 2 - again, no problem. 0x10 would
mean both were set.
Incorrect. 0x10 would be 16 in decimal.
0x08 + 0x02 = 0xA = 0b1010 (binary) = 10 in decimal.

My question is more of style, and more complicated situations - is the
best way to handle this just with IF statements and test for the 3
different conditions? Likewise, if I want to use some of the other
interrupt conditions, how would I handle circumstances with 5 or 6
different interrupts, and a ton of different possibilities?
I would consider a switch statement.
Thanks up front for any help from an old guy that is trying to catch
up!


Nov 15 '05 #2
Shawn wrote:
Hello all...

I'm new to the world of C programming, and am working with an
embedded microcontroller as a hobby. I have a reasonably simple
program running now, but I'm having trouble figuring out how to test
for a specific bit when an interrupt is triggered.

Basically when the interrupt is set, the program reads a register to
determine what caused the interrupt. It could be one or both of 2
values - bit 2 and/or biit 4. Depending on which or both it is, I
want to do different things.

If the hex result of the interrupt register is 0x08, bit 4 is set -
OK, likewise 0x02 would mean bit 2 - again, no problem. 0x10 would
mean both were set.

My question is more of style, and more complicated situations - is
the best way to handle this just with IF statements and test for the
3 different conditions? Likewise, if I want to use some of the other
interrupt conditions, how would I handle circumstances with 5 or 6
different interrupts, and a ton of different possibilities?

Thanks up front for any help from an old guy that is trying to catch
up!


It completely depends on what you want to do. It's not a C-specific
problem, strictly speaking, just a question of design.

If bit 2 and 4 are set, do you want to perform the actions associated
with bit 2, then those with bit 4, or do you want to do something
completely different altogether? Do you want the option of altering the
actions at runtime for every possible combination of bits? Are the
actions drawn from a small pool of predefined possibilities?

Here's one basic approach, assuming that the conditions are independent
and you do not need runtime flexibility.

#define FOO 0x02
#define BAR 0x08

extern volatile unsigned char global_register;

void handle_interrupt(void) {
if (global_register & FOO) {
/* Actions associated with FOO condition */
}
if (global_register & BAR) {
/* Actions associated with BAR condition */
}
}

If the conditions are interdependent, you could do:

#define FOOBAR (FOO | BAR)

void handle_interrupt(void) {
switch (global_register) {
case FOO: {
/* Actions associated with FOO-only condition, */
}; break;
case BAR: {
/* Actions associated with BAR-only condition */
}; break;
case FOOBAR: {
/* Actions associated with FOO and BAR-condition */
}; break;
default: {
/* Default actions if no predefined condition combinations apply */
}
}

You can even mix these. Here's assuming that FOO and BAR are dependent,
but some BAZ has two independent FOO and BAR conditions:

#define BAZ 0x..

void handle_interrupt(void) {
switch (global_register) {
case FOO: {
/* Actions associated with FOO-only condition */
}; break;
case BAR: {
/* Actions associated with BAR-only condition */
}; break;
case FOOBAR: {
/* Actions associated with FOO and BAR-condition */
}; break;
default: {
if (global_register & BAZ) {
if (global_register & FOO) {
/* Actions associated with BAZ and FOO condition */
}
if (global_register & BAR) {
/* Actions associated with BAZ and BAR condition */
}
} else {
/* Default actions */
}
}

This rapidly gets hard to read, however; I recommend turning the various
branches in functions.

If you want the option of associating handlers for each condition or
combination of condition at runtime, things change yet again, and you'll
want a (possibly dynamic) table of bitmasks. I'll defer this.

Basically, you can get as simple or complicated as you like, depending
on your needs.

S.
Nov 15 '05 #3
Shawn wrote:

Hello all...

I'm new to the world of C programming, and am working with an embedded
microcontroller as a hobby. I have a reasonably simple program
running now, but I'm having trouble figuring out how to test for a
specific bit when an interrupt is triggered.

Basically when the interrupt is set, the program reads a register to
determine what caused the interrupt. It could be one or both of 2
values - bit 2 and/or biit 4. Depending on which or both it is, I
want to do different things.

If the hex result of the interrupt register is 0x08, bit 4 is set -
OK, likewise 0x02 would mean bit 2 - again, no problem. 0x10 would
mean both were set.
Actually, bits 4 and 2 (using your 1-relative numbering) set would
be 0x0A (or decimal 10) and not 0x10.
My question is more of style, and more complicated situations - is the
best way to handle this just with IF statements and test for the 3
different conditions? Likewise, if I want to use some of the other
interrupt conditions, how would I handle circumstances with 5 or 6
different interrupts, and a ton of different possibilities?

Thanks up front for any help from an old guy that is trying to catch
up!


One way would be switch:

switch( value & 0x0a )
{
case 0x02:
... only bit 2 set ...
break;
case 0x08:
... only bit 4 set ...
break;
case 0x0a:
... both set ...
break;
case 0:
... neither set ...
break;
}

You mention multiple bits being set. If you have 5 different bits,
do you really need to do 32 different things, or do you need to do
something specific for each bit that is set? If that's the case,
then a series of if's would do it:

if ( value & 0x80 )
{
... bit 8 is set ...
}
if ( value & 0x40 )
{
... bit 7 is set ...
}
if ( value & 0x20 )
{
... bit 6 is set ...
}
... and so on down to 0x01 ...

And, if you really do need 32 different things, then switch/case is
probably the way to go.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
19
by: What-a-Tool | last post by:
I have a school project (ASP) in which I have to call three different ASP pages from three different and identical (except for the form "action", obviously) HTM pages. This I have no problem with....
6
by: gsb | last post by:
Don't know if this is the right place to post this JavaScript issue. If not, could someone point me in the right direction please. I am trying to make a "cross browser compliant" floating...
0
by: Bennett F. Dill | last post by:
Thanks for reading. I'm having problems with cookies from asp to asp.net and back! It seems like I can set a cookie in asp.net fine, and alter it at will, as soon as asp touches it, asp.net...
6
by: rekaeps | last post by:
We are developing an ASP.NET 2.0 (C#) application, and I'm having troubles sending e-mail from the server when accessing the web site from a separate client computer. Also, in the same scenario,...
14
by: c676228 | last post by:
Hi everyone, Our site is down, because of our hosting company applied sql server 2000 sp4 on windows 2000 server. right after it applied the service pack, our sql server database is down and...
2
by: Shankar | last post by:
Hi All, I completed my B.Sc. I want to settle as a s/w Tester that why I had learned Testing Tools. Please give ur suggestions to achieve my target. I dont have any experience in s/w Testing....
18
by: Andrew Wan | last post by:
I have been developing web applications with ASP & Javascript for a long time. I have been using Visual Studio 2003.NET. While VS2003 is okay for intellisense of ASP & Javascript, it's still not...
0
by: jyoti sheth | last post by:
Hi, I got task to perform API testing using python & I don't have any knowledge of API testing it would be a gre8 help if anyone can give pointers for learning API testing using python (any...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.