Using DMA Controller – Interfacing PIC18F46K42 with NOKIA LCD

This post shows how to configure and use Microchip PIC18F46K42 8-bit microcontroller integrated DMA (Direct Memory Access) module.
This example shows how to use the DMA module of the MCU to send screen data buffer to NOKIA 5110 LCD via SPI interface. The PIC compiler used in this example is CCS C.

The DMA controller allows data transfer between memory regions (GPR/SFR, PFM, EEPROM) directly without intervention from the CPU (Central Processing Unit), therefor the CPU can spend more time on other tasks.

The PIC18F46K42 microcontroller has two independent DMA modules, in this project one DMA module is used to transfer display data buffer which is stored in MCU GPR (General Purpose Register) to SPI (Serial Peripheral Interface) module transmit buffer SFR (Special Function Register).

The name Nokia 5110 (3310) LCD comes from Nokia 5110 (or Nokia 3310) mobile phone. The Nokia 5110 LCD contains the PCD8544 controller, it is similar to the one used in Nokia 5110 mobile phone, it uses SPI interface protocol with maximum clock frequency of 4 MHz, it requires 5 control pins (at most), it’s low cost and easy to use.

The resolution of this LCD is 84 x 48 which means it has 4032 pixels. This module works with 3.3V only and it doesn’t support 5V (not 5V tolerant), this means interfacing it with 5V microcontroller such as PIC18F46K42 MCU requires voltage level shifter.

The Nokia 5110 LCD screen is shown below:

Nokia 5110 LCD module

This module has 8 pins (from left to right): RST (reset), CE (chip enable), DC (or D/C: data/command), Din (data in), Clk (clock), VCC (3.3V), BL (back light) and Gnd (ground).
The pins which should be connected to the microcontroller are: RST, CE, DC, Din and Clk.

Hardware Required:

  • PIC18F46K42 microcontroller   —->  datasheet
  • Nokia 5110 (3310) LCD module
  • AMS1117-3V3 voltage regulator
  • 22 uF capacitor
  • 10 uF capacitor
  • 3 x 100 nF ceramic capacitor
  • 5 x 2k resistor
  • 5 x 1 resistor
  • 5V source
  • Breadboard
  • Jumper wires
  • PIC programmer (PICkit 3, PICkit 4…)

Interfacing PIC18F46K42 MCU with Nokia 5110 LCD circuit:
The following image shows example simplified circuit schematic diagram (click on the image for better view)..

The Nokia 5110 which is shown in the circuit diagram has 8 pins (from left to right): RST (reset), CE (chip enable), DC (or D/C: data/command), Din (data in), Clk (clock), VCC (3.3V), BL (back light) and Gnd (ground).

Interfacing PIC18F46K42 MCU with NOKIA 5110 LCD - DMA to SPI data transfer

The main power source voltage of the circuit is 5V, it’s connected to connector J1 (5V Input connector) where pin 1 is negative and pin 2 is positive.

The Nokia 5110 LCD works with 3.3V only (power supply and control lines). The LCD module is supplied with 3.3V from the AMS1117-3V3 voltage regulator (U1) output, this regulator converts  5V input into 3.3V (supplies the LCD controller PCD8544 with regulated 3V3).

All PIC18F46K42 microcontroller output pins are 5V, connecting a 5V pin directly to the Nokia 5110 LCD may damage its controller.
To connect the PIC18F46K42 to the LCD module, I used voltage divider for each line. This means there are 5 voltage dividers, each one consists of 1k and 2k resistors, this drops control voltage from 5V into 3.3V.

Actually, choosing the values of voltage divider resistors depends on many factors such as: maximum frequency, pin capacitance … but 1k & 2k will do the job as it should be, however, using a 3.3V microcontroller is recommended when dealing with 3.3V devices.

So, the Nokia 5110 LCD pins are connected to PIC18F46K42 MCU as follows (each one through voltage divider):
RST (reset) pin is connected to pin RD0 (#19),
CE (chip enable) pin is connected to pin RD1 (#20),
DC (data/command) pin is connected to pin RD2 (#21),
DIN (data in)  pin is connected to pin RC4 (#23),
CLK (clock) pin is connected to pin RC5 (#24).

VCC and BL are connected to AMS1117-3V3 regulator output pin and GND is connected to circuit ground (0V).

In this project the PIC18F46K42 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.

Interfacing PIC18F46K42 MCU with Nokia 5110 LCD using DMA controller C code:
The following C code is for CCS C compiler, it was tested with versions 5.104.

To be able to compile project C code with no error, 2 libraries are required:
The first library is a driver for the Nokia 5110 LCD (PCD8544 controller), its full name (with extension) is PCD8544.c, download link is below:
Nokia 5110 LCD library for CCS C compiler

The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for CCS C compiler

after the download of the 2 libraries, add both files to project folder.

Hints:
The 2 library files are included in the main code as shown below:

Connection of LCD pins with the microcontroller are defined as:

The PIC18F46K42 DMA module is initialized using the function DMA_Initialize(), and the LCD is updated (refreshed) whenever the function DMA_Transfer() is called. Calling the last function starts sending the data buffer pcd8544_buffer to the display using DMA module by setting the bit: SIRQEN, when all data are transferred this bit is cleared. Once data transfer is completed, DMA1 Interrupt is called and the LCD is de-selected by rising the CS pin (RD1).

Note that CCS C compiler already has functions that can initialize the DMA module and content of the function DMA_Initialize() can be replaced by the following:

Instead of CCS C compiler above functions I used to set manually each DMA register because its more easier to relate the settings with device’s datasheet.

The DMA module is configured such as source start address is the address of pcd8544_buffer[0] and destination start address is SPI1 module transmit buffer register SPI1TXB.
The source start address has to be incremented after each transfer in order to transfer all bytes located in the pcd8544_buffer array. In the other hand the destination address remains unchanged after each transfer as we want to transfer all data through the same SPI module.

After each transfer, SPI1TXB interrupt triggers the DMA controller for another byte transfer until the whole data buffer pcd8544_buffer transfer is completed, the size of the data buffer is 504 bytes. Data transfer is initialized by setting the bit SIRQEN and is automatically cleared once all data transfer complete.

Rest of code is described through comments.

Full CCS C code:

The following short video shows my DIY circuit :

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top