GLCD Ks0108 Code not working

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. I have modified a code  library for GLCD Ks0108 type.

 

2. Nothing is getting displayed on GLCD in code.

 

3. I have checked individual pins by set & reset them on CRO, so they are ok.

 

4. MCU is running at 120Mhz core, library had 5 Nops by default in function glcd_delay() & it was written for 8Mhz. As my MCU is running at 120Mhz, so I had added 15 times more Nops.

 

5. Able to debug through code no issues but nothing is displaying on lcd.

 

6. here is the link for code. It has four files: main.c, glcd.c, glcd.h, fonts.h.
glcd.c I am adding in this forum, rest can be seen on google drive here: https://drive.google.com/open?id...

 

7. MCU i am using is STM32F205RBT6, but I think it is more of initialization issue. Thats why posted in general electronics forum.

 

#include "stm32f2xx.h"
#include "glcd.h"

#define KS0108_SCREEN_WIDTH	    128
#define KS0108_SCREEN_HEIGHT	64
#define DISPLAY_SET_Y       0x40
#define DISPLAY_SET_X       0xB8
#define DISPLAY_START_LINE  0xC0
#define DISPLAY_ON_CMD		0x3E
  #define ON	0x01
  #define OFF	0x00
#define DISPLAY_STATUS_BUSY	0x80

uint8_t screen_x = 0U;
uint8_t screen_y = 0U;

/**
 *  @function : void glcd_bitmap(char * bmp , uint8_t x, uint8_t y , uint8_t dx , uint8_t dy)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_bitmap(char * bmp , uint8_t x, uint8_t y , uint8_t dx , uint8_t dy)
{

    uint8_t i;
    uint8_t j;

    for(j = 0U ; j < dy / 8U ; j++)
    {
        glcd_go_to(x , y + j);

        for(i = 0U ; i < dx ; i++)
        {
            glcd_write_data(glcd_read_byte_from_ROM_memory(bmp++));
        }
    }

} /* function ends here */

/**
 *  @function : void glcd_set_pixel(uint8_t x , uint8_t y , uint8_t color)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_set_pixel(uint8_t x , uint8_t y , uint8_t color)
{
    uint8_t tmp;

    glcd_go_to(x , y/8U);
    tmp = glcd_read_data();
    glcd_go_to(x , y/8U);
    tmp = glcd_read_data();
    glcd_go_to(x , y/8U);

    if(color)
    {
        tmp = tmp | (1U << (y%8U));
    }
    else
    {
        tmp = tmp & (~(1U << (y%8U)));
    }

    glcd_write_data(tmp);

} /* function ends here */

/**
 *  @function : void glcd_write_string(char *string_to_write)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_write_string(char *string_to_write)
{
    while(*string_to_write)
    {
        glcd_write_char(*string_to_write++);
    }

} /* function ends here */

/**
 *  @function : void glcd_write_char(char char_to_write)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_write_char(char char_to_write)
{
    uint8_t i;

    char_to_write = char_to_write - 32;

    for(i = 0U ; i < 5U ; i++)
    {
        glcd_write_data(glcd_read_byte_from_ROM_memory((char *)(font5x8 + (5 * char_to_write) + i)));
    }

    glcd_write_data(0x00);

} /* function ends here */

/**
 *  @function : void glcd_clear_screen(void)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_clear_screen(void)
{
    uint8_t i;
    uint8_t j;

    for(j = 0U ; j < KS0108_SCREEN_HEIGHT / 8U ; j++)
	{
        glcd_go_to(0U , j);

        for(i = 0U ; i < KS0108_SCREEN_WIDTH ; i++)
 		{
            glcd_write_data(0x00U);
        }
	}

} /* function ends here */

/**
 *  @function : void glcd_text_go_to(uint8_t x , uint8_t y)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_text_go_to(uint8_t x , uint8_t y)
{
    glcd_go_to(x*6,y);

} /* function ends here */

/**
 *  @function : void glcd_go_to(uint8_t x, uint8_t y)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_go_to(uint8_t x, uint8_t y)
{
    uint8_t i;

    screen_x = x;
    screen_y = y;

    for(i = 0; i < KS0108_SCREEN_WIDTH / 64U; i++)
    {
      glcd_write_command(DISPLAY_SET_Y | 0 , i);
      glcd_write_command(DISPLAY_SET_X | y , i);
      glcd_write_command(DISPLAY_START_LINE | 0,i);
    }
    glcd_write_command((DISPLAY_SET_Y | (x % 64)) , (x / 64));
    glcd_write_command((DISPLAY_SET_X | y) , (x / 64));

} /* function ends here */

/**
 *  @function : void glcd_initialize(void)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_initialize(void)
{
    uint8_t i;

    glcd_initialize_ports();

    for(i = 0U; i < 3U; i++)
    {
        glcd_write_command((DISPLAY_ON_CMD | ON), i);
    }

} /* function ends here */

/**
 *  @function : unsigned char glcd_read_byte_from_ROM_memory(char * ptr)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
unsigned char glcd_read_byte_from_ROM_memory(char * ptr)
{
    return *(ptr);

} /* function ends here */

/**
 *  @function : void glcd_initialize_ports(void)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_initialize_ports(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB , ENABLE );
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_3 | GPIO_Pin_4
                                    | GPIO_Pin_5 | GPIO_Pin_6
                                    | GPIO_Pin_7 | GPIO_Pin_8
                                    | GPIO_Pin_9 | GPIO_Pin_12
                                    | GPIO_Pin_13 | GPIO_Pin_14
                                    | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure); 

    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC , ENABLE );
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);   

    GLCD_CS1_SET();
    GLCD_CS2_SET();
    GLCD_CS3_SET();
    GLCD_RS_SET();
    GLCD_RW_SET();
    GLCD_DB0_SET();
    GLCD_DB1_SET();
    GLCD_DB2_SET();
    GLCD_DB3_SET();
    GLCD_DB4_SET();
    GLCD_DB5_SET();
    GLCD_DB6_SET();
    GLCD_DB7_SET();

} /* function ends here */

/**
 *  @function : void glcd_write_data(uint8_t data_to_write)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_write_data(uint8_t data_to_write)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    while( glcd_read_status(screen_x / 64U) & DISPLAY_STATUS_BUSY );

/* make port as output */
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_6 | GPIO_Pin_7
                                    | GPIO_Pin_8 | GPIO_Pin_9
                                    | GPIO_Pin_12 | GPIO_Pin_13
                                    | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);     

    GLCD_RW_RESET();
    glcd_delay();
    GLCD_RS_SET();
    glcd_delay();

    if(data_to_write & 0x01U) { GLCD_DB0_SET(); } else{ GLCD_DB0_RESET(); }
    if(data_to_write & 0x02U) { GLCD_DB1_SET(); } else{ GLCD_DB1_RESET(); }
    if(data_to_write & 0x04U) { GLCD_DB2_SET(); } else{ GLCD_DB2_RESET(); }
    if(data_to_write & 0x08U) { GLCD_DB3_SET(); } else{ GLCD_DB3_RESET(); }
    if(data_to_write & 0x10U) { GLCD_DB4_SET(); } else{ GLCD_DB4_RESET(); }
    if(data_to_write & 0x20U) { GLCD_DB5_SET(); } else{ GLCD_DB5_RESET(); }
    if(data_to_write & 0x40U) { GLCD_DB6_SET(); } else{ GLCD_DB6_RESET(); }
    if(data_to_write & 0x80U) { GLCD_DB7_SET(); } else{ GLCD_DB7_RESET(); }
    glcd_delay();    

    glcd_enable_controller(screen_x / 64U);
    glcd_delay();

    GLCD_EN_SET();
    glcd_delay();
    GLCD_EN_RESET();
    glcd_delay();

    glcd_disable_controller(screen_x / 64U);
    screen_x++;

} /* function ends here */

/**
 *  @function : uint8_t glcd_read_data(void)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
uint8_t glcd_read_data(void)
{
    uint8_t status;
    GPIO_InitTypeDef GPIO_InitStructure;

    while( glcd_read_status(screen_x / 64U) & DISPLAY_STATUS_BUSY );

/* make port as input */
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_6 | GPIO_Pin_7
                                    | GPIO_Pin_8 | GPIO_Pin_9
                                    | GPIO_Pin_12 | GPIO_Pin_13
                                    | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);  

    GLCD_RW_SET();
    GLCD_RS_SET();

    glcd_enable_controller(screen_x / 64U);
    glcd_delay();
    GLCD_EN_SET();
    glcd_delay();

/* read status */
    status = 0U;
    if(GLCD_DB0_VALUE()) { status = status | 0x01U; }
    if(GLCD_DB1_VALUE()) { status = status | 0x02U; }
    if(GLCD_DB2_VALUE()) { status = status | 0x04U; }
    if(GLCD_DB3_VALUE()) { status = status | 0x08U; }
    if(GLCD_DB4_VALUE()) { status = status | 0x10U; }
    if(GLCD_DB5_VALUE()) { status = status | 0x20U; }
    if(GLCD_DB6_VALUE()) { status = status | 0x40U; }
    if(GLCD_DB7_VALUE()) { status = status | 0x80U; }     

    GLCD_EN_RESET();
    glcd_disable_controller(screen_x / 64U);
    screen_x++;

    return status;

} /* function ends here */

/**
 *  @function : void glcd_write_command(uint8_t command_to_write, uint8_t controller)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_write_command(uint8_t command_to_write, uint8_t controller)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    while( glcd_read_status(controller)  &  DISPLAY_STATUS_BUSY);

/* make port as output */
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_6 | GPIO_Pin_7
                                    | GPIO_Pin_8 | GPIO_Pin_9
                                    | GPIO_Pin_12 | GPIO_Pin_13
                                    | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);     

    GLCD_RW_RESET();
    GLCD_RS_RESET();
    glcd_delay();
    glcd_enable_controller(controller);
    glcd_delay();

    if(command_to_write & 0x01U) { GLCD_DB0_SET(); } else{ GLCD_DB0_RESET(); }
    if(command_to_write & 0x02U) { GLCD_DB1_SET(); } else{ GLCD_DB1_RESET(); }
    if(command_to_write & 0x04U) { GLCD_DB2_SET(); } else{ GLCD_DB2_RESET(); }
    if(command_to_write & 0x08U) { GLCD_DB3_SET(); } else{ GLCD_DB3_RESET(); }
    if(command_to_write & 0x10U) { GLCD_DB4_SET(); } else{ GLCD_DB4_RESET(); }
    if(command_to_write & 0x20U) { GLCD_DB5_SET(); } else{ GLCD_DB5_RESET(); }
    if(command_to_write & 0x40U) { GLCD_DB6_SET(); } else{ GLCD_DB6_RESET(); }
    if(command_to_write & 0x80U) { GLCD_DB7_SET(); } else{ GLCD_DB7_RESET(); }    

    glcd_delay();
    GLCD_EN_SET();
    glcd_delay();
    GLCD_EN_RESET();
    glcd_delay();

    glcd_disable_controller(controller);

} /* function ends here */

/**
 *  @function : uint8_t glcd_read_status(uint8_t controller)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
uint8_t glcd_read_status(uint8_t controller)
{
    uint8_t status;
    GPIO_InitTypeDef GPIO_InitStructure;

/* make port as input */
    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin   =   GPIO_Pin_6 | GPIO_Pin_7
                                    | GPIO_Pin_8 | GPIO_Pin_9
                                    | GPIO_Pin_12 | GPIO_Pin_13
                                    | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);      

    GLCD_RW_SET();
    GLCD_RS_RESET();
    glcd_enable_controller(controller);
    glcd_delay();
    GLCD_EN_SET();
    glcd_delay();

/* read status */
    status = 0U;
    if(GLCD_DB0_VALUE()) { status = status | 0x01U; }
    if(GLCD_DB1_VALUE()) { status = status | 0x02U; }
    if(GLCD_DB2_VALUE()) { status = status | 0x04U; }
    if(GLCD_DB3_VALUE()) { status = status | 0x08U; }
    if(GLCD_DB4_VALUE()) { status = status | 0x10U; }
    if(GLCD_DB5_VALUE()) { status = status | 0x20U; }
    if(GLCD_DB6_VALUE()) { status = status | 0x40U; }
    if(GLCD_DB7_VALUE()) { status = status | 0x80U; }      

    GLCD_EN_RESET();

    glcd_disable_controller(controller);

    return status;

} /* function ends here */

/**
 *  @function : void glcd_disable_controller(uint8_t controller)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_disable_controller(uint8_t controller)
{
    switch(controller)
    {
        case 0:
            GLCD_CS1_SET();
        break;

        case 1:
            GLCD_CS2_SET();
        break;

        case 2:
            GLCD_CS3_SET();
        break;

        default:
        break;
	}

} /* function ends here */

/**
 *  @function : void glcd_enable_controller(uint8_t controller)
 *  @brief    :
 *  @parameter:
 *  @return   :
 **/
void glcd_enable_controller(uint8_t controller)
{
    switch(controller)
    {
        case 0:
            GLCD_CS1_RESET();
        break;

        case 1:
            GLCD_CS2_RESET();
        break;

        case 2:
            GLCD_CS3_RESET();
        break;

        default:
        break;
	}

} /* function ends here */

/**
 *  @function : void glcd_delay(void)
 *  @brief    : library had 5 delays for 8mhz, so adding 15 times more delays for 120Mhz
 *  @parameter: none
 *  @return   : none
 **/
void glcd_delay(void)
{
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();
    __NOP(); __NOP(); __NOP(); __NOP(); __NOP();    

} /* function ends here */ 

/************************************** End Of File ***************************************************************/

 

Last Edited: Fri. Oct 6, 2017 - 01:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code appears to be for STM's STM32 range?

 

Why do you think Atmel would pay to provide this site to support a competitors product?

 

Locked. Take this to an STM support forum.

Topic locked