Pages: [1]   Go Down
Author Topic: Problems with bitWrite  (Read 794 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I've been trying to store some values in a byte using bitWrite, but it doesn't seem to be working.
Code:
{
  byte KMAP = B00000000;
  byte x;
  for (x = 0; x < 8; x++)  {
    PORTC = x;
    delay(2);
    bitWrite(KMAP, x, digitalRead(in));
  }
}
This is my code, and on the surface, I can't see anything wrong with it?  Help would be appreciated immensely.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31539
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but it doesn't seem to be working.
So tell us what it does and how you know this.
This is only a fragment of code and it does not give any clear context.
From the code I would expect the resulting value to be all zeros or all ones, what are you expecting?
Logged

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

Sorry, yes, so it toggles outputs based on x while counting up, then reads an input pin and attempts to store the output into bit x of byte KMAP, but even when I tell it to print the value that it is reading into bit x, it always comes up as 1, and even though therefore KMAP should contain 255, it contains 0.  I am confused as to why KMAP contains 0 even when I'm telling it to write 1's or anything else to the bits.
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31539
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it toggles outputs based on x while counting up,
No it does not. It puts the binary value of x on to PORTC.

Quote
I am confused as to why KMAP contains 0 even when I'm telling it to write 1's or anything else to the bits.
No you are telling it to write what ever you read on pin defined by the variable 'in'
As I have said a small code fragment will not tell the whole tale. The code is working as I would expect, why do you expect anything different?
Logged

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

So putting the binary value of x onto PORTC doesn't turn the pins on and off? I'm not sure how this works then, I thought that it would turn be like if x is 101 in binary, then A0 would be high, A1 would be low, and A2 would be high.  Anyway, this is my entire code.
Code:
#include <LiquidCrystal.h>
#include <ST7565.h>
#include <SPI.h>
#define in 10
#define button 9
#define SW1 8
byte KMAP/*2][4]*/;
String BOOL = "";
String Old = "";
byte t;
ST7565 glcd(9, 8, 7, 6, 5);
String BL = ("A'B'C'","A'B'C","A'BC'","A'BC","AB'C'","AB'C","ABC'","ABC");
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
/* This section sets up the intial conditions required to execute
 /  all subsequent functions, mostly setting the pins as input
 /  and output and intializing the LCD libraries.
 */
{

  //pinMode(outC, OUTPUT);              //Set these pins to output
  //pinMode(outB, OUTPUT);
  //pinMode(outA, OUTPUT);
  pinMode(in, INPUT);                 //Set these pins to input
  digitalWrite(in, HIGH);
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);         //Turn on internal pullup resistor
  Serial.begin(9600);                 //Start serial communication
  Serial.println("Serial begin.");    //Print the test line
  Serial.println(freeRam());
  //glcd.begin(0x18);
  lcd.begin(16, 2);
  //glcd.st7565_init();
  //glcd.st7565_command(CMD_DISPLAY_ON);
  //glcd.st7565_command(CMD_SET_ALLPTS_NORMAL);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  //lcd.print("Hello World");

}
void loop() 
{
  if (digitalRead(button) == LOW) {
    digitalWrite(13, HIGH);
    READ();
    CONV();
  }
  else {
  digitalWrite(13, LOW);
  }
  DISP();
  //glcd.display();
}
   
  /*digitalWrite(13, LOW);
  if (digitalRead(SW1) == LOW)  {
    DISPT();
  }
  else if (digitalRead(SW1) == HIGH)  {
    DISPB();
  }*/



void READ() 
/* This section is the read and store section; it interacts with
 /  the outside world via the designated output and input pins.
 /  This section will write to each output, counting up in binary
 /  with the MSB called A, and the LSB called C.
 */
{
 
  byte KMAP = B00000000;
  byte x;
  for (x = 0; x < 8; x++)  {
    PORTC = x;
    delay(2);
    bitWrite(KMAP, x, digitalRead(in));
  }
 
}

void CONV() 
/* This is the Karnaugh mapping section, which checks for contiguous
 /  sections of true outputs on a grid created in the array KMAP.
 /  This simplifies the expression by eliminating variables whose level
 /  is irrelevant because the output is true when it is true or false.
 /  Simplified expressions are added to string BOOL.
 */
{
  BOOL = "";
  byte TEMP = KMAP;
  if (KMAP == 255)  {
    BOOL = "All conditions are true.";
    t = 1;
    return;
  }
  if (KMAP == 0)  {
    BOOL = "All conditions are false.";
    t = 1;
    return;
  }
  if (TEMP & 90)  {
    BOOL += "C' + ";
    TEMP &= 165;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 165)  {
    BOOL += "C + ";
    TEMP &= 90;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 15)  {
    BOOL += "A' + ";
    TEMP &= 240;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 240)  {
    BOOL += "A + ";
    TEMP &= 15;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 51)  {
    BOOL += "B' + ";
    TEMP &= 204;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 204)  {
    BOOL += "B + ";
    TEMP &= 51;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 3)  {
    BOOL += "A'B' + ";
    TEMP &= 252;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 12)  {
    BOOL += "A'B + ";
    TEMP &= 243;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 48)  {
    BOOL += "AB' + ";
    TEMP &= 207;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 192)  {
    BOOL += "AB + ";
    TEMP &= 63;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 5)  {
    BOOL += "A'C' + ";
    TEMP &= 250;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 10)  {
    BOOL += "A'C + ";
    TEMP &= 245;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 80)  {
    BOOL += "AC' + ";
    TEMP &= 175;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 160)  {
    BOOL += "AC + ";
    TEMP &= 95;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 17)  {
    BOOL += "B'C' + ";
    TEMP &= 238;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 34)  {
    BOOL += "B'C + ";
    TEMP &= 221;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 68)  {
    BOOL += "BC' + ";
    TEMP &= 187;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 136)  {
    BOOL += "BC + ";
    TEMP &= 119;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 1)  {
    BOOL += "A'B'C' + ";
    TEMP &= 254;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 2)  {
    BOOL += "A'B'C + ";
    TEMP &= 253;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 4)  {
    BOOL += "A'BC' + ";
    TEMP &= 251;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 8)  {
    BOOL += "A'BC + ";
    TEMP &= 247;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 16)  {
    BOOL += "AB'C' + ";
    TEMP &= 239;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 32)  {
    BOOL += "AB'C + ";
    TEMP &= 223;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 64)  {
    BOOL += "ABC' + ";
    TEMP &= 191;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 128)  {
    BOOL += "ABC + ";
    TEMP &= 127;
  }
}

void DISP()
{
  if (!t)  {
    BOOL[BOOL.length() - 2] = NULL;
  }
 
  if (BOOL != Old)  {
    Serial.println(BOOL);
    Old = BOOL;
    lcd.println(BOOL);
    Serial.println(KMAP);
  }

}

//void DISPT()  {
// 
//  // Displays the truth table for the logic circuit.
//  //glcd.drawchar(
//  for (byte x = 0; x < 8; x++)  {
//    glcd.drawstring(x, 0, (char*)BL[x]);
//  }
//  /*glcd.drawstring(1, 0, "A'B'C");
//  glcd.drawstring(2, 0, "A'BC'");
//  glcd.drawstring(3, 0, "A'BC");
//  glcd.drawstring(4, 0, "AB'C'");
//  glcd.drawstring(5, 0, "AB'C");
//  glcd.drawstring(6, 0, "ABC'");
//  glcd.drawstring(7, 0, "ABC");*/
// 
//  byte y;
//  for (y = 0; y < 8; y++)  {
//    glcd.drawchar(y, 8, bitRead(KMAP, y));
//  }
//  /*
// 
//  byte x;
//  byte y;
//  for (x = 0; x < 2; x++)  {
//    for (y = 0; y < 3; y++)  {
//      glcd.drawchar(y, x, KMAP[x][y]);
//    }
//    glcd.drawchar(y, x, KMAP[x][y]);
//  }*/
//}
//
//void DISPB()  {
//  // Displays the boolean expression for the logic cicuit.
//  int L = BOOL.length() - 2;
//  int l;
//  byte m;
//  for (l = 0; l < L; l++)  {   
//    glcd.drawchar(m, 0, BOOL[l]);
//    m++;
//    Serial.print(BOOL[l]);
//    Serial.print(" ");
//  }
//}

int freeRam()  {
 
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31539
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought that it would turn be like if x is 101 in binary, then A0 would be high, A1 would be low, and A2 would be high.
Yes it would but you said it toggled the bits, that is not what you described above. Also note that as you never set these pins to be outputs then you will never see anything coming out.

Have you tried, as a test, replacing the
Code:
bitWrite(KMAP, x, digitalRead(in));
with
Code:
bitWrite(KMAP, x, 1);
Logged

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

I just did so, uploaded to the board, and it still gives me 0 in KMAP and "All conditions are false." in BOOL.  I've also tried moving the digitalRead outside of the function like so:
Code:
byte n = digitalRead(in);
bitWrite(KMAP, x, n);

Which did not work either.
Logged

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

You have a global variable named KMAP and a local variable, in READ() called KMAP. It is the local variable that you are manipulating. Which one are you printing? Clearly not the one in READ() as there are no Serial.print() statements there. It is not the local one you are printing outside of READ().
Logged

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

Thanks a bunch, I didn't realize that.  Now that I've removed the byte in front of KMAP in READ() it starts working correctly, thank you!
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Was that supposed to toggle the pins one at a time?  If x =3 youre actually turning on two pins
  When x=7 three pins.  If you want to go through the pins one at a time then you need to write 1,2,4,8,16,32,64, 128 to the port, not 1,2,3,4,5,6,7,8.
Logged

Pages: [1]   Go Up
Jump to: