Pages: [1]   Go Down
Author Topic: Bug in bitWrite ?  (Read 662 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a function that is supposed to set or clear bits in a global variable

byte   outData[4];


void WriteData( int bitNo, int val )
{
  int bytePos = bitNo / 8;
  int bitPos = bitNo % 8;
/*
// this commented out bit works perfectly
  if ( val == 0 )
    bitClear( outData[ bytePos ], bitPos);
  else
    bitSet( outData[ bytePos ], bitPos);
*/

// but this bitWrite function will clear bits but not set them
  bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );
  delay(2000);
  WritePort(bytePos,outData[ bytePos ]);
}

Looks like a bug to me - unless I am missing something somewhere....

Dave.
« Last Edit: June 25, 2010, 04:25:30 am by calistra » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
So
Code:
bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );

expands to:
Code:
(val != 0) ? (1) : (0) ? bitClear(outData[ bytePos ], bitPos) : bitSet (outData[ bytePos ], bitPos);

Try instead:
Code:
bitWrite( outData[ bytePos ], bitPos, val);
« Last Edit: June 25, 2010, 04:47:01 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Cape Town, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 177
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
bitWrite( outData[ bytePos ], bitPos, (val != 0) ? (1) : (0) );

You want to do this rather.

Code:
bitWrite( outData[ bytePos ], bitPos, val);

or

Code:
bitWrite( outData[ bytePos ], bitPos, ((val != 0) ? (1) : (0)) );

Note the braces.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2336
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must mean parentheses instead of brackets.
Logged

Cape Town, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 177
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You must mean parentheses

I do   :smiley
Logged

Pages: [1]   Go Up
Jump to: