Writing Software To Contol The LCD Display

My first program in C wrote the instruction to turn the cursor on and then write the character "e" a few times. It started by setting the display to instruction mode. The number 15 was sent as data which corresponds to the instruction to turn the cursor on. Next the display was set to data mode and the character "e" was written a few times. It took some experimentation to alternate the read/write enable properly with the software.

In the pictures you can see on my first attempt the alternation of the read/write enable wasn't in the right order, resulting in the space character being written before the e's. That was a result of the value of the instruction to turn the cursor on being taken as input for data, after the cursor on instruction and before writing the actual data to the display.

Once I figured out the read/write enable usage, I wrote a standard program that turned on the cursor and then wrote hello world to the display.

Next I tested the program with a 32 characters to make sure the display worked properly. I found that I had to write an instruction to the display to change it to 2 line mode. Also each line has a 40 character buffer, so line 1 would have to be padded from character 17 to 40 before printing line 2

The program was modified to read it's input from a text file containing exactly 32 ascii characters called "displayinput". It's source is here: display.c

 

/* display.c
 * Author: Dan Burke
 * CSC 494 Project - An Interactive Internet LCD Display
 * compile with gcc -o display display.c
 * Give the compiled program "display" execute permissions and place it
 * in the root directory of the user that will be running jakarta.
 * The program reads it's input from a text file called
 * "displayinput" that contains exactly 32 ascii characters to write to
 * the display.
 */
#include <stdio.h>
#include <asm/types.h>
#include <asm/io.h>
#include <asm/segment.h>
#include <linux/module.h>
#include <linux/param.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <unistd.h>

#define LP_BASE 0x378
#define DATA LP_BASE+0
#define CONTROL LP_BASE+2


int main(int argc, char *argv[]){

        int y;
        
        /* The data to write to the display */
        FILE *inputfile = fopen("displayinput", "r");
        
        char ch;
        char space = ' ';
        
        /* Reserve space in i/o for the parallel port */
        if (ioperm(LP_BASE, 3, 1) < 0){
                fprintf(stderr, "Cannot reserve i/o space\n");
        }

        /* Change the display to instruction mode */
        outb(0x09, CONTROL);
        
        /* Write 15 to the display, the instruction to turn on the cursor */
        outb(0x0f, DATA);
        usleep(20);

        /* Now alternate to the read/write enable to write the instruction to
         * the display
         */
        outb(0x08, CONTROL);
        usleep(20);
        outb(0x09, CONTROL);
        
        /* Change the instruction to clear the display */
        outb(0x01, DATA);

        /* Ensure the display is in instruction mode */
        outb(0x09, CONTROL);
        usleep(20);

        /* Now alternate the read/write enable to write the instruction to 
         * the display 
         */
        outb(0x08, CONTROL);
        usleep(20);
        outb(0x09, CONTROL);

        /* Change the instruction to set the display to 2 line mode */
        outb(0x38, DATA);
        outb(0x09, CONTROL);
        usleep(20);

        /* Alternate the read/write enable to write the instruction */
        outb(0x08, CONTROL);
        usleep(20);
        outb(0x09, CONTROL);        
        
        /* Read the first 16 characters and write them to the display. */
        ch = getc(inputfile);
        for(y = 0; y < 16; y++){
                outb(ch, DATA);
                outb(0x00, CONTROL);
                usleep(2);
                outb(0x01, CONTROL);
                usleep(2);
                ch = getc(inputfile);
                if (ch == -1)
                        break;
        }
        if (ch == -1){
                exit(0);
        }

        /* Since the display buffer for each line is 40 characters,
         * write a space character to characters 17-40 to move to line
         * 2.
         */
        for(y = 16; y < 40; y++){
                outb(space, DATA);
                outb(0x00, CONTROL);
                usleep(2);
                outb(0x01, CONTROL);
                usleep(2);
        }

        /* Now read characters 17-32 from the file and write them to
         * line 2 of the display.
         */
        for (y = 0; y < 16; y++){
                outb(ch, DATA);
                outb(0x00, CONTROL);
                usleep(2);
                outb(0x01, CONTROL);
                usleep(2);
                ch = getc(inputfile);
                if (ch == -1)
                        break;
        }        
        
        /* Release the i/o space for the parallel port. */
        if (ioperm(LP_BASE, 3, 0) < 0){
                printf("Error releasing\n");
        }


        return 0;
}


Successfully Sending Data To The Display


Successfully Sending Data To The Display


The C Program


Successfully Sending Data To The Display

Back to the index