Pages: [1]   Go Down
Author Topic: (Solved) bitWrite (0xFE, 15, 1); Does not compute (correctly)  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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;
}
« Last Edit: May 17, 2012, 11:54:42 pm by mrdeek » Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13016
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


unsigned LED_New_ShiftRegister = 255;
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1283
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley not a 16bit register ...

Thank you.
Logged

Pages: [1]   Go Up
Jump to: