Pages: [1]   Go Down
Author Topic: A case of RTFM - bitSet  (Read 664 times)
0 Members and 1 Guest are viewing this topic.
Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   This might save someone some time.

   I am using bit flags to control access to shared variables between the loop function and interrupt service routines. To make my code as beginner friendly as possible I used the bit functions (bitSet,bitRead,bitWrite,bitClear).

   Everything works fine for my first 3 variables, but the 4th is never accessible.

   The reason is very simple and if I had been paying attention I would have spotted it hours ago. The Arduino bit functions quite reasonably regard the bits in a byte as numbered 0-7, so there is no bit 8 to set.

   Its obvious enough, but if you are used to using bits directly through powers of 2, you like me will have a problem.

   RTFM

Duane B

rcarduino.blogspot.com

Logged


Offline Offline
Edison Member
*
Karma: 23
Posts: 1173
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Same thing with arrays...

If you have a 10-element array and you read Array[10], you are reading something random/unknown beyond the array which holds elements 0 thru 9.  Worse, if you write beyond the end of the array screwy thingc can happen and you can even crash your program!

P.S.
If you always start counting at "zero', you might be a programmer!  smiley-grin  ...How many kids do you have?  "Four",  "0, 1, 2, 3"
« Last Edit: March 22, 2012, 01:28:56 pm by DVDdoug » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if you are used to using bits directly through powers of 2, you like me will have a problem.
:
Not sure what you mean there
20 = 1= 0x01
27 = 128 = 0x80
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
   Cut a long story short, I was using bit flags like so

#define CHANNEL_ONE 1
#define CHANNEL_TWO 2
#define CHANNEL_THREE 4
#define CHANNEL_FOUR 8

   Without paying enough attention to the function I was calling bitSet(n,CHANNEL_FOUR) which instead being equivalent to -

n |= 8

 was actually equivalent to

n |= (1<<8)

which overflows my byte.

Moderator edit: eliminated smileys

That was the first time I have used the Arduino bit functions, I am going back to bitwise operators.

Duane B

rcarduino.blogspot.com
« Last Edit: March 22, 2012, 02:19:03 pm by AWOL » Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Cut a long story short, I was using bit flags like so

#define CHANNEL_ONE 1
#define CHANNEL_TWO 2
#define CHANNEL_THREE 4
#define CHANNEL_FOUR 8

   Without paying enough attention to the function I was calling bitSet(n,CHANNEL_FOUR) which instead being equivalent to
Why not simply assign CHANNEL_N proper values - 0, 1, 3, and 7?
Logged

Pages: [1]   Go Up
Jump to: