Posted on Leave a comment

Interface LCD 16×2 Character in 4-Bit Mode with PIC16F877A

To save the number of pins of the microcontroller we interface the character LCD in 4-bit mode.

If we take a look at the PINS of the character LCD

[ VSS | VDD | V0 | R/S | R/W | E | D0 | D1 | D2 | D3 | D4 | D5 | D6 | D7 | A | K ]

If we were to use this LCD in 8-bit mode we have to use 8 GPIO pins for the DATA (D0-D7) and three GPIO Pins for control(R/S, R/W, E).

But in 4-bit mode, we use three control pins and only four data pins D4-D7 .

Schematics

Schematics diagram for Home Automation using Bluetooth

In the above schematic, I have interfaced the LCD in 4-bit mode.

LCD BACKLIGHT
I have connected a 220 ohm resistor to the K pin; since my lcd module has a 100 ohm resistor on board. It is labeled R8. So 100 + 220 = 320 ohm resistance total.
If the LED takes approx. 3V forward volage then the current will be given by equation
Current = (VCC – Forward Voltage) / Total resistance
=> (5v-3v)/320ohm = 2v / 320 ohm = 0.00625 A = 6.25 mA(approx.)

LCD Command

You can make your own commands using this table shown below.

CODE

main.hLCD_16x2.hboard.h
/* 
 * File:   main.c
 * Author: abhay
 *
 * Created on July 14, 2023, 12:05 AM
 */

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 16000000
#include <xc.h>
#include <pic16f877a.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "board.h"
#include "LCD_16x2.h"
#include<string.h>

char GPIO_init(void);

/*
 * 
 */
int main(int argc, char** argv) {
    GPIO_init();
    InitLCD(); // Initialize LCD in 8bit mode

    //const char *msg = "Hello World!";
    // const char *msg1 = "Abhay";
    //WriteStringToLCD(msg1);
    char lcd_buff1[16] = "Abhay Kant";
    char lcd_buff2[16] = "Kant";
    
    
    lcd_set_cursor(1, 1);
    WriteDataToLCD('E');
    WriteDataToLCD('X');
    WriteDataToLCD('A');
    WriteDataToLCD('S');
    WriteDataToLCD('U');
    WriteDataToLCD('B');
    WriteDataToLCD('.');
    WriteDataToLCD('C');
    WriteDataToLCD('O');
    WriteDataToLCD('M');

    lcd_set_cursor(2, 1);
    WriteStringToLCD(lcd_buff1);

    while (1) {

    }

    return (EXIT_SUCCESS);
}

char GPIO_init(void) {
    /*
     * TRIS = Data Direction Register
     * 0 = OUTPUT
     * 1 = INPUT
     * TRISD &= ~(1 << 1); // LED RD1 as OUTPUT
     * TRISD1 = 0; // RD1 as OUTPUT
     */



    LED_PORT_DIR &= ~(1 << 1);
    LED_PIN = 0;

    Relay_1a_DIR &= ~(1 << 0);
    Relay_1a_PIN = 0;

    Relay_1b_DIR &= ~(1 << 2);
    Relay_1b_PIN = 0;

    Relay_2a_DIR &= ~(1 << 4);
    Relay_2a_PIN = 0;

    Relay_2b_DIR &= ~(1 << 5);
    Relay_2b_PIN = 0;

    /*
     * LCD Pins Initialize
     */
    TRISB = 0;
    return 0;
}
/*
 * File:   LCD_16x2.h
 * Author: abhay
 *
 * Created on July 16, 2023, 6:21 PM
 */

#ifndef LCD_16X2_H
#define	LCD_16X2_H

#include "board.h"


#ifdef	__cplusplus
extern "C" {
#endif
    /*
     *Sr.No.	Hex Code	Command to LCD instruction Register
        1       01          Clear display screen
        2       02          Return home
        3       04          Decrement cursor (shift cursor to left)
        4       06          Increment cursor (shift cursor to right)
        5       05          Shift display right
        6       07          Shift display left
        7       08          Display off, cursor off
        8       0A          Display off, cursor on
        9       0C          Display on, cursor off
        10  	0E          Display on, cursor blinking off
        11  	0F          Display on, cursor blinking on
        12      10          Shift cursor position to left
        13      14          Shift the cursor position to the right
        14      18          Shift the entire display to the left
        15      1C          Shift the entire display to the right
        16      80          Force cursor to the beginning ( 1st line)
        17  	C0          Force cursor to the beginning ( 2nd line)
        18  	38          2 lines and 5×7 matrix
     */
    // Define Pins

#define LCD_RS         RB1     // RS pin for LCD
#define LCD_RW         RB2     // RS pin for LCD
#define LCD_E          RB3     // Enable pin for LCD
#define LCD_Data_Bus_D4    RB4    // Data bus bit 4
#define LCD_Data_Bus_D5    RB5    // Data bus bit 5
#define LCD_Data_Bus_D6    RB6    // Data bus bit 6
#define LCD_Data_Bus_D7    RB7    // Data bus bit 7
    // Define Pins direction registrers
#define LCD_E_Dir           TRISB3
#define LCD_RS_Dir          TRISB1
#define LCD_RW_Dir          TRISB2
#define LCD_Data_Bus_Dir_D4     TRISB4
#define LCD_Data_Bus_Dir_D5     TRISB5
#define LCD_Data_Bus_Dir_D6    TRISB6
#define LCD_Data_Bus_Dir_D7   TRISB7
    // Constants
#define E_Delay       1000
    // Function Declarations
    void WriteCommandToLCD(unsigned char);
    void WriteDataToLCD(char);
    void InitLCD(void);
    void WriteStringToLCD(const char*);
    void ClearLCDScreen(void);

    void ToggleEpinOfLCD(void) {
        LCD_E = 1; // Give a pulse on E pin
        __delay_us(E_Delay); // so that LCD can latch the
        LCD_E = 0; // data from data bus
        __delay_us(E_Delay);
    }

    void WriteCommandToLCD(unsigned char Command) {
        LCD_RS = 0; // It is a command
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= (Command & 0xF0); // Write Upper nibble of data
        ToggleEpinOfLCD(); // Give pulse on E pin
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= ((Command << 4)&0xF0); // Write Lower nibble of data
        ToggleEpinOfLCD(); // Give pulse on E pin
    }

    void WriteDataToLCD(char LCDChar) {
        LCD_RS = 1; // It is data
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= (LCDChar & 0xF0); // Write Upper nibble of data
        ToggleEpinOfLCD(); // Give pulse on E pin
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= ((LCDChar << 4)&0xF0); // Write Lower nibble of data
        ToggleEpinOfLCD(); // Give pulse on E pin
    }

    void InitLCD(void) {
        // Firstly make all pins output
        LCD_E = 0; // E  = 0
        LCD_RS = 0; // RS = 0
        LCD_Data_Bus_D4 = 0; // Data bus = 0
        LCD_Data_Bus_D5 = 0; // Data bus = 0
        LCD_Data_Bus_D6 = 0; // Data bus = 0
        LCD_Data_Bus_D7 = 0; // Data bus = 0
        LCD_E_Dir = 0; // Make Output
        LCD_RS_Dir = 0; // Make Output
        LCD_RW_Dir = 0;
        LCD_RW = 0;
        LCD_Data_Bus_Dir_D4 = 0; // Make Output
        LCD_Data_Bus_Dir_D5 = 0; // Make Output
        LCD_Data_Bus_Dir_D6 = 0; // Make Output
        LCD_Data_Bus_Dir_D7 = 0; // Make Output
        ///////////////// Reset process from datasheet //////////////
        __delay_ms(40);
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= 0x30; // Write 0x3 value on data bus
        ToggleEpinOfLCD(); // Give pulse on E pin
        __delay_ms(6);
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= 0x30; // Write 0x3 value on data bus
        ToggleEpinOfLCD(); // Give pulse on E pin
        __delay_us(300);
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= 0x30; // Write 0x3 value on data bus
        ToggleEpinOfLCD(); // Give pulse on E pin
        __delay_ms(2);
        PORTB &= 0x0F; // Make Data pins zero
        PORTB |= 0x20; // Write 0x2 value on data bus
        ToggleEpinOfLCD(); // Give pulse on E pin
        __delay_ms(2);
        /////////////// Reset Process End ////////////////
        WriteCommandToLCD(0x28); //function set
        WriteCommandToLCD(0x0c); //display on,cursor off,blink off
        WriteCommandToLCD(0x01); //clear display
        WriteCommandToLCD(0x06); //entry mode, set increment
        WriteCommandToLCD(0x0e); //display on,cursor on,blink off
        WriteCommandToLCD(0x0f); //display on,cursor on,blink on
    }

    void WriteStringToLCD(const char *s) {
        while (*s) {
            WriteDataToLCD(*s++); // print first character on LCD
        }
    }

    void ClearLCDScreen(void) // Clear the Screen and return cursor to zero position
    {
        WriteCommandToLCD(0x01); // Clear the screen
        __delay_ms(2); // Delay for cursor to return at zero position
    }

    void lcd_set_cursor(uint8_t row, uint8_t col) {
        if (row == 1) {
            WriteCommandToLCD(0x80 | (col - 1));
        } else if (row == 2) {
            WriteCommandToLCD(0xC0 | (col - 1));
        }
    }

#ifdef	__cplusplus
}
#endif

#endif	/* LCD_16X2_H */

/* 
 * File:   board.h
 * Author: abhay
 *
 * Created on July 14, 2023, 12:41 AM
 */

#ifndef BOARD_H
#define	BOARD_H

#ifdef	__cplusplus
extern "C" {
#endif
/**
 * LED 
 * color: RED
 * @param PORT: D
 * @param PIN: 1
 */
#define LED_PORT_DIR TRISD
#define LED_Port PORTD
#define LED_PIN PORTDbits.RD1
    
#define Relay_1a_DIR TRISD
#define Relay_1a_Port PORTD
#define Relay_1a_PIN PORTDbits.RD0

#define Relay_1b_DIR TRISD    
#define Relay_1b_Port PORTD
#define Relay_1b_PIN PORTDbits.RD2

#define Relay_2a_DIR TRISD
#define Relay_2a_Port PORTD
#define Relay_2a_PIN PORTDbits.RD4

#define Relay_2b_DIR TRISD
#define Relay_2b_Port PORTD
#define Relay_2b_PIN PORTDbits.RD5

#ifdef	__cplusplus
}
#endif

#endif	/* BOARD_H */

Posted on Leave a comment

How to use 2.8 inch LCD driver on Mini Stm32 V3.0 using STM32CubeIDE

To use the 2.8-inch LCD.

I first checked the hardware schematic to find out all the pins attached to the LCD.

The LCD has a driver ic on its flex cable.
when I read the device code from the IC. The IC is ILI9325

You can check this post here for that

Mini STM32 V3.0

After checking the schematic diagram of both the development board and the LCD. It is time to create a project in STM32 Cube IDE.

After creating the project this is what the pin arrangment looks like in the integrated cubemx of stm32cube ide.

Download the LCD Library from below

  1. Download and extract the zip file.
  2. Inside you will find a folder named “LCD28”
  3. Copy that folder and place it into the driver folder of your stm32cubeide project.
  4. Insert the following header files into your main.c file
/* USER CODE BEGIN Includes */
#include "stm32f103xb.h"
#include "stdio.h"
#include "stdlib.h"
#include "../../Drivers/LCD28/ili932x.h"

/* USER CODE END Includes */
  1. Add the following code to initialize the LCD
/* USER CODE BEGIN 2 */
  

	HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_SET);
	LCD_Init();
	LCD_Clear(BLACK);

  /* USER CODE END 2 */
  1. Here are the list of functions you can use to make graphic on the 2.8 inch display
void LCD_Clear(uint16_t Color);
void LCD_Delay(uint32_t nCount);
void LCD_DrawPoint(uint16_t x,uint16_t y);
void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
void Draw_Circle(uint8_t x0,uint16_t y0,uint8_t r);
void LCD_Fill(uint8_t xsta,uint16_t ysta,uint8_t xend,uint16_t yend,uint16_t color);
uint16_t WriteOneASCII(uint8_t *pucMsk,
                              uint16_t x0,
                              uint16_t y0,
                              uint16_t color);
uint16_t WriteOneHzChar(uint8_t *pucMsk,
                               uint16_t x0,
                               uint16_t y0,
                               uint16_t color);
void WriteString(uint16_t x0, uint16_t y0,uint8_t *pcStr, uint16_t color);

Demo Program

/* USER CODE BEGIN WHILE */

	HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_SET);
	HAL_Delay(1000);
	LCD_Init();
	LCD_Clear(BLACK);

	uint8_t *textPtr;

	// Example usage of the provided LCD functions
	LCD_Clear(0xFFFF); // Clear the LCD screen with white color

	// Draw a blue line from (10, 10) to (100, 50)
	LCD_DrawLine(10, 10, 100, 50);

	// Draw a filled rectangle from (120, 50) to (180, 150) with red color
	LCD_Fill(120, 50, 180, 150, 0xF800);

	// Draw a green circle with center at (200, 200) and radius 20
	Draw_Circle(200, 200, 20);

	// Write a string at (50, 250) with black color

	textPtr = ((uint8_t *)"Hello www.EXASUB.com");
	WriteString(50,(250),textPtr,RED);



	// Color array in the desired order
	uint16_t colors[] = {BLACK, NAVY, DGREEN, DCYAN, MAROON, PURPLE, OLIVE, LGRAY,
			DGRAY, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE};
	uint8_t numColors = sizeof(colors) / sizeof(colors[0]);

	// Index variable for cycling through colors
	uint8_t colorIndex = 0;
	while (1)
	{
		/* USER CODE END WHILE */
		textPtr = ((uint8_t *)"Hello www.EXASUB.com");
		WriteString(50,(250),textPtr,colors[colorIndex]);
		// Increment the color index and wrap around if necessary
		colorIndex = (colorIndex + 1) % numColors;
		textPtr = ((uint8_t *)"ScIeNcE TeCh EnG MaTh ");
		WriteString(10,(270),textPtr,colors[colorIndex]);

		HAL_Delay(250);


		/* USER CODE BEGIN 3 */
	}
	/* USER CODE END 3 */
Posted on Leave a comment

Removed the faulty touch screen from STM32F429I-DISC1

The LCD display on this touchscreen is having issues. It is not responding to touch. And suddenly the display stopped working. So I cut the foam and suddenly the ribbon tore off completely.

As you can see the screen is completely removed.

It now gives access to all the pins that are being used by the LCD.

I am planning to make a VGA controller using this board since the microcontroller has an LTDC (LCD TFT Controller)

Posted on Leave a comment

How to generate custom fonts for LCD display

There are various tools to generate font style for LCD display.

  1. Write your own fonts style. But this is will become hectic if you have a larger font size.
  2. Software tool
    1. The Dot Factory by Eran Duchan
    2. GLCD Font Creator

I like “The Dot Factory” font creation tool. It is minimalistic and gets the job done.

Here is the link to the Github page for The Dot Factory https://github.com/pavius/the-dot-factory