Display Bitmap on LCD

Hello,

I am trying to draw ‘0’ and ‘1’ on my LCD display. I try to display these two bitmaps with adafruit library.

const  unsigned char N_1 [] PROGMEM = 
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
  };
const  unsigned char N_1 [] PROGMEM = 
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
  };
const  unsigned char* const N[] PROGMEM={N_0,N_1};

It works fine if I use tft.drawBitmap(120,120,N[0],19,24,GREEN,BLACK);

But it started to give me garbage when I use

for(int i=0;i<2;i++) 
tft.drawBitmap(120,120,N[i],19,24,GREEN,BLACK);

Don’t understand why the second one doesn’t work.
Thanks in advance.

You are drawing them on top of each other?

I think drawing on top should be fine. There is a fore & back color specified.

Does..

tft.drawBitmap(120,120,N[1],19,24,GREEN,BLACK);

Work ok? (outside loop)

-jim lee

Fair point - did not catch that;

Seems the width and height are off, aren't they?

jimLee: I think drawing on top should be fine. There is a fore & back color specified.

Does..

tft.drawBitmap(120,120,N[1],19,24,GREEN,BLACK);

Work ok? (outside loop)

-jim lee

Yes, it works if I use N[0] or N[1]. It even works if I use

   int i=0;
   tft.drawBitmap(120,120,N[i],19,24,GREEN,BLACK);
   delay(1000);
   i++;
   tft.drawBitmap(120,120,N[1],19,24,GREEN,BLACK);
   delay(1000);

But once I put it inside a loop, then everything goes wrong.

J-M-L: Fair point - did not catch that;

Seems the width and height are off, aren't they?

No, they aren't. The random dots seemed to be restricted in that 19*24 area.

Ok but your bitmap is larger than 19x24 so what is displayed is probably not what you designed but agree the behavior should be the same.

In your example when you split the loop manually you have N[1] in the second one, would that work with N[i ]?

How big is the rest of your program? What does your memory usage looks like?

Yes, it works with the following. I tried

   int i=0;
   tft.drawBitmap(120,120,N[i],19,24,GREEN,BLACK);
   delay(1000);
   i++;
   tft.drawBitmap(120,120,N[i],19,24,GREEN,BLACK);

delay(1000);

The memory usage: Sketch uses 14580 bytes (45%) of program storage space. Maximum is 32256 bytes. Global variables use 272 bytes (13%) of dynamic memory, leaving 1776 bytes for local variables.

Really not sure what happens.

OK so the only difference is the delay. what if you put a delay in the for loop?

Just tried this.

 for(int i=1;i<5;i++)
  {tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK);
 delay(500);
  }

Still giving me garbage. :o

Chuan:
Just tried this.

 for(int i=1;i<5;i++)

{tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK);
delay(500);
  }




Still giving me garbage. :o

Well you should not go beyond i=1… so that’s not surprising…what about this?

const  unsigned char N_1 [] PROGMEM = 
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00};

const  unsigned char N_1 [] PROGMEM = 
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00  };

const  unsigned char* const N[] PROGMEM={N_0,N_1};

// .... later after declaring everything right

for(int i=0;i<2;i++) {
   tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK); // W and H are stupid values here
   delay(2000);
}

can you write a small program without all the rest of your code, with just the basic TFT declaration, the bitmap definition and post the code with the for loop. also what’s the size of your display?

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET 10 // Can alternately just connect to Arduino's reset pin



#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const  unsigned char N_0 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X7F,0XFF,0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,
0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,
0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};
const  unsigned char N_1 [] PROGMEM = 
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
  };
const  unsigned char N_2 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X3F,0XFF,0XC0,0X7F,0XFF,0XE0,0X7F,0XFF,0XE0,0X7F,0XFF,0XE0,0X7C,0X03,0XE0,0X7C,
0X03,0XE0,0X7C,0X03,0XE0,0X7C,0X03,0XE0,0X00,0X07,0XE0,0X00,0X0F,0XC0,0X00,0X1F,
0X80,0X00,0X3F,0X00,0X00,0X7E,0X00,0X00,0XFC,0X00,0X01,0XF8,0X00,0X03,0XF0,0X00,
0X07,0XE0,0X00,0X0F,0XC0,0X00,0X1F,0X80,0X00,0X3F,0X00,0X00,0X7F,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,};
const  unsigned char N_3 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X7F,0XFF,0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0X00,0X07,0XE0,0X00,0X3F,0XE0,0X00,0XFF,
0X80,0X01,0XFC,0X00,0X00,0XFF,0X80,0X00,0X3F,0XE0,0X00,0X0F,0XE0,0X00,0X03,0XE0,
0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};
const  unsigned char N_4 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0X7E,0X00,0X00,0X7C,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF0,0X00,0X03,
0XE0,0X00,0X03,0XE0,0X00,0X07,0XC0,0X00,0X07,0XC7,0X80,0X0F,0X87,0X80,0X1F,0X07,
0X80,0X1F,0X07,0X80,0X3E,0X07,0X80,0X3E,0X07,0X80,0X7C,0X07,0X80,0XFC,0X07,0X80,
0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0X00,0X07,0X80,0X00,
0X07,0X80,0X00,0X07,0X80,0X00,0X07,0X80,};
const  unsigned char N_5 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XFF,0XFF,0XC0,0XFF,0XFF,
0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0X00,0X03,0XE0,0X00,0X03,0XE0,0X00,0X03,0XE0,
0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};
const  unsigned char N_6 [] PROGMEM =
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X7F,0XFF,0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XFF,0XFF,
0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,
0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};
const  unsigned char N_7 [] PROGMEM =  
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XC0,0XF8,0X07,0XC0,0X00,0X07,0XC0,0X00,0X0F,0X80,0X00,0X0F,0X80,0X00,0X1F,
0X00,0X00,0X1F,0X00,0X00,0X3E,0X00,0X00,0X3E,0X00,0X00,0X3C,0X00,0X00,0X7C,0X00,
0X00,0X7C,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF0,0X00,0X01,0XF0,0X00,0X03,
0XE0,0X00,0X03,0XE0,0X00,0X07,0XE0,0X00,};
const  unsigned char N_8 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X7F,0XFF,0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0X1F,0XE0,0X7F,0XFF,
0XC0,0X0F,0XFE,0X00,0X1F,0XFF,0X00,0X7F,0XBF,0XC0,0XFC,0X07,0XE0,0XF8,0X03,0XE0,
0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};
const  unsigned char N_9 [] PROGMEM = 
{ 0X00,0X01,0X13,0X00,0X18,0X00,
0X7F,0XFF,0XC0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0XF8,0X03,0XE0,0XF8,
0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,
0XE0,0XFF,0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XE0,0X00,0X03,0XE0,0X00,0X03,0XE0,
0X00,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XF8,0X03,0XE0,0XFF,0XFF,0XE0,0XFF,
0XFF,0XE0,0XFF,0XFF,0XE0,0X7F,0XFF,0XC0,};

const  unsigned char* const N[] PROGMEM={N_0,N_1,N_2,N_3,N_4,N_5,N_6,N_7,N_8,N_9};


int one();

void setup()
{
    Serial.begin(9600);
    tft.reset();
    uint16_t identifier = tft.readID();
    tft.begin(identifier);
    tft.fillScreen(BLACK);
}

void loop()
{

 for(int i=0;i<5;i++)
  
  tft.drawBitmap(120,120,N[i],19,24,GREEN,BLACK);
 
}

And I opened the bitmap in Windows Paint and it telled me it’s 1924.
I am using MCU friend 2.4’’ touch TFT screen,240
320, driver IC ILI9325 .

Thanks for you patience. :slight_smile:

J-M-L:
Well you should not go beyond i=1… so that’s not surprising…what about this?

const  unsigned char N_1 [] PROGMEM = 

{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00};

const  unsigned char N_1 PROGMEM =
{0X00,0X01,0X13,0X00,0X18,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X01,0XF8,0X00,0X03,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,
0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,
0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00,0X00,
0XF8,0X00,0X00,0XF8,0X00,0X00,0XF8,0X00  };

const  unsigned char* const N PROGMEM={N_0,N_1};

// … later after declaring everything right

for(int i=0;i<2;i++) {
  tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK); // W and H are stupid values here
  delay(2000);
}





can you write a small program without all the rest of your code, with just the basic TFT declaration, the bitmap definition and post the code with the for loop. also what's the size of your display?

Still giving me garbage. It seemed only to work if it were not included in a for loop.

Here’s what it looks like when the tft.drawBitmap wasn’t included in that for loop.

Really have no idea what was going on. Perhaps the most weird thing I have seen.

that's weird. can you do one change

replace the unsigned char with uint8_t

const [color=red]unsigned char[/color] N_0 [] PROGMEM = ... becomes const [color=green]uint8_t[/color] N_0 [] PROGMEM = ...

same for all bitmaps and

const [color=red]unsigned char[/color]* const N[] PROGMEM={ becomes const [color=green]uint8_t[/color]* const N[] PROGMEM={

this is just to ensure we fully match the signature in the library for the tft.drawBitmap call in case the compilers gets confused...

J-M-L: that's weird. can you do one change

replace the unsigned char with uint8_t

const [color=red]unsigned char[/color] N_0 [] PROGMEM = ... becomes const [color=green]uint8_t[/color] N_0 [] PROGMEM = ...

same for all bitmaps and

const [color=red]unsigned char[/color]* const N[] PROGMEM={ becomes const [color=green]uint8_t[/color]* const N[] PROGMEM={

this is just to ensure we fully match the signature in the library for the tft.drawBitmap call in case the compilers gets confused...

Just tried to replace them but still doesn't work.

Edit:

I found this on Stack Overflow and it works.

Instead of:

array[page]

You'll need to use:

(unsigned char*) pgm_read_word( &array[page] )

While not your problem the size of the bitmap is weird.

Your bitmap holds 78 bytes. a byte represents 8 pixels so you have 78x8=624 pixels Your width and Height are 19x24 = 456 pixels

Do you have a header in your format?

Chuan:
Just tried this.

 for(int i=1;i<5;i++)

{tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK);
delay(500);
  }




Still giving me garbage. :o

Still different delay. You need to be consistent to rule out the for loop.

 for(int i=0;i<2;i++)
  {tft.drawBitmap(120,144,N[i],19,24,GREEN,BLACK);
 delay(1000);
  }

I think you are right. I used a software to convert the pictures into C char array, not knowing what was happening in that process.

J-M-L: While not your problem the size of the bitmap is weird.

Your bitmap holds 78 bytes. a byte represents 8 pixels so you have 78x8=624 pixels Your width and Height are 19x24 = 456 pixels

Do you have a header in your format?

Also I noticed that when it works, the pattern are not exactly what I want, for example, the line at bottom of '0' is thinner than that of top.