11.3 PROJECT 11.3 – Counting with the LCD

In this project, we will see how to display numbers in addition to text on the LCD. Here, an up-counter will be designed that will count with 1 second intervals. The count will be displayed as follows:

img

11.3.1 Block Diagram

The block diagram of the project is as in Figure 11.1.

11.3.2 Circuit Diagram

The circuit diagram of the project is as in Figure 11.2.

11.3.3 Project PDL

The PDL of this project is very simple and is given in Figure 11.8.

Figure 11.8 PDL of the project

img

11.3.4 Project Program

The program is named LCD3.C and the program listing of the project is shown in Figure 11.9. At the beginning of the project, the connections between the microcontroller and the LCD are defined using sbit statements and the counter variable, Cnt, is initialised to 0. In the main program, PORT B is configured as a digital output port. The LCD library is initialised, LCD is cleared, and the cursor is turned OFF. Then an endless loop is formed using a for statement. Inside this loop, the counter variable Cnt is incremented by one. This variable is then converted into a string using the mikroC Pro for PIC built-in library function ByteToStr. Here, the numeric variable is the first argument, and address of a character array, where the converted string will be stored, is the second argument. The character array must have a size of at least 4 bytes. The converted string equivalent of the count is stored, starting from address 5 of array Txt as follows:

Figure 11.9 Program listing of the project

img

img

img

Thus, for example, number 1 is displayed as Cnt = 1, where there are two spaces before the actual number to be displayed. The program then waits for 1 second and the above process is repeated.

11.3.5 Suggestions for Further Development

As shown above, the function ByteToStr inserts leading spaces when it converts a number into a string. In some applications, we may want to remove these spaces, and for example, display number 1 as:

img

This can easily be done by using the library function Ltrim to remove leading spaces. The main part of the modified program listing is shown in Figure 11.10. Here, the count is converted into string and stored in character array pointed to by Txt1. Leading spaces are then removed with the Ltrim function and the new array is pointed to by Txt2. Library function strcat appends two strings, whose addresses are supplied as arguments. The string whose address is the second argument is appended to the string whose address is the first argument. The first string is not changed up to the point where the Null character is located, and the second string is appended, starting from this point. In Figure 11.10, the Null character is set at position 5 of the first string (i.e. at the point Cnt =), so that numbers are displayed with no leading zeroes.Variable res is a pointer to the appended string.

Figure 11.10 Modified program listing

img
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.63.138