STM8 Microcontrollers – the Final Chapters

Bit-Banging PCD8544 GLCD

Without I2C, SPI or other forms of communications, it is possible to interface graphical displays with microcontrollers. By bit-banging GPIO pins it is possible to integrate graphical displays like PCD8544. This display was the display of several early Nokia phones like the famous Nokia 5110. At present, these LCDs are sold as ready-made GLCD modules ready to be integrated.

5110

Though PCD8544 supports SPI communication method, here in this example bit-banging method is used. Thus, we can realize it being driven by software SPI instead of hardware-based SPI.

Hardware Connection

GLCD

Code Example

font.h

static const unsigned char font[][5] =
{
     {0x00, 0x00, 0x00, 0x00, 0x00} // 20
    ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
    ,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
    ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
    ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
    ,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
    ,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
    ,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
    ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
    ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
    ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
    ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
    ,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
    ,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
    ,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
    ,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
    ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
    ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
    ,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
    ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
    ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
    ,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
    ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
    ,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
    ,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
    ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
    ,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
    ,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
    ,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
    ,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
    ,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
    ,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
    ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
    ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
    ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
    ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
    ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
    ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
    ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
    ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
    ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
    ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
    ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
    ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
    ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
    ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
    ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
    ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
    ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
    ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
    ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
    ,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
    ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
    ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
    ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
    ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
    ,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
    ,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
    ,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
    ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
    ,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ?
    ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
    ,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
    ,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
    ,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
    ,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
    ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
    ,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
    ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
    ,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
    ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
    ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
    ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
    ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
    ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
    ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
    ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
    ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
    ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
    ,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
    ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
    ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
    ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
    ,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
    ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
    ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
    ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
    ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
    ,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
    ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
    ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
    ,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
    ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
    ,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
    ,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ?
    ,{0x78, 0x46, 0x41, 0x46, 0x78} // 7f ?
};

PCD8544.h

#include "STM8S.h"
#include "font.h"


#define BL_pin                          GPIO_PIN_4
#define RST_pin                         GPIO_PIN_7
#define CE_pin                          GPIO_PIN_0
#define DC_pin                          GPIO_PIN_1
#define MOSI_pin                        GPIO_PIN_6
#define SCK_pin                         GPIO_PIN_5

#define PCD8544_port_1                  GPIOC
#define PCD8544_port_2                  GPIOG

#define BL_OUT_LOW()                    GPIO_WriteLow(PCD8544_port_1, BL_pin)
#define BL_OUT_HIGH()                   GPIO_WriteHigh(PCD8544_port_1, BL_pin)
#define RST_OUT_LOW()                   GPIO_WriteLow(PCD8544_port_1, RST_pin)
#define RST_OUT_HIGH()                  GPIO_WriteHigh(PCD8544_port_1, RST_pin)
#define CE_OUT_LOW()                    GPIO_WriteLow(PCD8544_port_2, CE_pin)
#define CE_OUT_HIGH()                   GPIO_WriteHigh(PCD8544_port_2, CE_pin)
#define DC_OUT_LOW()                    GPIO_WriteLow(PCD8544_port_2, DC_pin)
#define DC_OUT_HIGH()                   GPIO_WriteHigh(PCD8544_port_2, DC_pin)
#define MOSI_OUT_LOW()                  GPIO_WriteLow(PCD8544_port_1,MOSI_pin)
#define MOSI_OUT_HIGH()                 GPIO_WriteHigh(PCD8544_port_1, MOSI_pin)
#define SCK_OUT_LOW()                   GPIO_WriteLow(PCD8544_port_1, SCK_pin)
#define SCK_OUT_HIGH()                  GPIO_WriteHigh(PCD8544_port_1, SCK_pin)

#define PCD8544_set_Y_addr              0x40
#define PCD8544_set_X_addr              0x80

#define PCD8544_set_temp                0x04
#define PCD8544_set_bias                0x10
#define PCD8544_set_VOP                 0x80

#define PCD8544_power_down              0x04
#define PCD8544_entry_mode              0x02
#define PCD8544_extended_instruction   0x01

#define PCD8544_display_blank           0x00
#define PCD8544_display_normal          0x04
#define PCD8544_display_all_on          0x01
#define PCD8544_display_inverted        0x05

#define PCD8544_function_set            0x20
#define PCD8544_display_control         0x08

#define CMD                              0
#define DAT                              1

#define X_max                            84
#define Y_max                            48
#define Rows                             6

#define BLACK                            0
#define WHITE                            1
#define PIXEL_XOR                       2

#define ON                               1
#define OFF                              0

#define NO                               0
#define YES                              1

#define buffer_size                     504


void setup_GLCD_GPIOs(void);
void PCD8544_write(unsigned char type, unsigned char value);
void PCD8544_reset(void);
void PCD8544_init(void);
void PCD8544_backlight_state(unsigned char value);
void PCD8544_set_contrast(unsigned char value);
void PCD8544_set_cursor(unsigned char x_pos, unsigned char y_pos);
void PCD8544_print_char(unsigned char ch, unsigned char colour);
void PCD8544_print_custom_char(unsigned char *map);
void PCD8544_fill(unsigned char bufr);
void PCD8544_clear_buffer(unsigned char colour);
void PCD8544_clear_screen(unsigned char colour);
void PCD8544_print_image(const unsigned char *bmp);
void PCD8544_print_string(unsigned char x_pos, unsigned char y_pos, unsigned char *ch, unsigned char colour);
void print_char(unsigned char x_pos, unsigned char y_pos, unsigned char ch, unsigned char colour);
void print_string(unsigned char x_pos, unsigned char y_pos, unsigned char *ch, unsigned char colour);
void print_chr(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char colour);
void print_int(unsigned char x_pos, unsigned char y_pos, signed long value, unsigned char colour);
void print_decimal(unsigned char x_pos, unsigned char y_pos, unsigned int value, unsigned char points, unsigned char colour);
void print_float(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points, unsigned char colour);
void Draw_Pixel(unsigned char x_pos, unsigned char y_pos, unsigned char colour);
void Draw_Line(signed int x1, signed int y1, signed int x2, signed int y2, unsigned char colour);
void Draw_Rectangle(signed int x1, signed int y1, signed int x2, signed int y2, unsigned char fill, unsigned char colour);
void Draw_Circle(signed int xc, signed int yc, signed int radius, unsigned char fill, unsigned char colour);

PCD8544.c

#include "PCD8544.h"


extern unsigned char PCD8544_buffer[X_max][Rows];


void setup_GLCD_GPIOs(void)
{
     GPIO_Init(PCD8544_port_1,
               ((GPIO_Pin_TypeDef)(BL_pin | RST_pin | MOSI_pin | SCK_pin)),
                GPIO_MODE_OUT_PP_HIGH_FAST);

     GPIO_Init(PCD8544_port_2,
               ((GPIO_Pin_TypeDef)(DC_pin | CE_pin)),
                GPIO_MODE_OUT_PP_HIGH_FAST);

     delay_ms(10);
}


void PCD8544_write(bool mode, unsigned char value)
{
    unsigned char s = 0x08;

    switch(mode)
    {
        case DAT:
        {
            DC_OUT_HIGH();
            break;
        }

        default:
        {
            DC_OUT_LOW();
            break;
        }
    }

    CE_OUT_LOW();

    while(s > 0)
    {
        SCK_OUT_LOW();

        if((value & 0x80) == 0)
        {
             MOSI_OUT_LOW();
        }
        else
        {
            MOSI_OUT_HIGH();
        }

        value <<= 1;
        SCK_OUT_HIGH();
        s--;
    };

    CE_OUT_HIGH();
}


void PCD8544_reset(void)
{
    RST_OUT_LOW();
    delay_us(100);
    RST_OUT_HIGH();
}


void PCD8544_init(void)
{
    setup_GLCD_GPIOs();
    PCD8544_reset();
    PCD8544_write(CMD, (PCD8544_extended_instruction | PCD8544_function_set));
    PCD8544_write(CMD, (PCD8544_set_bias | 0x02));
    PCD8544_set_contrast(0x39);
    PCD8544_write(CMD, PCD8544_set_temp);
    PCD8544_write(CMD, (PCD8544_display_normal | PCD8544_display_control));
    PCD8544_write(CMD, PCD8544_function_set);
    PCD8544_write(CMD, PCD8544_display_all_on);
    PCD8544_write(CMD, PCD8544_display_normal);
    PCD8544_clear_buffer(OFF);
}


void PCD8544_backlight_state(bool mode)
{
    switch(mode)
    {
        case ON:
        {
            BL_OUT_LOW();
            break;
        }

        default:
        {
            BL_OUT_HIGH();
            break;
        }
    }
}


void PCD8544_set_contrast(unsigned char value)
{
    PCD8544_write(CMD, (PCD8544_extended_instruction | PCD8544_function_set));
    PCD8544_write(CMD, (PCD8544_set_VOP | (value & 0x7F)));
    PCD8544_write(CMD, PCD8544_function_set);
}


void PCD8544_set_cursor(unsigned char x_pos, unsigned char y_pos)
{
    PCD8544_write(CMD, (PCD8544_set_X_addr | x_pos));
    PCD8544_write(CMD, (PCD8544_set_Y_addr | y_pos));
}


void PCD8544_print_char(unsigned char ch, unsigned char colour)
{
    unsigned char s = 0;
    unsigned char chr = 0;

    for(s = 0; s <= 4; s++)
    {
       chr = font[(ch - 0x20)][s];
       if(colour == BLACK)
       {
           chr = ~chr;
       }
       PCD8544_write(DAT, chr);
    }
}


void PCD8544_print_custom_char(unsigned char *map)
{
    unsigned char s = 0;

    for(s = 0; s <= 4; s++)
    {
        PCD8544_write(DAT, *map++);
    }
}


void PCD8544_fill(unsigned char bufr)
{
    unsigned int s = 0;

    PCD8544_set_cursor(0, 0);

    for(s = 0; s < buffer_size; s++)
    {
        PCD8544_write(DAT, bufr);
    }
}


void PCD8544_clear_buffer(unsigned char colour)
{
    unsigned char x_pos = 0;
    unsigned char y_pos = 0;

    for(x_pos; x_pos < X_max; x_pos++)
    {
        for(y_pos; y_pos < Rows; y_pos++)
        {
            PCD8544_buffer[x_pos][y_pos] = colour;
        }
    }
}


void PCD8544_clear_screen(unsigned char colour)
{
    unsigned char x_pos = 0;
    unsigned char y_pos = 0;

    for(y_pos = 0; y_pos < Rows; y_pos++)
    {
        for(x_pos = 0; x_pos < X_max; x_pos++)
        {
            PCD8544_print_string(x_pos, y_pos, " ", colour);
        }
    }
}


void PCD8544_print_image(const unsigned char *bmp)
{
    unsigned int s = 0;

    PCD8544_set_cursor(0, 0);

    for(s = 0; s < buffer_size; s++)
    {
        PCD8544_write(DAT, bmp[s]);
    }
}


void PCD8544_print_string(unsigned char x_pos, unsigned char y_pos, unsigned char *ch, unsigned char colour)
{
    PCD8544_set_cursor(x_pos, y_pos);

    do
    {
       PCD8544_print_char(*ch++, colour);
    }while((*ch >= 0x20) && (*ch <= 0x7F) && (*ch != '\0'));
}


void print_chr(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char colour)
{
    unsigned char ch = 0x00;

    if(value < 0)
    {
        PCD8544_set_cursor(x_pos, y_pos);
        PCD8544_print_char(0x2D, colour);
        value = -value;
    }
    else
    {
        PCD8544_set_cursor(x_pos, y_pos);
        PCD8544_print_char(0x20, colour);
    }

     if((value > 99) && (value <= 999))
     {
         ch = (value / 100);
         PCD8544_set_cursor((x_pos + 6), y_pos);
         PCD8544_print_char((48 + ch), colour);

         ch = ((value % 100) / 10);
         PCD8544_set_cursor((x_pos + 12), y_pos);
         PCD8544_print_char((48 + ch), colour);

         ch = (value % 10);
         PCD8544_set_cursor((x_pos + 18), y_pos);
         PCD8544_print_char((48 + ch), colour);
     }
     else if((value > 9) && (value <= 99))
     {
         ch = ((value % 100) / 10);
         PCD8544_set_cursor((x_pos + 6), y_pos);
         PCD8544_print_char((48 + ch), colour);

         ch = (value % 10);
         PCD8544_set_cursor((x_pos + 12), y_pos);
         PCD8544_print_char((48 + ch), colour);

         PCD8544_set_cursor((x_pos + 18), y_pos);
         PCD8544_print_char(0x20, colour);
     }
     else if((value >= 0) && (value <= 9))
     {
         ch = (value % 10);
         PCD8544_set_cursor((x_pos + 6), y_pos);
         PCD8544_print_char((48 + ch), colour);

         PCD8544_set_cursor((x_pos + 12), y_pos);
         PCD8544_print_char(0x20, colour);

         PCD8544_set_cursor((x_pos + 18), y_pos);
         PCD8544_print_char(0x20, colour);
     }
}


void print_int(unsigned char x_pos, unsigned char y_pos, signed long value, unsigned char colour)
{
    unsigned char ch = 0x00;

    if(value < 0)
    {
        PCD8544_set_cursor(x_pos, y_pos);
        PCD8544_print_char(0x2D, colour);
        value = -value;
    }
    else
    {
        PCD8544_set_cursor(x_pos, y_pos);
        PCD8544_print_char(0x20, colour);
    }

    if(value > 9999)
    {
        ch = (value / 10000);
        PCD8544_set_cursor((x_pos + 6), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 10000)/ 1000);
        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 1000) / 100);
        PCD8544_set_cursor((x_pos + 18), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 100) / 10);
        PCD8544_set_cursor((x_pos + 24), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = (value % 10);
        PCD8544_set_cursor((x_pos + 30), y_pos);
        PCD8544_print_char((48 + ch), colour);
    }

    else if((value > 999) && (value <= 9999))
    {
        ch = ((value % 10000)/ 1000);
        PCD8544_set_cursor((x_pos + 6), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 1000) / 100);
        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 100) / 10);
        PCD8544_set_cursor((x_pos + 18), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = (value % 10);
        PCD8544_set_cursor((x_pos + 24), y_pos);
        PCD8544_print_char((48 + ch), colour);

        PCD8544_set_cursor((x_pos + 30), y_pos);
        PCD8544_print_char(0x20, colour);
    }
    else if((value > 99) && (value <= 999))
    {
        ch = ((value % 1000) / 100);
        PCD8544_set_cursor((x_pos + 6), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = ((value % 100) / 10);
        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = (value % 10);
        PCD8544_set_cursor((x_pos + 18), y_pos);
        PCD8544_print_char((48 + ch), colour);

        PCD8544_set_cursor((x_pos + 24), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 30), y_pos);
        PCD8544_print_char(0x20, colour);
    }
    else if((value > 9) && (value <= 99))
    {
        ch = ((value % 100) / 10);
        PCD8544_set_cursor((x_pos + 6), y_pos);
        PCD8544_print_char((48 + ch), colour);

        ch = (value % 10);
        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char((48 + ch), colour);

        PCD8544_set_cursor((x_pos + 18), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 24), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 30), y_pos);
        PCD8544_print_char(0x20, colour);
    }
    else
    {
        ch = (value % 10);
        PCD8544_set_cursor((x_pos + 6), y_pos);
        PCD8544_print_char((48 + ch), colour);

        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 18), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 24), y_pos);
        PCD8544_print_char(0x20, colour);

        PCD8544_set_cursor((x_pos + 30), y_pos);
        PCD8544_print_char(0x20, colour);
    }
}


void print_decimal(unsigned char x_pos, unsigned char y_pos, unsigned int value, unsigned char points, unsigned char colour)
{
    unsigned char ch = 0x00;

    PCD8544_set_cursor(x_pos, y_pos);
    PCD8544_print_char(0x2E, colour);

    ch = (value / 1000);
    PCD8544_set_cursor((x_pos + 6), y_pos);
    PCD8544_print_char((48 + ch), colour);

    if(points > 1)
    {
        ch = ((value % 1000) / 100);
        PCD8544_set_cursor((x_pos + 12), y_pos);
        PCD8544_print_char((48 + ch), colour);


        if(points > 2)
        {
            ch = ((value % 100) / 10);
            PCD8544_set_cursor((x_pos + 18), y_pos);
            PCD8544_print_char((48 + ch), colour);

            if(points > 3)
            {
                ch = (value % 10);
                PCD8544_set_cursor((x_pos + 24), y_pos);
                PCD8544_print_char((48 + ch), colour);;
            }
        }
    }
}


void print_float(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points, unsigned char colour)
{
    signed long tmp = 0x00;

    tmp = ((signed long)value);
    print_int(x_pos, y_pos, tmp, colour);
    tmp = ((value - tmp) * 10000);

    if(tmp < 0)
    {
       tmp = -tmp;
    }

    if((value >= 10000) && (value < 100000))
    {
        print_decimal((x_pos + 36), y_pos, tmp, points, colour);
    }
    else if((value >= 1000) && (value < 10000))
    {
        print_decimal((x_pos + 30), y_pos, tmp, points, colour);
    }
    else if((value >= 100) && (value < 1000))
    {
        print_decimal((x_pos + 24), y_pos, tmp, points, colour);
    }
    else if((value >= 10) && (value < 100))
    {
        print_decimal((x_pos + 18), y_pos, tmp, points, colour);
    }
    else if(value < 10)
    {
        print_decimal((x_pos + 12), y_pos, tmp, points, colour);
        if(value < 0)
        {
            PCD8544_set_cursor(x_pos, y_pos);
            PCD8544_print_char(0x2D, colour);
        }
        else
        {
            PCD8544_set_cursor(x_pos, y_pos);
            PCD8544_print_char(0x20, colour);
        }
    }
}


void Draw_Pixel(unsigned char x_pos, unsigned char y_pos, unsigned char colour)
{
    unsigned char row = 0;
    unsigned char value = 0;

    if((x_pos < 0) || (x_pos >= X_max) || (y_pos < 0) || (y_pos >= Y_max))
    {
        return;
    }

    row = (y_pos >> 3);

    value = PCD8544_buffer[x_pos][row];

    if(colour == BLACK)
    {
        value |= (1 << (y_pos % 8));
    }
    else if(colour == WHITE)
    {
        value &= (~(1 << (y_pos % 8)));
    }
    else if(colour == PIXEL_XOR)
    {
        value ^= (1 << (y_pos % 8));
    }

    PCD8544_buffer[x_pos][row] = value;

    PCD8544_set_cursor(x_pos, row);
    PCD8544_write(DAT, value);
}


void Draw_Line(signed int x1, signed int y1, signed int x2, signed int y2, unsigned char colour)
{
    signed int dx = 0x0000;
    signed int dy = 0x0000;
    signed int stepx = 0x0000;
    signed int stepy = 0x0000;
    signed int fraction = 0x0000;

    dy = (y2 - y1);
    dx = (x2 - x1);

    if (dy < 0)
    {
        dy = -dy;
        stepy = -1;
    }
    else
    {
        stepy = 1;
    }

    if (dx < 0)
    {
        dx = -dx;
        stepx = -1;
    }
    else
    {
        stepx = 1;
    }

    dx <<= 0x01;
    dy <<= 0x01;

    Draw_Pixel(x1, y1, colour);

    if (dx > dy)
    {
        fraction = (dy - (dx >> 1));
        while (x1 != x2)
        {
            if (fraction >= 0)
            {
                y1 += stepy;
                fraction -= dx;
            }
            x1 += stepx;
            fraction += dy;

            Draw_Pixel(x1, y1, colour);
        }
    }
    else
    {
        fraction = (dx - (dy >> 1));

        while (y1 != y2)
        {
            if (fraction >= 0)
            {
                x1 += stepx;
                fraction -= dy;
            }
            y1 += stepy;
            fraction += dx;
            Draw_Pixel(x1, y1, colour);
        }
    }
}


void Draw_Rectangle(signed int x1, signed int y1, signed int x2, signed int y2, unsigned char fill, unsigned char colour)
{
     unsigned char i = 0x00;
     unsigned char xmin = 0x00;
     unsigned char xmax = 0x00;
     unsigned char ymin = 0x00;
     unsigned char ymax = 0x00;

     if(fill != NO)
     {
        if(x1 < x2)
        {
           xmin = x1;
           xmax = x2;
        }
        else
        {
           xmin = x2;
           xmax = x1;
        }

        if(y1 < y2)
        {
           ymin = y1;
           ymax = y2;
        }
        else
        {
           ymin = y2;
           ymax = y1;
        }

        for(; xmin <= xmax; ++xmin)
        {
             for(i = ymin; i <= ymax; ++i)
             {
                 Draw_Pixel(xmin, i, colour);
             }
         }
     }

     else
     {
        Draw_Line(x1, y1, x2, y1, colour);
        Draw_Line(x1, y2, x2, y2, colour);
        Draw_Line(x1, y1, x1, y2, colour);
        Draw_Line(x2, y1, x2, y2, colour);
     }
}


void Draw_Circle(signed int xc, signed int yc, signed int radius, unsigned char fill, unsigned char colour)
{
    signed int a = 0x0000;
    signed int b = 0x0000;
    signed int p = 0x0000;

    b = radius;
    p = (1 - b);

    do
    {
        if(fill != NO)
        {
            Draw_Line((xc - a), (yc + b), (xc + a), (yc + b), colour);
            Draw_Line((xc - a), (yc - b), (xc + a), (yc - b), colour);
            Draw_Line((xc - b), (yc + a), (xc + b), (yc + a), colour);
            Draw_Line((xc - b), (yc - a), (xc + b), (yc - a), colour);
        }
        else
        {
            Draw_Pixel((xc + a), (yc + b), colour);
            Draw_Pixel((xc + b), (yc + a), colour);
            Draw_Pixel((xc - a), (yc + b), colour);
            Draw_Pixel((xc - b), (yc + a), colour);
            Draw_Pixel((xc + b), (yc - a), colour);
            Draw_Pixel((xc + a), (yc - b), colour);
            Draw_Pixel((xc - a), (yc - b), colour);
            Draw_Pixel((xc - b), (yc - a), colour);
        }

        if(p < 0)
        {
            p += (0x03 + (0x02 * a++));
        }
        else
        {
            p += (0x05 + (0x02 * ((a++) - (b--))));
        }
    }while(a <= b);
}

main.c

#include "STM8S.h"
#include "PCD8544.h"


unsigned char PCD8544_buffer[X_max][Rows];


void setup_clock(void);
void setup_GPIOs(void);


void main(void)
{
     unsigned char g = 0;
     const unsigned char txt1[11] = {"MicroArena"};
     const unsigned char txt2[11] = {"SSHAHRYIAR"};

     signed char c = -9;
     signed int i = -66;
     float f = -0.04;

     setup_clock();
     setup_GPIOs();
     PCD8544_init();

     PCD8544_backlight_state(ON);
     delay_ms(1000);
     PCD8544_backlight_state(OFF);
     delay_ms(1000);

     PCD8544_clear_screen(WHITE);

     PCD8544_backlight_state(ON);
     Draw_Rectangle(2, 2, 81, 45, OFF, BLACK);
     delay_ms(200);

     Draw_Circle(6, 6, 2, ON, BLACK);
     delay_ms(200);
     Draw_Circle(77, 6, 2, ON, BLACK);
     delay_ms(200);
     Draw_Circle(77, 41, 2, ON, BLACK);
     delay_ms(200);
     Draw_Circle(6, 41, 2, ON, BLACK);
     delay_ms(200);

     Draw_Line(2, 11, 10, 11, BLACK);
     Draw_Line(73, 11, 81, 11, BLACK);
     delay_ms(200);
     Draw_Line(2, 36, 10, 36, BLACK);
     Draw_Line(73, 36, 81, 36, BLACK);
     delay_ms(200);
     Draw_Line(11, 45, 11, 2, BLACK);
     Draw_Line(72, 45, 72, 2, BLACK);
     delay_ms(200);

     PCD8544_backlight_state(OFF);
     delay_ms(400);

     PCD8544_backlight_state(ON);

     for(g = 0; g <= 9; g++)
     {
         PCD8544_set_cursor((18 + (g * 5)), 2);
         PCD8544_print_char(txt1[g], WHITE);
         delay_ms(90);
     }

     for(g = 0; g <= 9; g++)
     {
         PCD8544_set_cursor((18 + (g * 5)), 3);
         PCD8544_print_char(txt2[g], WHITE);
         delay_ms(90);
     }
     delay_ms(4000);

     PCD8544_clear_screen(WHITE);

     PCD8544_print_string(1, 2, "CHR:", WHITE);
     PCD8544_print_string(1, 3, "INT:", WHITE);
     PCD8544_print_string(1, 4, "FLT:", WHITE);

     while(1)
     {
         print_chr(26, 2, c, WHITE);
         print_int(26, 3, i, WHITE);
         print_float(26, 4, f, 2, WHITE);
         c++;
         i++;
         f += 0.01;
         delay_ms(400);
     };
}


void setup_clock(void)
{
       CLK_DeInit();

       CLK_HSECmd(ENABLE);
       while(CLK_GetFlagStatus(CLK_FLAG_HSERDY) == FALSE);

       CLK_LSICmd(DISABLE);

       CLK_HSICmd(ENABLE);
       while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

       CLK_ClockSwitchCmd(ENABLE);
       CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
       CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

       CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE,
       DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);

       CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART2, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER3, DISABLE);
       CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}


void setup_GPIOs(void)
{
       GPIO_DeInit(PCD8544_port_1);
       GPIO_DeInit(PCD8544_port_2);
}

Explanation

This display shares the same function demonstrated in the OLED display and so I am not going to repeat their uses again.

Demo

GLCD

Pages: 1 2 3 4 5 6 7 8 9 10

Continue Reading ...

Related Posts

78 comments

  • Shawon I’m sorry to bother you, but I can’t run ds18b20 with this code. Lcd shows meaningless character with this clock cpu you were configured, I configured CPUDIV1 and it worked but it only showed “4095.9” and it is not changing

    can you please help me ??
    please send you debug files AT iampruthvi63543@gmail.com

  • hello brother i run your code on stm8s00f0p3. I observed that
    LCD shows meaningless character. it only showed “4095.9” … can u help me? What more I can do to fix it??

    if you don’t mind can you ping me at iampruthvi63543@gmail.com

  • Hey Shawon

    May i know which file that contain this functions :


    void clock_setup(void);
    void GPIO_setup(void);
    void show_value(unsigned char value);

    I didn’t see any of these functions in SPL

    And this functions make an error in my Linker

    Thanks in advance.

  • Hi can you make a tutorial on interfacing mpu6050 with stm8s003f3. The problem I am facing is getting the gyro value only one time when the device is powered on then then nothing received in uart. I hope your tutorial will help me a lot.

  • Hi Shawon
    I have tried multiple times of interfacing gyroscope MPU6050 with stm8s003f3.

    void MPU6050_write(unsigned char cmd)
    {
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(MPU6050,I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(cmd);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTOP(ENABLE);
    }
    the function hangs at while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    i don’t know why this happen. The address of MPU6050 is 0xD0. Please reply me your suggestion.

  • Hi Shawon, thanks a lot for these great tutorial series.

    I’m trying to use the touch library for the STM8S003F3P6. I’m using your code and just editing the controller and the timers.
    Since this one doesn’t have the TIM3, so, I’m going to use the TIM2 instead.
    But after a second of running , the program freezes and the LED doesn’t blink and turns off. I even tried to just keep it on, not blinking, but it still turns off and stays like that forever.
    LED blinks again when commenting the TSL_Action() function in the while(1), but this way, the keys won’t work!
    There is no error or warning during compile!

    Do you have any idea about this?
    Thanks again for helping.

    • Hi again.

      Found out. For timer 2 we must use this:

      #define TIMACQ (TIM2)
      #define TIMACQ_CNTR_ADD (0x530C)

      The TIMACQ_CNTR_ADD’s value should be changed.

      Also, we should make sure some glass material stuff is on the pad, otherwise it won’t work or may freezes.

    • I have this problem too ,its because of TSL_SetStructPointer(); in TSL_Action() , can anyone help us???

  • Hello, I need lcd.h file, please share it and email me. thanks

  • void OLED_write(unsigned char value, unsigned char control_byte)
    {
    while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(SSD1306_I2C_Address, I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(control_byte);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
    I2C_SendData(value);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTOP(ENABLE);
    }

    It gets stuck at “while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));” this line for me, its not stepping beyond this point. Do you think this might be an issue of uC not able to detect the slave device or address.

  • This code is great. but how can i change font size.
    please help me.

  • Hi,what can i write instead of delay_cycle(1).ds18b20 is not functioning in iar,also delay_us& ms are not exact that i checked with logic analyzer.please help!thanks

  • Morteza Jamshidi

    Can you email me the source of the project and its files (One Wire (OW) – DS18B20) ? thank you. my Email address: mortezajamshidi9898@gmail.com

  • Morteza Jamshidi

    Can you email me the source of the project and its files? thank you. my Email address: mortezajamshidi9898@gmail.com

  • Hello i used your code with the IAR compiler but i had problems with delay_cycle, what house to use? I use lcd character without using I2C and HSIDIV = 8 and CPUDIV = 1

  • Hi if you can put sensor SHT10 with STM8s code.
    tnx

  • Hi if you can put sensor SHT10 with STM8s code
    tnx

  • Hi shwon, I’ve been trying to run oled dosplay but I have a problem . The program get stucks in “OLED_init()” function. I think it’s due to “OLED_write”function. I think it get stucks in one of those while loops in this function. Do u have any suggestion? How can I fix this problem?

    • Is it a SSD1306/SSD1309-based OLED display?

      • Yes it is,, just it’s a bit different with your display. I’ve tried these address bytes: “0x78 , 0x3C, 0x3D” they didn’t work… the scl and sda pins were pulled up with 4.7k resistor. I don’t know what should I do??

        • Is it a red PCB OLED? I asked so because some I2C-based OLED displays don’t work properly…. Many are fake one…. I had one such display and if you keep powering on and off our MCU + such OLED board several times, it will work….

  • I am trying to use i2c LCD but I am stuck in one place.

    The code is stuck in an endless loop in below function
    ////////////////
    void PCF8574_write(unsigned char data_byte)
    {
    I2C_GenerateSTART(ENABLE);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

    I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_TX);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    I2C_SendData(data_byte);
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTOP(ENABLE);
    }
    ////////////////

    the code is stuck in below line
    while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

    PCF8574_address is 0x4E

  • Hi sir, I use stm8s003f3p6, I want to run oled display, but I have an issue about stack memory. I’ve switched it to long stack memory but it doesn’t change.what should I do? I think this type of stm8s has’nt got as amount as stack memory I need.

    • Switch to chips of higher memory capacities…. This chip can’t be used to drive an OLED screen….

      • Hi shawon; I want to run OLED display but I have an error as follow: “bass size overflow(768)” I try with all type of stm8s microes but it’s not changed… I switched memory model to long stack memory. What should I do?

    • Your comment is awaiting moderation.

      I am trying to use i2c LCD but I am stuck in one place.

      The code is stuck in an endless loop in below function
      ////////////////
      void PCF8574_write(unsigned char data_byte)
      {
      I2C_GenerateSTART(ENABLE);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

      I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_TX);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

      I2C_SendData(data_byte);
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));

      I2C_GenerateSTOP(ENABLE);
      }
      ////////////////

      the code is stuck in below line
      while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

      PCF8574_address is 0x4E

      • What about pull-ups on SCL and SDA lines? Are you sure that your PCF8574’s I2C address is 0x4E?

        • I am trying without I2C module but still, nothing is showing on display.

        • I have only followed example code
          In tutorial it is not showing for pull up SDA and sck.
          .
          The address of is 0x27 I have tried with this also.

          I have also tried without i2c but that’s code is also not working .

          • Hmm first thing I guess is either the LCD is broken or you are trying with wrong I2C address… Be sure of the chip embedded in the I2C-LCD module…. Some come shipped with PCF8574 while some are shipped with PCF8574A…. These have different I2C addresses…. Secondly, in the tutorial pull-up is shown in the I2C-LCD module’s schematic and by the nature of I2C communication pull-ups are must…. It should be there no matter if specified or not….

        • Hi,

          I have pulled up SDA and SCL line by 10K and I have checked i2c address 0x27 is correct for 16×2 LCD.

          My LCD is not broken.

          I have checked the same LCD on ARDUINO same address it is working fine but in STM8s it is not showing anything.

          • Are you running the STM8 with 3.3V? If so then the LCD won’t work with 3.3V power…. Either supply the LCD with 5V or use a 3.3V compatible LCD….

          • Hi Shawon Shahryiar,

            Thanks for your replay.

            I am using 5VDC for LCD 16×2 from Stm8s board itself. I have tried with 2 different LCD both not working.
            My stm8s board is working on 3.3vdc only.

            I have debugged the code as per my knowledge the I2C->SR1(shift register) is not updating. it is showing 0 but it should be 1.

            The address is correct 0x3F.

            But my question is why is LCD with or without I2C LCD was not showing any data.
            I know that for I2C I have to use PULLUP both line. But if we use without i2c we don’t want PULL UP but here also it is not showing anything on Display.

            Please help me with this thing.

          • As of all the previous conversation and this one, I have no idea why is it not working…. specially I don’t understand why I2C->SR1 is not properly working…. I would suggest that you slow down your system clock and update the code according to system clock setting…. Most LCDs don’t work when their data update frequency is above 250kHz….

        • I think there is no Address issue. some I2C code issue is here

        • The address of PCF8574A is 0x3F. I tried it also with pull up resistance 10K but no result.

        • Hello Shawon Shahryiar,

          Please send me your mail id for this I2C problem discussion.

  • Hey; thanks a lot for your prefect article, could you send me please the delay libraries I haven’t got the right version of them, I have an error about “delay_cycle()”.

    • STM8S_delay.h


      #include "stm8s.h"

      #define F_CPU 16000000UL
      #define dly_const (F_CPU / 16000000.0F)

      void delay_us(unsigned int value);
      void delay_ms(unsigned int value);

      STM8S_delay.c


      #include "stm8s_delay.h"

      void delay_us(unsigned int value)
      {
      register unsigned int loops = (dly_const * value) ;

      while(loops)
      {
      _asm ("nop");
      loops--;
      };
      }

      void delay_ms(unsigned int value)
      {
      while(value)
      {
      delay_us(1000);
      value--;
      };
      }

      I didn’t use delay cycle in my library…. Are you using some other compiler other than Cosmic + STVD?

      • Look up at “one_wire.c” you used “delay_cycle(1)” three times. When I compile it I get this error: “missing prototype” .I use cosmic + stvd such as you.

      • Shawon I’m sorry to bother you, but I can’t run ds18b20 with this code. Lcd shows meaningless character with this clock cpu you were configured, I configured CPUDIV4 and it worked but it only showed “4095.9” I’m sure about hardware … can u help me? What more I can do to fix it??

        • Have you use pulled up the data pin? Did you try with my code? With my code does it work or not?

          • I’ve just coppied your code , at first complier didn’t find delay_cycle funcion then I replaced it with a while loop such as you said, after that I had to reduce the cpu clock cause LCD didn’t working properly(according to you most LCDs have a maximum working frequency of 250 khz.) at last I gave the wrong temprature from DS. I’ve pulled up the data line with 10 k res.

          • I didn’t mean that you copy my code and retry…. I meant that you check with my hardware connections and my finizalied debugger file “ow_ds18b20.s19” from the debug folder…. If there is still any issue then it is likely a hardware issue or else you are doing something wrong….

  • Really outstanding library! Thanks for helping out. I am using the STM8AF5288 so made some changes and worked like a charm. but the issue i am facing right now is the font size. is there any available font library that could be replaced to get bigger fonts? and If possible can you explain how the font is being created on the ssd1306, I mean the way the pixels are being drawn using the hex code (in the font.h file).
    Thanks in advance.

  • Really outstanding library! Thanks for helping out. I am using the STM8AF5288 so made some changes and worked like a charm. but the issue i am facing right now is the font size. is there any available font library that could be replaced to get bigger fonts ? and If possible can you explain how the font is being created on the ssd1306, I mean the way the pixels are being drawn using the hex code (in the font.h file).
    Thanks in advance.

    • There are several OLED libraries available in the internet with big fonts but I’m happy with this font size to make maximum use of the display…. Thus I didn’t go for development of libraries with big fonts….

      • thanks for the quick reply, But the thing is that i am facing a issue with that of the draw bitmap function.
        i.e.
        void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)

        {

        unsigned int s = 0x0000;

        unsigned char x_pos = 0x00;

        unsigned char y_pos = 0x00;

        for(y_pos = yb; y_pos <= ye; y_pos++)

        {

        OLED_gotoxy(xb, y_pos);

        for(x_pos = xb; x_pos < xe; x_pos++)

        {

        OLED_write(bmp_img[s++], DAT);

        }

        }

        }

        and i am calling it by the following way:
        OLED_draw_bitmap(0,0,128,64,(unsigned char *)&image_data_LesInv);

        and my bitmap file goes like this:
        static const uint8_t image_data_LesInv[128][64] = {
        // 'untitled', 128x64px
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0x7f, 0x3f, 0x3f, 0x3f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x01, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xf8, 0xf8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7f, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xe0, 0x00, 0x00, 0x00, 0x01, 0x07,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x0f, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x80, 0xe0,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0x80, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f,
        0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xfe, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8,
        0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
        };

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        but the issue is that my image does gets generated but it gets back to blank screen within a 500ms or so.
        can you please help with the same.

        • void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)
          {
          unsigned int s = 0x0000;
          unsigned char x_pos = 0x00;
          unsigned char y_pos = 0x00;

          for(y_pos = yb; y_pos <= ye; y_pos++)
          {
          OLED_gotoxy(xb, y_pos);
          for(x_pos = xb; x_pos < xe; x_pos++)
          {
          OLED_write(bmp_img[s++], DAT);
          }
          }
          }

          This was the original code but yours seems to be modified in the for loops…. Please recheck unless it is intentional….

          • both the loops are the same. I did not make any changes in the original code but the bitmap image seems to get printed on the screen but it gets instantly blank within 20ms.
            can you help me for the same.
            Thanks in advance.

          • Could you send me the whole code? I think there is an overflow somewhere….

  • Hello,
    How can i change the font size so i can support for example [92][12] ? Saw some libraries on the net but i am not sure if they are compatible with this code… or am i doing something wrong ? I tried manipulating the current one with some cycles within ” OLED_print_char ” also chaning 0x06 to 0x0C with a font library also tried creating fonts but i did not get the expected result. Can you give me some advice please ?
    Thank you for your time!

    • The coordinates of the OLED displays are mapped as multiples of 8-bits or 8 dots in both x and y directions. So just by changing x-coordinate values won’t result in larger fonts…. You must also take care of the y-coordinate too…. Take the example of the bitmap function:

      void OLED_draw_bitmap(unsigned char xb, unsigned char yb, unsigned char xe, unsigned char ye, unsigned char *bmp_img)
      {
      unsigned int s = 0x0000;
      unsigned char x_pos = 0x00;
      unsigned char y_pos = 0x00;

      for(y_pos = yb; y_pos <= ye; y_pos++)
      {
      OLED_gotoxy(xb, y_pos);
      for(x_pos = xb; x_pos < xe; x_pos++)
      {
      OLED_write(bmp_img[s], DAT);
      s++;
      }
      }
      }

      It takes care of the y-coordinate part once the x-coordinate points are filled up….

  • Hi great tutorial!
    Can you post code for interfacing external eeprom using i2c for stm8s003f3??

  • Pingback: .NET i jiné ...

  • Pingback: STM8 Microcontrollers – gStore

  • Outstanding bro. Carry on. Wish your good luck.

Leave a Reply to Morteza Jamshidi Cancel reply

Your email address will not be published. Required fields are marked *