LCD2 Example



µC/51 C Compiler

Program DemoLCD is a complete terminal emulator written in BASCOM 8051, BASCOM AVR et µC/51 to work on a K51-AVR board connected to a 20x2 LCD display and an I/O device on the serial port.
Like any other terminal, the program shows to the screen (in this case the LCD display) every data it receives from the serial port and sends to the serial port every data it receives from the keyboard (in this case the K51-AVR keys).
Differently from any other terminal, the program can execute terminal emulation commands in a completely programmable way.

The label Main: denotes the program main cycle which, as stated above, as first thing selects the serial port as input and the LCD display as output, checks for the presence of a character and, if present, sends it to the display through the filtering procedure, as second thing it reverses the situation, that is inputs from K51-AVR keys and outputs to the serial port then repeats the above described process, but the characters output to the serial port are not filtered.

The label Scan: denotes the beginning of the periodic interrupt response procedure determined by Timer 0, the period is 20 msec. The first thing to do is always to reload the timer and restart it, to keep the period as sharp as possible. The program scans one by one the input sources (serial port and K51-AVR keys), saving into the memory buffers the characters eventually available. Using this system the probability to lose characters in input when the program is busy, for example, in output operations or other kinds of operations decreases drastically. For a more detailed description please refer to the comments in the source code.

To select which input and output devices to employ and use the procedure here described the opportune values must be written into the variables Indev and Outdev. These values can be found at the bottom of the constant declaration section in the source code.

The procedures are:

Init, which initializes all the variables used by the program and starts Tmer 0.

Constatus, which checks whether the device selected as Input has received a character and has stored it in the buffer.
No input character can be lost because the Interrupt response procedure always intercepts it and stores it in the buffer, but if the buffer is not read before overflowing its size some problems may happen.

Conin, which fetches one character from the buffer of the device selected as Input source and returns it through the variable In, if no new character is available it returns 0. However this procedure returns 0 also if the character whose code is 0 has been received; to distinguish the events Constatus should be called before Conin.

Conout, which sends to the device selected as Output the character passed to it as parameter. If the output device is the LCD display then the character is filtered, as described in the following lines, through procedure Filerlcd.

The following ones are service procedures that are not made to be called directly but only from other procedures:

Bip, which generates a shrill sound lasting one tenth of a second using the K51-AVR on board buzzer. During its execution the interrupt keeps on receiving the input so no character is lost.

Bell, which generates a shrill sound lasting half of a second using the K51-AVR on board buzzer. During its execution the interrupt keeps on receiving the input so no character is lost.

Adjustxy, which warrants that the variables Row and Column keep values in the range of a 20 columns x 2 rows display.
This procedure is needed because the high level commands of BACOM 8051 do not manage the carriage return of the LCD cursor when it reaches the end of its current line, so the program must do this, intercepting all the situations where the cursor location is changed.

Filterlcd, which filters the characters that are sent to the LCD display deciding whether they are printable or must be interpretated as one kind of command. To modify the set of printable characters it is enough to change the Select Case on top of the Filterlcd code (denoted by the comment "IMMEDIATE COMMANDS", in addition to match the execution of specific procedures to the reception of certain characters the same Select Case must be changed.
One special character is considered as prefix, that is a character announcing the next character coming as a command code, this character doesn't execute its own code and can be configured by changing the value of the constant Prefix.
A command is made of a code and a sequence of parameters (that is a sequence of characters) whose length can be from zero to the limit due to memory still available.
Commands with no parameters execute the code they are matched with as soon as they are recognized. To see an example, please refer to the source code denoted by the comment "COMMAND WITHOUT PARAMETERS"
Commands with one or more parameters take advantage of the parameters gathering mechanism (stores every character received until the last parameter is received, its code is denoted by the comment "GATHER PARAMETERS", some of these commands con be found near the comment "COMMANDS WITH PARAMETERS". Execution of the code matched with these commands starts as soon as the last parameter has been gathered, their code is written after the comment "EXECUTION COMMANDS WITH PARAMETERS".

Page up-dated at September 10, 2003

homeHome Cards Categorical Index Tables I/O Data. S. Examples Prog.