Go Down

Topic: (Solved) bitWrite (0xFE, 15, 1); Does not compute (correctly) (Read 606 times) previous topic - next topic

mrdeek

May 18, 2012, 04:09 am Last Edit: May 18, 2012, 06:54 am by mrdeek Reason: 1
Hello,

I'm running a ProMini and I'm seeing an unexpected result. Can someone please confirm if it's my code or the Arduino Mal-processing the result.

What i expect is to see in the register:
00000000 // Clear
then
00000001
00000011
...
00111111
01111111
11111111

But what i get is ...

00000000
then
00000001
00000011
...
00111111
01111111
-1               <--- WHAT THE, That's not 0xFF or B1111111?

What i see happening is when i try to bitWrite bit 15 to a 1 ... I get a overflow and not a 0xFF. Is this me or the Arduino?  (I also see variable containing the bit 15 change to bit 16?)

SOLUTION:
int LED_New_ShiftRegister = 255;
SHOULD BE:
unsigned int LED_New_ShiftRegister = 0xFFFF;



THANK YOU for any help!
Mr.Deek


------------------------
Clode Example Below:

Code: [Select]


int LED_New_ShiftRegister = 255;

void loop()
{

// Set the shift register to 00000000
for (int X=0; X<16; X++)
 {
   LEDModRegister(X, 0);
 };
 LEDShiftOut();
 delay(1000);

// Step the shift register +1 to 00000001 .. to 11111111
for (int X=0; X<16; X++)
 {
   LEDModRegister(X, 1);
   LEDShiftOut();
   delay(100);
 };

 delay(1000);
}


// Function
int LEDModRegister(int WhichLED, int LEDSTATE)
{
 Serial.print ("SHIFT1: ");
 Serial.print (WhichLED);
 Serial.print ("\n");
 bitWrite(LED_New_ShiftRegister, WhichLED, LEDSTATE);
 Serial.print ("SHIFT2: ");
 Serial.print (WhichLED);
 Serial.print ("\n");
 return true;
}

Coding Badly


[font=Courier New]unsigned LED_New_ShiftRegister = 255;[/font]

marco_c

What you are seeing as -1 is just the bit pattern 11111111 as a signed (two's complement) byte, as per previous reply

The data a (raw bits) and how they are displayed are 2 different things (worth remembering). Bit pattern B00110000 can be shown as Hex 0x30, decimal 48 or the ASCII character '0', depending on how you want to interpret the bit pattern. This property, although it seems obvious, is often forgotten and it can be useful when you think about programming problems.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

mrdeek


Yup that solved it ...  (unsigned int == 16bit Byte) ... lol

unsigned int VAR_NAME = 0xFFFF;

I thought i was having another issue and changed unsigned int to a byte ( hoping i could use 8 bits) but for some reason forgot it was only 8bits :) not a 16bit register ...

Thank you.

Go Up