How to write C program to display character on LCD interactively | applied electronics engineering

Latest

How to write C program to display character on LCD interactively

By Applied Electronics - Wednesday, June 18, 2014 No Comments
Using combination of three software- Proteus, MPLAB and CCS C compiler, we can create a program in C for a microcontroller and view the character displayed on LCD interactively. This is amazing way of checking whether or not a microcontroller works as per the program.

First we need to select a microcontroller, let's say PIC12F675 and create a schematic diagram in Proteus. Start with a new project in Proteus, with schematic but without any PCB and Firmware and search and put the microcontroller onto the schematic.


As you can see this is a very simple microcontroller with few pins and supporting only most basic functions. This was selected intentionally to keep things simple here. But any microcontroller works the same way.

The next thing would be to plan. Plan what? We need a LCD here and there must be connection between the microcontroller and the LCD. So, let's place an LCD first.


Here too the simplest LCD is used and for illustration purpose it is sufficient. It has just the receive pin, power and ground pins.

Now we know we have to connect these two things together. There are 5 ports available from the microcontroller and we can choose any of them. The choosen one will be the transmit port. Lets select the port named GP0(A0 in the header file). So where does it connect to? This should be connected to the Pin labelled RXD of the LCD.

Connect them

Ok, what about other ports/pins of the microcontroller. The others as far as this simple demonstration is concerned are not required. The microcontroller requires clock pulse from an osciallator but we will use the inbuild internal oscillator. Then we can also choose the reset function but will leave it alone. The only thing we require is to set up the voltage source and ground pins for the microcontroller.

To set those pins, double click the microcontroller and click on hidden pin options and set them to +5VDC and GND as shown by the image below-


Now we are finished for the microcontroller except for loading the program file or the firmware. This will be done after finishing the work with LCD and programming work.

For LCD there is nothing to do more than giving it power supply and grounding its ground pin.


Ok, now thats done we turn to C Programming.

What we want is to write a C code that displays some characters on the LCD.

Start you MPLAB, create a new project, give it some name and maybe it's best if you save the project in the same directory as the Proteus project. But this is not necessary. What is important is that you save the .hex or .cof file with the header file in the same location as the Proteus project. Meaning that the other files generated are not essential once the .hex and .cof are generated. If you used the project wizard, you have to select the PIC12F675 as your device and select CCS C compiler as your compiler program.

Note that to produce the hex file for the microcontroller you need this CCS C compiler. If you have CCS C compiler and don't see the CCS C compiler option as shown below then you need to install the CCS C compiler plugin for MPLAB.


You should see the following screen.


Notice that the four folders on the right side are empty.

Now create a new file(File>New) with some name AND save it with .c extension, for example main.c or program.c.

Now write the following code-

#include "12F675.h"
#use delay(clock = 4M)  //4MHz clock
#use rs232(baud = 9600, xmit = PIN_A0, rcv = PIN_A1)

void main()
    {
        delay_ms(500);    //for LCD to start
      
        while(1)
            {
                putc('A');
                putc('p');
                putc('p');
                putc('l');
                putc('i');
                putc('e');
                putc('d');
                putc(' ');
                putc('E');
                putc('l');
                putc('e');
                putc('c');
                putc('t');
                putc('r');
                putc('o');
                putc('n');
                putc('i');
                putc('c');
                putc('s');

                putc(254);
                putc(192);
                putc('E');
                putc('n');
                putc('g');
                putc('i');
                putc('n');
                putc('e');
                putc('e');
                putc('r');
                putc('i');
                putc('n');
                putc('g');
              
                while(1);
              
            }
      
    }

The include is a preprocessor directive which directs the compiler to look into the content of header file called 12F675.h. Then next is the use directive to specify the clock frequency that the microprocessor will use. Then the next use directive specifies to use the rs232 CSS C function. The argument of this function is the speed given by baud as 9600bits/sec and the transit and receive pins as the A0 and A1(see the schematic above).

The next thing to do is to put this C file(here main.c) into the source file folder. To do this right click the Source Files folder and select Add Files. Browse and select the main.c file.

Similarly we have to add the header file too. To do this, right click on the Header Files folder and select Add Files and browse to the PICC>Devices folder which is usually available in the C:\Program Files>PICC>Devices folder.

After this the folders should show the files as shown below-



Now we are ready to compile the program. To do this click on Project>Build All. If everything is ok then you should get an output message "BUILD SUCCEEDED".

Now finally go to the Proteus schematic editor and double click on the microcontroller. Then click on the folder icon next to the Program file: option and browse to the location of main.hex or main.cof file and upload the file.


Make sure that the Processor Clock Frequency is set to 4MHz.

Click ok and go back to the schematic.

Now Run the simulation by clicking on the run button in the lower left corner.

After few milli-second you should see the following-

The compiled program is now running successfully in the Proteus simulation environment. Now we can tweets to the program to generate other useful things on the LCD.

Other tutorials to check-
Controlling LEDs with C program
Blicking ON/OFF a LED

No Comment to " How to write C program to display character on LCD interactively "