/*  2-line x 16 character LCD Driver software    */
/*  www.newcircuits.com                          */

#include <dos.h>
#include <stdio.h>
#include <string.h>

#define BASE_ADR 0x378            // Printer Port Base Address (0x378 = LPT1)
#define DATA_ADR BASE_ADR + 0     // Data Register of the Port Address (Bi-Directional)
#define STATUS_ADR BASE_ADR + 1   // Status Register of the Port Address (Read Only)
#define CONTROL_ADR BASE_ADR + 2  // Control Register of the Port Address (Bi-Directional)

void main(void)
{

 unsigned char init_sequence[10];
 unsigned char index, ucLength;
 char Line1[17], Line2[17];
 init_sequence[0] = 0x0F;               // Initialize the LCD
 init_sequence[1] = 0x01;               // Clear Screen
 init_sequence[2] = 0x38;               // Define 2-line, 8-bit
 Control_Byte = inportb(CONTROL_ADR);   // Read control register
 Control_Byte &= 0xDF;                  // Reset the 5th bit to zero
 outportb(CONTROL_ADR, Control_Byte);   // Write to control register 
 Control_Byte = inportb(CONTROL_ADR);   // Read control register
 Control_Byte |= 0x08;                  // Set the 4th bit to one
 outportb(CONTROL_ADR, Control_Byte);   // Write to control register
 // Initialize sequence
 for (index = 0; index < 3; index++)
 {
   outportb(DATA_ADR, init_sequence[index]);  // Write the init_sequence byte to data register
   Control_Byte = inportb(CONTROL_ADR);       // Read control register
   Control_Byte |= 0x01;                      // Set EN pin to one (High)
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(5);                                  // 5 mSec Delay
   Control_Byte = inportb(CONTROL_ADR);       // Read control register
   Control_Byte &= 0xFE;                      // Reset EN pin to zero (Low)    
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(5);                                  // 5 mSec Delay
 }
 Control_Byte = inportb(CONTROL_ADR);   // Read control register
 Control_Byte &= 0xF7;                  // Reset the 4th bit to zero 
 outportb(CONTROL_ADR, Control_Byte);   // Write to control register
 
 printf("Enter the first line text:");
 scanf("%16s", Line1);
 printf("Enter the second line text:");
 scanf("%16s", Line2);
 
 ucLength = strlen(Line1);

 for (index = 0; index < uclength; index++)
 {
   outportb(DATA_ADR, Line1[index]);
   Control_Byte |= 0x01;                      // Set EN pin to one (High)
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(2);                                  // 2 mSec Delay
   Control_Byte = inportb(CONTROL_ADR);       // Read control register
   Control_Byte &= 0xFE;                      // Reset EN pin to zero (Low)    
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(2);                                  // 2 mSec Delay
 }
  
 ucLength = strlen(Line2);
  
 for (index = 0; index < uclength; index++)
 {
   outportb(DATA_ADR, Line2[index]);
   Control_Byte |= 0x01;                      // Set EN pin to one (High)
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(2);                                  // 2 mSec Delay
   Control_Byte = inportb(CONTROL_ADR);       // Read control register
   Control_Byte &= 0xFE;                      // Reset EN pin to zero (Low)    
   outportb(CONTROL_ADR, Control_Byte);               // Write to control register
   delay(2);                                  // 2 mSec Delay
 }
  
}
