P11: peripheral LCD and C libraries for specialised functions. |
Resources in lectures and labs: | L11, Lab11 | Project | objectives |
Highlighted project: 4-bit serial transmitter with LCD display (phase #2)
1. Specifications | Planning | Dev. & test | Prototype | Report |
Serial transmitter design phase #2: Serial_transmitter_LCD.
Enhance the Serial_transmitter adding a standard LCD display to represent ASCII messages on the two-line 16-character screen. Information can be about the internal state, for example: "Click ST button to start", "Current state: Idle", "End transmission", etc. Full tutorial recording rec.
Modify the hardware to connect the signals to drive the LCD display. Let us use the CSD_PICstick for prototyping.
Program the application using the LCD library of the C compiler (for instance the XC8 Compiler for the PIC18F46K22).
Use a var_LCD_flag signal to write the LCD only when there is new information to be printed; communicating with the LCD is a slow operation that cannot block the FSM main loop.
The new symbol serial_transmitter_LCD is represented in Fig. 1.
|
Fig 1. Adding an external LCD peripheral to the basic 4-bit serial transmitter from P10. It implies adding new connections to the chip, for instance PORTD pins, and new functions in he source file from an external library, therefore, compiling a hierarchical multiple-file project (equivalent to our plan C2 in previous hardware projects). |
The usual display LM016L is presented in Fig. 2.
![]() |
Fig. 2. The LCD controller chip is the standard Hitachi HD44780 (Dot Matrix Liquid Crystal Display Controller/Driver) or equivalent like the HD44100. Some displays include LED background illumination (pin 15, A; pin 16, K). V0 is used to adjust the display contrast by means of a potentiometer. |
![]() |
We can use only a 4-bit bus to send commands or data to the LCD. The low-level communication protocol will be solved using a C language library of functions. It requires up to seven pins for handshaking and data. Optionally, the microcontroller can use another output port pin to switch on and off the LCD power supply to save energy if required.
|
Fig 3. Parallel interface. If only writing to the device is required, signal R/W (RW_L) can be grounded, thus six port pins are necessary for this parallel interface. Datasheets: (1), (2). |
Alternatively, we can use a serial 2-wire interface I2C to attach the LCD to the mC.
|
Fig 4. Serial I2C adapter based on the PCF8574 port expander chip. |
Other design tutorials and assignments.
Specifications | 2. Planning | Dev. & test | Prototype | report |
As in previous projects, draw your circuit in a sheet of paper and discuss where to connect: reset (CD_L or MCLR_L), OSC oscillator, digital I/O and push-buttons. We can use free pins from the CSD_PICstick board and reserve some PORTD pins to connect the LCD.
![]() |
Fig. 5. Connecting RD(5..0) pins to interface the LCD. |
Determine the functions in the main program that have to be modified in order to control the LCD display. Define RAM internal variables to facilitate LCD communication. Use a convenient indicator like var_LCD_flag to prevent writing continuously to the display; this flag is set only when new information has to be shown. For example Fig. 5 shows the hardware-software diagram. Output_logic() generates the variables to interface with LCD high-level functions.
![]() |
Fig. 5. Hardware-software diagram. |
And Fig.6 represents the truth table and its flowchart.
|
Fig 6. Output_logic function. The new variable var_LCD_flag is required to write the LCD only when new information is available. The flowchart must solve the idea. |
Plan a sequence for building and debugging the application: the idea is "plan & develop & test" step by step enhancing the initial state diagram with a new feature at a time. For instance:
Step #1 Start running the copy of P10 project and try to print only the typical ASCII message "Hello World" on the LCD. How long does it take to write a message at the LCD? How can you measure such time? Why a var_LCD_flag is required?
Step #2 Add now the messages you like in FSM states or add new states when necessary.
Step #3 Add numerical information on the display, mixing text and dynamic data, for instance in this example a count of the messages transmitted.
Fig. 7 below is a possible state diagram modified from P10 to allow printing ASCII messages beginning and ending transmissions.
|
Fig. 7. State diagram to print ASCII messages on the LCD. |
Specifications | Planning | 3. Dev. & 4. test | Prototype | Report |
Draw the schematic of the application in Proteus copying the P10 and modifying it accordingly to your new sketch in paper that includes the LCD connections. so that the LCD is made visible on the top schematic. Fig. 8 shows an example circuit Serial_transmitter_LCD.pdsprj.
![]() ![]() |
Fig. 8. Captured circuit in Proteus. Here we have two options for triggering transmissions: a manual ST button or an periodic train of pulses. |
Translate to C language your new flowcharts. Run the microcontroller's IDE MPLABX to develop and compile the C code copying and adapting the previous P10 code. Do it section by section according to your plan, testing whether it works before adding new code. The entity Serial_Transmitter becomes now in this new design phase Serial_Transmitter_LCD.
NOTE: here we have shortened the project folder name, because using the MPLABX tool is easy to reach Windows file names length limit:
C:\CSD\P11\s_trans_LCD\(files)
|
Fig 9. XC8 project options to generate the COF file to run Proteus simulations. |
In Fig. 10 is an example list of source files when using the libraries to interface this specific LCD hardware.
![]() |
Fig. 10. Example of a typical project that includes LCD libraries to drive specific and advanced hardware subsystems (peripherals), thus, becoming a multiple-file project, as a kind of software plan C2 with several components (functions) under the top schematic. |
The current XC8 compiler version is v3.0, C standard is C99. And these LCD library files lcd.c, lcd.h has to be included in the project. The file config.h contains all the microcontroller configuration bits.
This is an example C code translating the planned FSM: Serial_transmitter_LCD.c.
|
Fig 11. Edit the variable _XTAL_FREQ to be of the same value set in the hardware circuit (PIC18F46K22 parameter Processor Clock Frequency). |
Run the Proteus simulator. Do it in step by step mode while watching variables and placing break points, specially to follow the interrupt flags.
![]() |
Fig. 12. Circuit running and showing ASCII messages in the LCD display. |
Learn how to use the One-shot (single) trigger operation mode of the oscilloscope and how the frame of bit is transmitted once the ST button is detected. Can you add a pulse generator to automate ST pulses (20 ms duration, 1.5 Hz) ?
Measure how long does it take to write one row of 16 ASCII characters to the LCD screen.
Specifications | Planning | Dev. & Test | 5. Prototype | Report |
Example 1: CSD_PICstick board. Target chip: PIC18F46K22
Example 2: PICDEM 2 PLUS board. Target chip: PIC18F4520
Example 3: Explorer 8 board. Target chip: PIC18F4520
Example 4: PICDEM 2 PLUS board. Target chip: PIC16F877A
- This example is copied from (1). Thus, you can integrate these libraries into your project and use the LCD functions.
- This obsolete example was planned for HI-TECH C compiler which is no longer in use.
Example 5: Optional, beyond the CSD learning objectives. Training board: Explorer 8 + MCC + PIC18F46K22 + MPLAB X + XC8 + Proteus
Specifications | Planning | Dev. & Test | Prototype | 6. Report |
Follow this rubric for writing reports.
Further discussion and enhancements to this basic circuit:
- How to generate several transmission frequencies?
- How to transmit 8-bit of data?
-- How to add a parity bit to the transmission frame, for instance an odd parity bit?
- How to design a serial receiver? How to build a prototype for both transmitter and receiver modules?
- How the embedded USART peripheral works, so that the same functionality can be implemented in real-world applications liberating the mC of such time-consuming tasks?
You can even imagine different communication channel scenarios:
- How to adapt 0 V and 5 V to the standard RS232?
- How to invent a MODEM (1.2 kHz / 2.4 kHz) to be able to use telephone copper lines?
- How to invent a fiber optic adapter (for instance to implement a kind of HI-FI audio TOSH-LINK) or an infra-red (IR) link for a remote control?
- How to invent a radio link?