How to compare a memory bits in C++?

Posted by Trunet on Stack Overflow See other posts from Stack Overflow or by Trunet
Published on 2010-12-24T01:19:24Z Indexed on 2010/12/24 3:54 UTC
Read the original article Hit count: 300

Filed under:
|
|
|
|

Hi,

I need help with a memory bit comparison function.

I bought a LED Matrix here with 4 x HT1632C chips and I'm using it on my arduino mega2560.

There're no code available for this chipset(it's not the same as HT1632) and I'm writing on my own. I have a plot function that get x,y coordinates and a color and that pixel turn on. Only this is working perfectly.

But I need more performance on my display so I tried to make a shadowRam variable that is a "copy" of my device memory. Before I plot anything on display it checks on shadowRam to see if it's really necessary to change that pixel. When I enabled this(getShadowRam) on plot function my display has some, just SOME(like 3 or 4 on entire display) ghost pixels(pixels that is not supposed to be turned on).

If I just comment the prev_color if's on my plot function it works perfectly.

Also, I'm cleaning my shadowRam array setting all matrix to zero.

variables:

#define BLACK  0
#define GREEN  1
#define RED    2
#define ORANGE 3
#define CHIP_MAX 8
byte shadowRam[63][CHIP_MAX-1] = {0};

getShadowRam function:

byte HT1632C::getShadowRam(byte x, byte y) {
  byte addr, bitval, nChip;

  if (x>=32) {
    nChip = 3 + x/16 + (y>7?2:0);
  } else {
    nChip = 1 + x/16 + (y>7?2:0);
  }

  bitval = 8>>(y&3);

  x = x % 16;
  y = y % 8;
  addr = (x<<1) + (y>>2);

  if ((shadowRam[addr][nChip-1] & bitval) && (shadowRam[addr+32][nChip-1] & bitval)) {
    return ORANGE;
  } else if (shadowRam[addr][nChip-1] & bitval) {
    return GREEN;
  } else if (shadowRam[addr+32][nChip-1] & bitval) {
    return RED;
  } else {
    return BLACK;
  }
}

plot function:

void HT1632C::plot (int x, int y, int color)
{
 if (x<0 || x>X_MAX || y<0 || y>Y_MAX)
  return;

 if (color != BLACK && color != GREEN && color != RED && color != ORANGE)
     return;

 char addr, bitval;
 byte nChip;

 byte prev_color = HT1632C::getShadowRam(x,y);

 bitval = 8>>(y&3);

 if (x>=32) {
  nChip = 3 + x/16 + (y>7?2:0);
 } else {
  nChip = 1 + x/16 + (y>7?2:0);
 }

 x = x % 16;
 y = y % 8;
 addr = (x<<1) + (y>>2);

 switch(color) {
  case BLACK:
   if (prev_color != BLACK) { // compare with memory to only set if pixel is other color
    // clear the bit in both planes;
    shadowRam[addr][nChip-1] &= ~bitval;
    HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
    shadowRam[addr+32][nChip-1] &= ~bitval;
    HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
   }
   break;
     case GREEN:
      if (prev_color != GREEN) { // compare with memory to only set if pixel is other color
       // set the bit in the green plane and clear the bit in the red plane;
       shadowRam[addr][nChip-1] |= bitval;
       HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
       shadowRam[addr+32][nChip-1] &= ~bitval;
       HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
      }
      break;
     case RED:
      if (prev_color != RED) { // compare with memory to only set if pixel is other color
    // clear the bit in green plane and set the bit in the red plane;
    shadowRam[addr][nChip-1] &= ~bitval;
    HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
    shadowRam[addr+32][nChip-1] |= bitval;
    HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
     }
     break;
     case ORANGE:
     if (prev_color != ORANGE) { // compare with memory to only set if pixel is other color
    // set the bit in both the green and red planes;
    shadowRam[addr][nChip-1] |= bitval;
    HT1632C::sendData(nChip, addr, shadowRam[addr][nChip-1]);
    shadowRam[addr+32][nChip-1] |= bitval;
    HT1632C::sendData(nChip, addr+32, shadowRam[addr+32][nChip-1]);
     }
     break;
 }
}

If helps: The datasheet of board I'm using. On page 7 has the memory mapping I'm using.

Also, I have a video of display working.

© Stack Overflow or respective owner

Related posts about c++

Related posts about c