Driver for LCD display Nokia 5110

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

Driver for LCD display Nokia 5110

Post by inaugurator » Thu Jan 01, 2015 2:34 am

Hi,
I try to run display from Nokia 5110 with pyboard, but it does not work. I combined all variants of SPI options polarity, phase and significant bit, but it is unsuccessfully. What I am doing wrong?
Thanks for any help.

Code: Select all

 
# driver for display Nokia 5110 for micropython
# datasheet pcd8544 here: http://ziblog.ru/wp-content/uploads/2013/03/Nokia5110.pdf
#
# memo I/Os
# display     pyboard   state
# DIN    ->   Y7
# CLK    ->   Y6
# RST    ->   Y4        active 0
# CE     ->   Y5        must be 0 or gnd
# DC     ->   Y3        commands 0, data 1
# VCC    ->   3.3V
# LIGHT  ->   Y2        active 0
# GND    ->   GND

import pyb

class Display:
  def __init__(self, spi, rst, ce, dc, light):
    
    # init the SPI bus and pins
    spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)
    rst.init(rst.OUT_PP, rst.PULL_NONE)
    ce.init(ce.OUT_PP, ce.PULL_NONE)
    dc.init(dc.OUT_PP, dc.PULL_NONE)
    light.init(light.OUT_PP, light.PULL_NONE)
    
    # store the pins
    self.spi = spi
    self.rst = rst
    self.ce = ce
    self.dc = dc
    self.light = light
    
    # reset everything
    self.ce.high()
    self.light.high()
    self.rst.low() # A RST pulse must be applied
    pyb.udelay(10)
    self.rst.high()
    
    # example from datasheet p.22
    self.ce.low()
    self.dc.low()
    self.spi.send(b'\x90')
    self.spi.send(b'\x20')
    self.spi.send(b'\x0C')
    self.dc.high()
    self.spi.send(b'\x1F')
    self.spi.send(b'\x05')
    self.spi.send(b'\x07')
    self.spi.send(b'\x00')
    self.spi.send(b'\x1F')
    self.spi.send(b'\x04')
    self.spi.send(b'\x1F')
    self.dc.low()
    self.spi.send(b'\x0D')
    self.spi.send(b'\x80')
    
    
  def light_on(self):
    self.light.low()
    
  def light_off(self):
    self.light.high()
    
  def command(self, buf):
    self.ce.low()
    self.dc.low()
    self.spi.send(buf)
    
  def write(self, buf, X, Y):
    assert X <= 83
    assert Y <= 5
    xbyte = X + 128
    xbyte = xbyte.to_bytes(1)
    ybyte = Y + 64
    ybyte = ybyte.to_bytes(1)
    self.ce.low()
    self.dc.low() # command mode
    self.spi.send(xbyte)
    self.spi.send(ybyte)
    self.dc.high() # data mode
    self.spi.send(buf)
    
#from main.py
#SPI    = pyb.SPI(1)
#RST    = pyb.Pin('Y4')
#CE     = pyb.Pin('Y5')
#DC     = pyb.Pin('Y3')
#LIGHT  = pyb.Pin('Y2')
#dsp = Display(SPI, RST, CE, DC, LIGHT)
Evgeny

inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

Re: Driver for disp. Nokia 5110

Post by inaugurator » Sat Jan 03, 2015 12:18 am

I wrote the script a little bit better, but it still does not work. I attemped to use SPI 1 and 2.
I will try to write soft spi.
My git here: https://github.com/inaugurator/upyd5110

Code: Select all

# -*- coding: utf-8 -*- 
# Micropython board 1.0 library for Nokia 5110 display
# author: Евгений
# license: Apache 2.0
# original: https://github.com/inaugurator/upyd5110
# datasheet: https://www.sparkfun.com/datasheets/LCD/Monochrome/Nokia5110.pdf
# Micropython project: http://micropython.org/
# One data byte is vertical column with LSB on the top.
# SPI mode bits=8, polarity=0, phase=1
# 
# Pins definition
# display     pyboard           state
#
# VCC    ->   3V3 or any Pin    3.3V
# DIN    ->   MISO
# CLK    ->   SCK               0 - 4 MHz
# RST    ->   any Pin           active 0, at start it may be 0
# CE     ->   any Pin           for start transmission 0
# DC     ->   any Pin           commands 0, data 1
# LIGHT  ->   any Pin           active 0
# GND    ->   GND
#
# First side
# SPI    = pyb.SPI(1)
# RST    = pyb.Pin('Y4')
# CE     = pyb.Pin('Y5')
# DC     = pyb.Pin('Y3')
# LIGHT  = pyb.Pin('Y2')
# PWR    = pyb.Pin('Y1')
#
# Second side
# SPI    = pyb.SPI(2)
# RST    = pyb.Pin('X4')
# CE     = pyb.Pin('X5')
# DC     = pyb.Pin('X3')
# LIGHT  = pyb.Pin('X2')
# PWR    = pyb.Pin('X1')

import pyb # module for operation of the periphery

# main class
class Display:
  def __init__(self, spi, rst, ce, dc, light, pwr):
    
    # init the SPI bus and pins
    spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.LSB)
    rst.init(rst.OUT_PP, rst.PULL_NONE)
    ce.init(ce.OUT_PP, ce.PULL_NONE)
    dc.init(dc.OUT_PP, dc.PULL_NONE)
    light.init(light.OUT_PP, light.PULL_NONE)
    pwr.init(pwr.OUT_PP, pwr.PULL_NONE)
    
    # store the pins
    self.spi   = spi
    self.rst   = rst
    self.ce    = ce
    self.dc    = dc
    self.light = light
    self.pwr   = pwr
    
    # light off
    self.lightOff()
    
    # turn on
    self.turnOn() # power on
    self.command(b'\x21') # extended command mode
    self.command(b'\xC8') # for 3.3V power supply
    self.command(b'\x06') # set temperature correction
    self.command(b'\x13') # system bias
    self.command(b'\x20') # basic command mode
    self.command(b'\x0C') # b/w mode, horizontal addressing
    self.clear()
    
  # power on
  def turnOn(self):
    self.pwr.high()
    self.reset()

  # this needs for proper initialization
  def reset(self):
    self.rst.low() # rst active 0
    pyb.udelay(100) # reset impulse have to be > 100 ns and < 100 ms
    self.rst.high() # deactivate rst

  # display have to be blank before power off
  def turnOff(self):
    self.clear()
    self.command(b'\x20') # basic command mode
    self.command(b'\x08') # clear the display
    pyb.delay(10)
    self.pwr.low() # turn off power supply pin
    
  
  def clear(self):
    self.command(b'\x20') # basic command mode
    self.command(b'\x80') # set X address = 0
    self.command(b'\x40') # set Y address = 0
    self.write(b'\x00'*504) # fill RAM with '0's
  
  
  def command(self, buf):
    assert type(buf) == type(bytes()) # check that buf is bytes
    assert len(buf) == 1 # check that buf is one byte
    self.dc.low() # command mode
    self.write(buf) # write command
    self.dc.high() # return to data mode
  
  
  def write(self, buf):
    assert type(buf) == type(bytes()) # check that buf is bytes
    self.ce.low() # start transmission
    self.spi.send(buf) # send with SPI interface
    self.ce.high() # end transmission


  def setXY(self, X, Y):
    assert X <= 83 # X may be 0-83
    assert Y <= 5 # Y may be 0-5
    xbyte = X + 128 # X address is 0b1xxxxxxx
    xbyte = xbyte.to_bytes(1) # convert dex to bin
    ybyte = Y + 64 # Y address is 0b01000xxx
    ybyte = ybyte.to_bytes(1) # convert dex to bin
    self.command(b'\x20') # basic command mode
    self.command(xbyte) # set X address
    self.command(ybyte) # set Y address
    
  
  def lightOn(self):
    self.light.low() # active 0


  def lightOff(self):
    self.light.high() # inactive 1
Evgeny

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Driver for disp. Nokia 5110

Post by pfalcon » Sun Jan 04, 2015 3:03 pm

You surely have a working display, tested for e.g. with Arduino?
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

Re: Driver for LCD display Nokia 5110

Post by inaugurator » Sun Jan 04, 2015 7:03 pm

No, I haven't something like Arduino, only pyboard. I can't check it, but a display just from store. May be someone have this display and may check my library.
Evgeny

inaugurator
Posts: 23
Joined: Tue Sep 30, 2014 4:02 pm

Re: Driver for LCD display Nokia 5110

Post by inaugurator » Sun Jan 04, 2015 8:19 pm

Maybe I have a broken display. I don't want spend time for it anymore.
Evgeny

mikewop
Posts: 3
Joined: Mon Feb 02, 2015 2:21 am

Re: Driver for LCD display Nokia 5110

Post by mikewop » Mon Feb 02, 2015 2:32 am

Hi,

I see you have listed in the code comments:

Code: Select all

# DIN    ->   Y7
and

Code: Select all

# DIN    ->   MISO
DIN needs to go to MOSI (Master Out - Slave In) since the data flows from the PyBoard (Master) to the Nokia LCD (slave).

So the MOSI pin would be Y8 (SPI2) or X8 (SPI1).

One other thing, it seems you want to drive the Display power supply voltage directly from a pyBoard Pin.

Code: Select all

# PWR    = pyb.Pin('Y1')
Try connecting the display VCC directly to 3.3V on the pyBoard (3V3) first. With backlight the display draws around 10mA, and I am not sure the CPU pin can deliver that much current at a sufficient voltage.

Hope that helps!

Mike

mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Re: Driver for LCD display Nokia 5110

Post by mischko » Tue Feb 03, 2015 12:54 am

i'm also trying to get this to work. So far with no go.
I did find that the lightOn and lightOff are reversed.

hansel
Posts: 13
Joined: Wed Feb 11, 2015 7:34 pm
Location: Rotterdam, NL
Contact:

Re: Driver for LCD display Nokia 5110

Post by hansel » Tue Apr 07, 2015 8:33 pm

I came across this thread, maybe the link below can help somebody...

I've no time soon to look into this myself but I found sample code to control this display over SPI. Maybe it can be ported to Python?
The display is indeed nice and cheap! (I've used in in and Arduino project)

page: https://www.olimex.com/Products/Modules ... e-hardware
code: https://www.olimex.com/Products/Duino/S ... 10_SPI.zip

Cheers, Hans

User avatar
mbirth
Posts: 25
Joined: Mon Oct 20, 2014 1:00 pm
Location: Berlin, Germany
Contact:

Re: Driver for LCD display Nokia 5110

Post by mbirth » Fri Nov 06, 2015 12:45 am

I just received those displays from China to play around with my WiPy. I hope I'll find some time on the weekend. What I noticed from reading the datasheet: The first bit is the MSB, not the LSB like in your code. Also, data is sampled on the positive edge of SCLK, so I believe phase needs to be 0 instead of 1.
pyBoard v1.0 + LCD32MKv1.0 | WiPy + Expansion Board | GitHub

User avatar
mbirth
Posts: 25
Joined: Mon Oct 20, 2014 1:00 pm
Location: Berlin, Germany
Contact:

Re: Driver for LCD display Nokia 5110

Post by mbirth » Sat Nov 14, 2015 10:31 pm

pyBoard v1.0 + LCD32MKv1.0 | WiPy + Expansion Board | GitHub

Post Reply