title: Extracting nibbles from bytes in a for loop
body: <p>I'm having some problems with a DMA interrupt handler I've adapted for an LCD display on a MicroChip PIC32 embedded chip.</p>
  
  <p>I have limited internal memory and need a frame buffer for my colour LCD. Have decided to use 16 colours and use a nibble per pixel. I've created an array which looks like this: </p>
  
  <pre><code>unsigned char GraphicsFrame[FRAME_HEIGHT][LINE_LENGTH/2]; 
  </code></pre>
  
  <p>In my interrupt handler I am converting the 4-bit nibble into a 16-bit value to send to the LCD via the parallel port and DMA transfer. I use a look up table in the interrupt handler to achieve this but when debugging this I'm going into the General_Exception_Handler and it's pointing toward a problem in the way I extract the 4-bit nibble and convert it to the 16-bit value:</p>
  
  <pre><code>for( i=0,j=0; i&lt;30; i++, lineX++ )
  {
      // not particularly elegant, perhaps look at a better way
      if     ((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x0) lineBuffer[j++] = (unsigned short int)RGBBlack;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x1) lineBuffer[j++] = (unsigned short int)RGBBlue;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x2) lineBuffer[j++] = (unsigned short int)RGBRed;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x3) lineBuffer[j++] = (unsigned short int)RGBGreen;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x4) lineBuffer[j++] = (unsigned short int)RGBCyan;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x6) lineBuffer[j++] = (unsigned short int)RGBYellow;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0x7) lineBuffer[j++] = (unsigned short int)RGBKaneGreen;
      else if((((GraphicsFrame[line][i]) &gt;&gt; 4) &amp; 0x0F) == 0xF) lineBuffer[j++] = (unsigned short int)RGBWhite;
  
      if     ((GraphicsFrame[line][i] &amp; 0x0F) == 0x0) lineBuffer[j++] = (unsigned short int)RGBBlack;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x1) lineBuffer[j++] = (unsigned short int)RGBBlue;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x2) lineBuffer[j++] = (unsigned short int)RGBRed;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x3) lineBuffer[j++] = (unsigned short int)RGBGreen;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x4) lineBuffer[j++] = (unsigned short int)RGBCyan;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x5) lineBuffer[j++] = (unsigned short int)RGBMagenta;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x6) lineBuffer[j++] = (unsigned short int)RGBYellow;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0x7) lineBuffer[j++] = (unsigned short int)RGBKaneGreen;
      else if((GraphicsFrame[line][i] &amp; 0x0F) == 0xF) lineBuffer[j++] = (unsigned short int)RGBWhite;
  }
  </code></pre>
  
  <p>I'm trying to set up the DMA to transfer 60 pixels at a time (60 x 16-bits) using another array which contains the 60 pixels:</p>
  
  <pre><code>unsigned short int lineBuffer[60];
  </code></pre>
  
  <p>Can anyone spot a problem with the way I'm extracting the nibble and converting it? There are no warnings or errors so nothing is jumping out at me!</p>
  
  <p>Any help appreciated, thanks </p>