cancel
Showing results for 
Search instead for 
Did you mean: 

GLCD only half screen is working

vinaytiwary
Associate II

I am working on Graphic Display(JDH12864E) using STM32L031K6. I have written code and I found that my lcd is printing only half of screen.

For example:- I try to print ("12345678912345") then it prints only ("123456789") it means it is printing only 10 char.

I think the problem is with GLCD_EnableController and GLCD_DisableController function. I tried to search on google i i found that CS1 and CS2 pin is responsible for Left half of screen and Right half of screen but i could not resolve it. please help me to solve this error, so i can print full screen .

my code is :-

/********************************main.c**************************************************/
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  HAL_Delay(100);
  GLCD_Initialize();
  HAL_Delay(100);
  GLCD_ClearScreen();
  while (1)
  {
      GLCD_GoTo(0,0);
      GLCD_WriteString("Hello World!");
      HAL_Delay(10);
  }
}
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
                          |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
                          |GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);

  /*Configure GPIO pins : PA0 PA1 PA2 PA3
                           PA4 PA5 PA6 PA7
                           PA8 PA9 PA10 PA11
                           PA12 PA13 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
                          |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
                          |GPIO_PIN_12|GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pins : PB0 PB1 */
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
/***********************************LCD.c****************************************************/

#include "stm32l0xx_hal.h"
#include "graphic_display.h"
//#include "lcd.h"
#include "font5x8.h"
#include "stm32l0xx_hal_gpio.h"  // Include the GPIO header

unsigned char screen_x = 0;
unsigned char screen_y = 0;
unsigned char color = 1;

void GLCD_Initialize(void)
{
    unsigned char i;
    GLCD_InitializePorts();
    for(i = 0; i < 3; i++)
        GLCD_WriteCommand((DISPLAY_ON_CMD | ON), i);
}

void GLCD_Delay_us(uint32_t us)
{
    uint32_t start = SysTick->VAL;
    uint32_t ticks = (SystemCoreClock / 1000000) * us;
    while ((start - SysTick->VAL) < ticks);
}

void GLCD_EnableController(unsigned char controller)
{
    switch(controller){
        case 0 : HAL_GPIO_WritePin(KS0108_PORT, KS0108_CS1, GPIO_PIN_RESET); break;
        case 1 : HAL_GPIO_WritePin(KS0108_PORT, KS0108_CS2, GPIO_PIN_RESET); break;
    }
}

void GLCD_DisableController(unsigned char controller)
{
    switch(controller){
        case 0 : HAL_GPIO_WritePin(KS0108_PORT, KS0108_CS1, GPIO_PIN_SET); break;
        case 1 : HAL_GPIO_WritePin(KS0108_PORT, KS0108_CS2, GPIO_PIN_SET); break;
    }
}

unsigned char GLCD_ReadStatus(unsigned char controller)
{
    unsigned char status;

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = (0xFF << KS0108_D0);
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
    GPIO_InitStructure.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(KS0108_PORT, &GPIO_InitStructure);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_RS, GPIO_PIN_RESET);
    GLCD_EnableController(controller);
    GLCD_Delay_us(3);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_SET);
    GLCD_Delay_us(3);
    status = ((HAL_GPIO_ReadPin(KS0108_PORT, GPIO_InitStructure.Pin) >> KS0108_D0) & 0xFF);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_RESET);
    GLCD_Delay_us(3);
    GLCD_DisableController(controller);
    return status;
}

void GLCD_WriteCommand(unsigned char commandToWrite, unsigned char controller)
{
    //while (GLCD_ReadStatus(controller) & DISPLAY_STATUS_BUSY);
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = (0xFF << KS0108_D0);
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(KS0108_PORT, &GPIO_InitStructure);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_RS, GPIO_PIN_RESET);
    GLCD_EnableController(controller);
    HAL_GPIO_WritePin(KS0108_PORT, GPIO_InitStructure.Pin, (commandToWrite << KS0108_D0));
   commandToWrite ^= 0xFF;
    HAL_GPIO_WritePin(KS0108_PORT, GPIO_InitStructure.Pin, (commandToWrite << KS0108_D0));
    GLCD_Delay_us(3);
    HAL_GPIO_WritePin(KS0108_PORT, (commandToWrite << KS0108_D0), GPIO_PIN_RESET); 
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_SET);
    GLCD_Delay_us(3);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_RESET);
    GLCD_DisableController(controller);
    GLCD_Delay_us(3);
}

void GLCD_WriteData(unsigned char dataToWrite)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = (0xFF << KS0108_D0);
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(KS0108_PORT, &GPIO_InitStructure);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_RS, GPIO_PIN_SET);
    HAL_GPIO_WritePin(KS0108_PORT, GPIO_InitStructure.Pin, (dataToWrite << KS0108_D0));
    dataToWrite ^= 0xFF;
    HAL_GPIO_WritePin(KS0108_PORT, GPIO_InitStructure.Pin, (dataToWrite << KS0108_D0));
    GLCD_Delay_us(2);
    HAL_GPIO_WritePin(KS0108_PORT, (dataToWrite << KS0108_D0), GPIO_PIN_RESET); //removing withe box
    GLCD_EnableController(screen_x / 64);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_SET);
    GLCD_Delay_us(2);
    HAL_GPIO_WritePin(KS0108_PORT, KS0108_EN, GPIO_PIN_RESET);
    GLCD_Delay_us(2);
    GLCD_DisableController(screen_x / 64);
   // screen_x++;
}

void GLCD_InitializePorts(void)
{
    // Enable GPIO port clock
    RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // Enable GPIOA clock

    // Configure GPIO pins
    GPIOA->MODER &= ~(GPIO_MODER_MODE0_Msk | GPIO_MODER_MODE1_Msk | GPIO_MODER_MODE2_Msk | GPIO_MODER_MODE3_Msk |
                      GPIO_MODER_MODE4_Msk | GPIO_MODER_MODE5_Msk | GPIO_MODER_MODE6_Msk | GPIO_MODER_MODE7_Msk);
    GPIOA->MODER |= (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE0_Pos) | (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE1_Pos) |
                    (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE2_Pos) | (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE3_Pos) |
                    (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE4_Pos) | (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE5_Pos) |
                    (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE6_Pos) | (GPIO_MODE_OUTPUT_PP << GPIO_MODER_MODE7_Pos);

    GPIOA->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEED0_Msk | GPIO_OSPEEDER_OSPEED1_Msk | GPIO_OSPEEDER_OSPEED2_Msk | GPIO_OSPEEDER_OSPEED3_Msk |
                        GPIO_OSPEEDER_OSPEED4_Msk | GPIO_OSPEEDER_OSPEED5_Msk | GPIO_OSPEEDER_OSPEED6_Msk | GPIO_OSPEEDER_OSPEED7_Msk);
    GPIOA->OSPEEDR |= (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED0_Pos) | (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED1_Pos) |
                      (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED2_Pos) | (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED3_Pos) |
                      (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED4_Pos) | (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED5_Pos) |
                      (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED6_Pos) | (GPIO_SPEED_FREQ_LOW << GPIO_OSPEEDER_OSPEED7_Pos);

    // Set GPIO pins to default values
    GPIOA->ODR = KS0108_CS1 | KS0108_CS2 | KS0108_RS | (0xFF << KS0108_D0);
}
unsigned char GLCD_ReadByteFromROMMemory(char * ptr)
{
    return *(ptr);
}

void GLCD_GoTo(unsigned char x, unsigned char y)
{
    unsigned char i;
    screen_x = x;
    screen_y = y;

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

}

void GLCD_ClearScreen(void)
{
    unsigned char i, j;
    for(j = 0; j < KS0108_SCREEN_HEIGHT / 8; j++)
    {
        GLCD_GoTo(0, j);
        for(i = 0; i < KS0108_SCREEN_WIDTH; i++)
            GLCD_WriteData(0x00);
    }
}

void GLCD_WriteChar(char charToWrite)
{
    char i;
    charToWrite -= ' ';
    for(i = 0; i < 5; i++)
        GLCD_WriteData(GLCD_ReadByteFromROMMemory((char *)(font5x8 + (5 * charToWrite) + i)));
    GLCD_WriteData(0x00);
}

void GLCD_WriteString(char * stringToWrite)
{
    while(*stringToWrite)
        GLCD_WriteChar(*stringToWrite++);
}

void delay_us(uint32_t us)
{
    uint32_t start = SysTick->VAL;
    uint32_t ticks = (SystemCoreClock / 1000000) * us;
    while ((start - SysTick->VAL) < ticks);
}

/***********************************LCD.c**************************************************************/
#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

#define ON (1)
#define OFF (0)

#define KS0108_DATA_PORT GPIOA

#define KS0108_PORT  GPIOA

#define KS0108_RS    GPIO_PIN_8
#define KS0108_RW    GPIO_PIN_13
#define KS0108_EN    GPIO_PIN_9

#define KS0108_CS1   GPIO_PIN_11
#define KS0108_CS2   GPIO_PIN_10
//#define KS0108_CS3   GPIO_PIN_13

#define KS0108_D0    0

/******************************************************************************************************/

 

2 REPLIES 2
SHs
ST Employee

Hello @vinaytiwary ,

Need more details about your hardware setup, have you checked whether you are utilizing reserved-for-further-purposes IOs? If no, please refer to the schematic of your board, you may be using the type of pins that are connected with solder bridges to USER push buttons,LEDs ,UART or any other hardware necessary purposes.

Please close this topic by clicking on “Accept as solution" button if it fully answered your question.
AA1
Senior III

It seems in GLCD_WriteData screen_x is always < 64. And this happens because screen_x is not incremented. GLCD_WriteChar must update screen_x according to font width. I think in your case it is 6. So add this line: screen_x += 6;