Exploring STC 8051 Microcontrollers – Coding

Interfacing 2×16 LCD

After having both clock system and GPIO mastered, the next important thing to do is to drive an ordinary alphanumerical LCD or simply text LCD. This is very important as text LCDs are great tools for quickly and visually displaying information.

Code

LCD.h

 #define LCD_GPIO_init()                    do{P35_push_pull_mode; \
                                              P36_push_pull_mode; \
                                              P37_push_pull_mode; \
                                              P04_push_pull_mode; \
                                              P05_push_pull_mode; \
                                              P06_push_pull_mode; \
                                              P07_push_pull_mode; \
                                             }while(0)
 
#define LCD_RS_HIGH                        P35_high
#define LCD_RS_LOW                         P35_low
 
#define LCD_RW_HIGH                        P36_high
#define LCD_RW_LOW                         P36_low
 
#define LCD_EN_HIGH                        P37_high
#define LCD_EN_LOW                         P37_low
 
#define LCD_DB4_HIGH                       P04_high
#define LCD_DB4_LOW                        P04_low
 
#define LCD_DB5_HIGH                       P05_high
#define LCD_DB5_LOW                        P05_low
 
#define LCD_DB6_HIGH                       P06_high
#define LCD_DB6_LOW                        P06_low
 
#define LCD_DB7_HIGH                       P07_high
#define LCD_DB7_LOW                        P07_low
 
#define clear_display                      0x01
#define goto_home                          0x02
 
#define cursor_direction_inc               (0x04 | 0x02)
#define cursor_direction_dec               (0x04 | 0x00)
#define display_shift                      (0x04 | 0x01)
#define display_no_shift                   (0x04 | 0x00)
 
#define display_on                         (0x08 | 0x04)
#define display_off                        (0x08 | 0x02)
#define cursor_on                          (0x08 | 0x02)
#define cursor_off                         (0x08 | 0x00)
#define blink_on                           (0x08 | 0x01)
#define blink_off                          (0x08 | 0x00)
 
#define _8_pin_interface                   (0x20 | 0x10)
#define _4_pin_interface                   (0x20 | 0x00)
#define _2_row_display                     (0x20 | 0x08)
#define _1_row_display                     (0x20 | 0x00)
#define _5x10_dots                         (0x20 | 0x40)
#define _5x7_dots                          (0x20 | 0x00)
 
#define DAT                                1
#define CMD                                0
 
void LCD_init(void);
void LCD_send(unsigned char value, unsigned char mode);
void LCD_4bit_send(unsigned char lcd_data);
void LCD_putstr(char *lcd_string);
void LCD_putchar(char char_data);
void LCD_clear_home(void);
void LCD_goto(unsigned char x_pos, unsigned char y_pos);
void toggle_EN_pin(void);

LCD.c

 #include "LCD.h"
 
void LCD_init(void)
{
    delay_ms(10);
 
    LCD_GPIO_init();
    
    LCD_RW_LOW;
 
    LCD_RS_LOW;
    delay_ms(10);
    toggle_EN_pin();
 
    LCD_send(0x33, CMD);
    LCD_send(0x32, CMD);
 
    LCD_send((_4_pin_interface | _2_row_display | _5x7_dots), CMD);
    LCD_send((display_on | cursor_off | blink_off), CMD);
    LCD_send((clear_display), CMD);
    LCD_send((cursor_direction_inc | display_no_shift), CMD);
}
 
void LCD_send(unsigned char value, unsigned char mode)
{
    switch(mode)
    {
        case DAT:
        {
            LCD_RS_HIGH;
            break;
        }
        case CMD:
        {
            LCD_RS_LOW;
            break;
        }
    }
 
    LCD_4bit_send(value);
}
 
void LCD_4bit_send(unsigned char lcd_data)
{
    unsigned char temp = 0;
    
    temp = ((lcd_data & 0x80) >> 7);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB7_HIGH;
            break;
        }
        default:
        {
            LCD_DB7_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x40) >> 6);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB6_HIGH;
            break;
        }
        default:
        {
            LCD_DB6_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x20) >> 5);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB5_HIGH;
            break;
        }
        default:
        {
            LCD_DB5_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x10) >> 4);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB4_HIGH;
            break;
        }
        default:
        {
            LCD_DB4_LOW;
            break;
        }
    }
 
    toggle_EN_pin();
 
    temp = ((lcd_data & 0x08) >> 3);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB7_HIGH;
            break;
        }
        default:
        {
            LCD_DB7_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x04) >> 2);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB6_HIGH;
            break;
        }
        default:
        {
            LCD_DB6_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x02) >> 1);
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB5_HIGH;
            break;
        }
        default:
        {
            LCD_DB5_LOW;
            break;
        }
    }
    
    temp = ((lcd_data & 0x01));
    
    switch(temp)
    {
        case 1:
        { 
            LCD_DB4_HIGH;
            break;
        }
        default:
        {
            LCD_DB4_LOW;
            break;
        }
    }
 
    toggle_EN_pin();
}
 
void LCD_putstr(char *lcd_string)
{
    do
    {
        LCD_send(*lcd_string++, DAT);
    }while(*lcd_string != '\0');
}
 
void LCD_putchar(char char_data)
{
    LCD_send(char_data, DAT);
}
 
void LCD_clear_home(void)
{
    LCD_send(clear_display, CMD);
    LCD_send(goto_home, CMD);
}
 
void LCD_goto(unsigned char x_pos, unsigned char y_pos)
{
    if(y_pos == 0)
    {
        LCD_send((0x80 | x_pos), CMD);
    }
    else
    {
        LCD_send((0x80 | 0x40 | x_pos), CMD);
    }
}
 
void toggle_EN_pin(void)
{
    LCD_EN_HIGH;
    delay_ms(2);
    LCD_EN_LOW;
    delay_ms(2);
}

lcd_print.h

 #define no_of_custom_symbol     1
#define array_size_per_symbol   8
#define array_size              (array_size_per_symbol * no_of_custom_symbol)
 
void load_custom_symbol(void);
void print_symbol(unsigned char x_pos, unsigned char y_pos, unsigned char symbol_index);
void print_C(unsigned char x_pos, unsigned char y_pos, signed int value);
void print_I(unsigned char x_pos, unsigned char y_pos, signed long value);
void print_D(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char points);
void print_F(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points);

lcd_print.c

 #include "lcd_print.h"
 
void load_custom_symbol(void)
{
    unsigned char s = 0;
 
    const unsigned char custom_symbol[array_size] =
    {
        0x00, 0x06, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00
    };
 
    LCD_send(0x40, CMD);
 
    for(s = 0; s < array_size; s++)
    {
         LCD_send(custom_symbol[s], DAT);
    }
 
    LCD_send(0x80, CMD);
}
 
void print_symbol(unsigned char x_pos, unsigned char y_pos, unsigned char symbol_index)
{
    LCD_goto(x_pos, y_pos);
    LCD_send(symbol_index, DAT);
}
 
void print_C(unsigned char x_pos, unsigned char y_pos, signed int value)
{
     char ch[5] = {0x20, 0x20, 0x20, 0x20, '\0'};
 
     if(value < 0x00)
     {
        ch[0] = 0x2D;
        value = -value;
     }
     else
     {
        ch[0] = 0x20;
     }
 
     if((value > 99) && (value <= 999))
     {
         ch[1] = ((value / 100) + 0x30);
         ch[2] = (((value % 100) / 10) + 0x30);
         ch[3] = ((value % 10) + 0x30);
     }
     else if((value > 9) && (value <= 99))
     {
         ch[1] = (((value % 100) / 10) + 0x30);
         ch[2] = ((value % 10) + 0x30);
         ch[3] = 0x20;
     }
     else if((value >= 0) && (value <= 9))
     {
         ch[1] = ((value % 10) + 0x30);
         ch[2] = 0x20;
         ch[3] = 0x20;
     }
 
     LCD_goto(x_pos, y_pos);
     LCD_putstr(ch);
}
 
void print_I(unsigned char x_pos, unsigned char y_pos, signed long value)
{
    char ch[7] = {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, '\0'};
 
    if(value < 0)
    {
        ch[0] = 0x2D;
        value = -value;
    }
    else
    {
        ch[0] = 0x20;
    }
 
    if(value > 9999)
    {
        ch[1] = ((value / 10000) + 0x30);
        ch[2] = (((value % 10000)/ 1000) + 0x30);
        ch[3] = (((value % 1000) / 100) + 0x30);
        ch[4] = (((value % 100) / 10) + 0x30);
        ch[5] = ((value % 10) + 0x30);
    }
 
    else if((value > 999) && (value <= 9999))
    {
        ch[1] = (((value % 10000)/ 1000) + 0x30);
        ch[2] = (((value % 1000) / 100) + 0x30);
        ch[3] = (((value % 100) / 10) + 0x30);
        ch[4] = ((value % 10) + 0x30);
        ch[5] = 0x20;
    }
    else if((value > 99) && (value <= 999))
    {
        ch[1] = (((value % 1000) / 100) + 0x30);
        ch[2] = (((value % 100) / 10) + 0x30);
        ch[3] = ((value % 10) + 0x30);
        ch[4] = 0x20;
        ch[5] = 0x20;
    }
    else if((value > 9) && (value <= 99))
    {
        ch[1] = (((value % 100) / 10) + 0x30);
        ch[2] = ((value % 10) + 0x30);
        ch[3] = 0x20;
        ch[4] = 0x20;
        ch[5] = 0x20;
    }
    else
    {
        ch[1] = ((value % 10) + 0x30);
        ch[2] = 0x20;
        ch[3] = 0x20;
        ch[4] = 0x20;
        ch[5] = 0x20;
    }
 
    LCD_goto(x_pos, y_pos);
    LCD_putstr(ch);
}
 
void print_D(unsigned char x_pos, unsigned char y_pos, signed int value, unsigned char points)
{
    char ch[5] = {0x2E, 0x20, 0x20, 0x20, 0x20};
 
    ch[1] = ((value / 100) + 0x30);
 
    if(points > 1)
    {
        ch[2] = (((value / 10) % 10) + 0x30);
 
        if(points > 1)
        {
            ch[3] = ((value % 10) + 0x30);
        }
    }
 
    LCD_goto(x_pos, y_pos);
    LCD_putstr(ch);
}
 
void print_F(unsigned char x_pos, unsigned char y_pos, float value, unsigned char points)
{
    signed long tmp = 0x00000000;
 
    tmp = value;
    print_I(x_pos, y_pos, tmp);
    tmp = ((value - tmp) * 1000);
 
    if(tmp < 0)
    {
       tmp = -tmp;
    }
 
    if(value < 0)
    {
        value = -value;
        LCD_goto(x_pos, y_pos);
        LCD_putchar(0x2D);
    }
    else
    {
        LCD_goto(x_pos, y_pos);
        LCD_putchar(0x20);
    }
 
    if((value >= 10000) && (value < 100000))
    {
        print_D((x_pos + 6), y_pos, tmp, points);
    }
    else if((value >= 1000) && (value < 10000))
    {
        print_D((x_pos + 5), y_pos, tmp, points);
    }
    else if((value >= 100) && (value < 1000))
    {
        print_D((x_pos + 4), y_pos, tmp, points);
    }
    else if((value >= 10) && (value < 100))
    {
        print_D((x_pos + 3), y_pos, tmp, points);
    }
    else if(value < 10)
    {
        print_D((x_pos + 2), y_pos, tmp, points);
    }
}

main.c

 #include "STC8xxx.h"
#include "BSP.h"
#include "LCD.c"
 
void setup(void);
void show_value(unsigned char value);
 
void main(void)
{
    unsigned char s = 0x00;
 
    char txt1[] = {"MICROARENA"};
    char txt2[] = {"SShahryiar"};
    char txt3[] = {"STC8A Series"};
    char txt4[] = {"STC8A8K64S4A12"};
    
    setup();
    
    LCD_clear_home();
 
    LCD_goto(3, 0);
    LCD_putstr(txt1);
    LCD_goto(3, 1);
    LCD_putstr(txt2);
    delay_ms(4000);
 
    LCD_clear_home();
 
    for(s = 0; s < 12; s++)
    {
        LCD_goto((2 + s), 0);
        LCD_putchar(txt3[s]);
        delay_ms(60);
    }
    for(s = 0; s < 14; s++)
    {
        LCD_goto((1 + s), 1);
        LCD_putchar(txt4[s]);
        delay_ms(60);
    }
    delay_ms(4000);
 
    s = 0;
    LCD_clear_home();
 
    LCD_goto(3, 0);
    LCD_putstr(txt1);
 
    while(1)
    {
        show_value(s);
        s++;
        delay_ms(400);
    };
}
 
void setup(void)
{
    CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_div_1, MCLK_out_P54);
    
    LCD_init();
}
 
void show_value(unsigned char value)
{
   unsigned char ch = 0x00;
 
   ch = ((value / 100) + 0x30);
   LCD_goto(6, 1);
   LCD_putchar(ch);
 
   ch = (((value / 10) % 10) + 0x30);
   LCD_goto(7, 1);
   LCD_putchar(ch);
 
   ch = ((value % 10) + 0x30);
   LCD_goto(8, 1);
   LCD_putchar(ch);
}

Schematic

Explanation

I have demoed this example in all of my past tutorials and so I won’t be explaining it again. The only trick I would like to share is the fact that LCD datasheet documents state how to drive and initialize them. Thus, it is best to read datasheet and try to implement on own. It is very simple.

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Related Posts

10 comments

  • Hi, I am using STC MCU since 10 years. Tech support is ZERO. but they are low cost, very stable. Now I have a problem when the chip that I used is obsolete. Now start to use STC8C2K64S4-28I-LQFP32 but no stc8Cxx.h file, I am using stc8Hxx.h file which compiles but in some stage freeze, the existing firmware. With stc8hxx.h file I can compile STC8F2K64S4-28I-LQFP32 and works not bad
    .
    I wrote them many times for the stc8Cxx.h file never got answer. Where Can I find that file?
    Thank you

  • Hi. Can you explain how to use I2C in the slave mode ?

  • Thanks for these tutorials. I’m getting back into STCmicro coding now, having left them alone for the past several years. Back then I only used the STC89C52RC (and C54RD) but this time I’m also using the more powerful STC15 and STC8 types. Your blogs provide a wealth of useful information.

  • Hello,

    You have done great job with all these tutorials. I am an electronics engineer trying to learn some new stuff. I am located in Greece , Europe and I would like to purchase the development board that you are using and download some datasheets in English if possible but I cannot find them anywhere. Could you please help me?

  • i always get excited when you release new tutorials ,you are really doing a great job i wish i could write code and develop libraries like you.

  • Well, this is very nice and thorough tutorial indeed, many thanks!
    Unfortunately I doubt there is good any reason to learn the STC platform beyond curiosity.
    The STC 8051, although pretty evolved from the original 8051 ISA, does not offer anything crucial to justify the relatively high price of these micros and development tools along with certain cumbersomeness of this ancient platform.
    They simply can not compete even with the legacy Cortex M0 in any way. I am even not aware about any affordable debugger/emulator for them.
    All in all, I would never recommend anybody to start learning/using any 8051 without some very good reason to do so.

Leave a Reply

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