Home Parallel Port Serial Port USB .NET Webboard I/O Today API Project Link


 Interfacing LCD display to printer port in C# .NET

Thai Page Click Here

How to interfacing LCD with printer port in C# .NET

Overview of LCD interfacing


    Understand C#.NET programming to display LCD with printer port
This program controls a LCD for display of string that we will be write string to printer port for control LCD. In general ,We can connect LCD with a printer port signal that

For the LCD modules,You can provide to electronic store that there are a wide range like 8x1, 8x2, 16x1, 16x2, 20x2, 20x4, 40x4. For this project ,I have used 16x2- that means 2 rows of 16 characters. It is a IC model HD44780 compatible module, having 16 pins including 2 pins for backlight source.
Following table gives pin structure of LCD module. This LCD modules without backlight will have using only 14 pins. If you are using such LCDs, simply ignore 15th and 16th pins.

Pin No Symbol Details
1 GND Ground
2 Vcc Supply Voltage +5V
3 Vo Contrast adjustment
4 RS 0->Control input,    1-> Data input
5 R/W Read/ Write
6 E Enable
7 to 14 D0 to D7 Data
15 VB1 Backlight +5V
16 VB0 Backlight ground

Pins Position and Wring Name of LCD module Table

Note

For more information about before starting our project LCD.

  • When you are using computer in full screen mode like Games, movie or TV; You need to exit the application to get a small updating information from the computer. i.e., if you need to watch time in that time, you have to close the games. But instead of that you can use LCD module to display real time from the PC and you can use it along with your application. Real time implementation from the system clock example is explained in this article. If you are good in programming, even you can connect to the internet to get news, stock exchange updates and make them flash in the LCD module, only if you found it important, you can go through it by exiting your application.
  • If you need to modify the code, you need not have to disconnect the circuit or re-program the chip as you do in the case of microcontroller.
  • You need to spend less: One LCD module, DB25 female connector, one potentiometer (optional), and some wires- this is what you need along with a computer.

Interfacing LCD with C#.NET

In above picture LCD connection, LCD module is connected to the lpt port using D25 male connector. The Pin number 3 of the LCD is for adjusting the contrast, connected in such a way that it can be varied from 0V to 5V. Keep it to 0 initially.

If everything is OK, you should get the LCD module as follows when power is switched ON.

If you get this screen, then we can start programming. Otherwise check your connections, try by varying the 10K potentiometer. If you get this display also, you can get maximum clearness by varying the pot.
For us, pot was needed to be nearly 0V. So, it is OK if you don't use pot also, just connect pin 3 to ground.

Following table explains how to write control words. When RS=0 and R/W=0, data in the pins D0 to D7 will have following meaning.

Instruction D7 D6 D5 D4 D3 D2 D1 D0 Description
Clear Display 0 0 0 0 0 0 0 1 Clears Display and returns cursor to home position.
Cursor home
0 0 0 0 0 0 1 X Returns cursor to home position. Also returns display being shifted to the original position.
Entry mode set 0 0 0 0 0 1 I/D S I/D = 0 --> cursor is in decrement position.       I/D = 1 --> cursor is in increment position.
S = 0 --> Shift is invisible.      S = 1 --> Shift is visible
Display ON- OFF Control 0 0 0 0 1 D C B D- Display, C- cursor, B- Blinking cursor
 = 0 --> OFF
 =1 --> ON
Cursor/ Display Shift 0 0 0 1 S/C R/L X X S/C = 0 --> Move cursor.     S/C = 1 --> Shift display.
R/L = 0 --> Shift left.     R/L = 1 --> Shift right
Function Set 0 0 1 DL N F X X DL = 0 --> 4 bit interface. DL = 1 --> 8 bit interface. N = 0 --> 1/8 or 1/11 Duty (1 line). N = 1 --> 1/16 Duty (2 lines). F = 0 --> 5x7 dots.     F = 1 --> 5x10 dots.

I would like to left other instruction related to read and write LCD RAM area, we are going to see them later. Using these information, we are going to write some routines for basic functions of LCD. Please look at our this program below. There I have written functions for all our needs in LCD interfacing. So, For your next programs, we will be to change our "main" function only.

Step by Step

  1. Open Visual Studio .NET Software.
  2. Select menu : File-->New-->Project , and Select "Visual C# Project" on .NET software after that select "Console Application"

  3. Go to the View Code windows .


    using System;
    using System.Runtime.InteropServices;

    namespace ParallelPort
    {
       Private class PortAccess
      {
       Call OutPut function from DLL file.
         [DllImport("inpout32.dll", EntryPoint="Out32")]
         public static extern void Output(int adress, int value);

    
    	#include <dos.h>;
    	#include <char.h>
    	#include <conio.h>
    	
    	#define PortAddress 0x378 /* Here ,Please make sure your printer port address */
    
    	#define DATA PortAddress+0
    	#define STATUS PortAddress+1
    	#define CONTROL PortAddress+2
    // * I wrote a func for init ,write ,putch ,puts,goto,clear,home and entry mode for LCD which are: //
    
    	private void lcd_init(void);
    	private void lcd_write(char char2write);
    	private void lcd_putch(char char2write);
    	private void lcd_puts(char * str2write);
    	private void lcd_goto(int row, int column);
    	private void lcd_clear(void);
    	private void lcd_home(void);
    	private void lcd_cursor(int cursor);
    	private void lcd_entry_mode(int mode);
    
    	static void main()
    	{   
    		Application.Run(new Form1());
    		lcd_init();  	// start LCD function for initial	
    	}
        private void button1_Click(object sender, System.EventArgs e)
        {
    		lcd_goto(1,1);
    		lcd_puts("Welcome To");
    		lcd_goto(1,0);
    		lcd_puts("LCD Wolrd");
    	}
    
    	private void lcd_init()
    	{
    		outportb(CONTROL, inportb(CONTROL) & 0xDF); 
    	//config data pins as output 
    		outportb(CONTROL, inportb(CONTROL) | 0x08);
    	//RS is made high: control (register select)
    		lcd_write(0x0f);
    		delay(20);
    		lcd_write( 0x01);
    		delay(20);
    		lcd_write( 0x38);
    		delay(20);
    	}
    
    	private void lcd_write(char char2write)
    	{
    		outportb(DATA, char2write);
    		outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe */
    		delay(2);
    		outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe */
    		delay(2);
    	}
    	
    	private void lcd_putch(char char2write)
    	{
    		outportb(CONTROL, inportb(CONTROL) & 0xF7);
    	//RS=low: data
    		lcd_write(char2write);
    	}
    
    	private void lcd_puts(char *str2write)
    	{
    		outportb(CONTROL, inportb(CONTROL) & 0xF7);
    	//RS=low: data
    		while(*str2write)
    		    lcd_write(*(str2write++));
    	}
    
    	private void lcd_goto(int row, int column)
    	{
    		outportb(CONTROL, inportb(CONTROL) | 0x08);
    		if(row==2) column+=0x40;
    		lcd_write(0x80 | column);
    	}
    
    	private void lcd_clear()
    	{
    		outportb(CONTROL, inportb(CONTROL) | 0x08);
    		lcd_write(0x01);
    	}
    
    	private void lcd_home()
    	{
    		outportb(CONTROL, inportb(CONTROL) | 0x08);
    		lcd_write(0x02);
    	} 
    
    	private void lcd_entry_mode(int mode)
    	{ 
    		outportb(CONTROL, inportb(CONTROL) | 0x08);
    		lcd_write(0x04 + (mode%4));
    	}
    	
    	private void lcd_cursor(int cursor)
    	{ 
    		outportb( CONTROL, inportb(CONTROL) | 0x08 );
    		lcd_write( 0x0c + (cursor%4));
    	}
    
    

    Press Ctrl + F5 to Complie and Run the porgram on Console Window(Windows is like DOS). That the LCD should be display the "Welcome To" and "LCD Wolrd" word. English is not my first language, so please excuse any mistakes. So any suggestions and/or feedback will be appreciated. Thanks for reading till here.



Copyright (c) 2000 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com