N3310LCD

.:Intro:.

N3310LCS provides a means to interface the mbed to a Nokia 3310 LCD. This uses the Nokia 3310 LCD shield/board (originally for the Arduino) from nuelectronics (www.nuelectronics.com). The nuelectronics site also has the circuit diagram for the LCD shield, so if you have a spare 3310 ripped out of an old phone, you can build one yourself.

The code is a port/rewrite of the Ardunio code originating from the nuelectronics site, adapted for the mbed. The code also handles the LCD shield's joystick and includes a menu driven demo.

.:How To Use:.

Connections:

The header file N3310LCDSPIConfig.h has the pin mappings between the LCD shield board (including joystick) and the mbed.

// NOTE pins have been chosen not to conflict with any I2C usage. 
// MOSI = p5, MISO = p6, SCK = p7 is also an option
const PinName N3310SPIPort::MOSI = p11;
const PinName N3310SPIPort::MISO = p12;   // not used for 3310
const PinName N3310SPIPort::SCK = p13;

const PinName N3310SPIPort::CE = p21;
const PinName N3310SPIPort::LCD_RST = p22;
const PinName N3310SPIPort::DAT_CMD = p23;
const PinName N3310SPIPort::BL_ON = p24;

const PinName N3310SPIPort::AD0 = p20;    // joystick analog

/************************************************
*
* Nokia 3310 LCD Shield Pins
* NOTE: the LCD shield must be powered from a 3.3V supply in order
* for the joystick to be read correctly by the mbed analog in
* (which operates on a range of 0 - 3.3V).
*
* Connector J3:
* p13: SCK
* p12: MISO (not used)
* p11: MOSI
* p10: CE
* p9: LCD_RST
* p8: DAT_CMD
* 
* Connector J1:
* p7: BL_ON
*
* Connector J2:
* p1 : AD0
*
**************************************************/

To summarise the mapping:

mbed  p11 <-> J3 p11  LCD shield
      p12 <->    p12
      p13 <->    p13
      p21 <->    p10
      p22 <->    p9
      p23 <->    p8
      p24 <-> J1 p7
      p20 <-> J2 p1
      GND <-> J4 p4
      VOUT <-> J4 p3 (if not using backlight)

NOTE: The 3310 LCD shield can be powered from 3.3V to 5.0V. However the mbed analog in, which is used to read the joystick, only handles 0.0 - 3.3V. So the LCD shield needs to be powered using a 3.3V supply for the joystick to operate correctly (using 5V the right hand position is lost). Due to the power consumption for the backlights I use a seperate power supply. If you don't connect the back lights (p24 mbed), you can drive it using the mbed VOUT.

Demo:

The demo menu gives access to the following using the joystick:

Joystick down moves down one line, up moves up one line, left resets to top line, right selects the menu item and opens a screen. Depressing the centre operates OK in a screen closes the screen and returns to the menu.

Usage:

The best way to adapt N3310LCD for your own use is to study the demo (main.cpp). The display functionality is provided by the N3310LCD class (N3310LCD.h). The demo shows how to use the joystick, with the joystick (Joystick.h) being polled every 10ms using an mbed Ticker.

#include "N3310SPIConfig.h"
#include "N3310LCD.h"
#include "Joystick.h"
#include "mbed_bmp.h"

// demo for nuelectronics Nokia 3310 LCD shield (www.nuelectronics.com)
// 

// menu starting points
#define MENU_X    10        // 0-83
#define MENU_Y    1        // 0-5

#define DEMO_ITEMS 4

// menu definition
char menu_items[DEMO_ITEMS][12] =
{
    "TEMPERATURE",
    "CHAR MAP",
    "BITMAP",
    "ABOUT"    
};

void temperature(N3310LCD* lcd)
{
    lcd->writeStringBig(5, 1, "+21.12", NORMAL);
    lcd->writeString(73, 2, "C", NORMAL);
}

void charmap(N3310LCD* lcd)
{
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 14; j++)
        {
          lcd->locate(j*6, i);
          lcd->writeChar(i*14 + j + 32, NORMAL);
        }
    }
}

void bitmap(N3310LCD* lcd)
{
    lcd->drawBitmap(20, 1, mbed_bmp, 48, 24);
}

void about(N3310LCD* lcd)
{
    lcd->writeString(0, 1, "Nokia 3310 LCD", NORMAL);
    lcd->writeString(15, 2, "driven by", NORMAL);
    lcd->writeString(30, 3, "mbed", NORMAL);
}

void (*menu_funcs[DEMO_ITEMS])(N3310LCD*) = 
{
    temperature,
    charmap,
    bitmap,
    about
};

void initMenu(N3310LCD* lcd)
{
    lcd->writeString(MENU_X, MENU_Y, menu_items[0], HIGHLIGHT );
    
    for (int i = 1; i < DEMO_ITEMS; i++)
    {
        lcd->writeString(MENU_X, MENU_Y + i, menu_items[i], NORMAL);
    }
}

void waitforOKKey(N3310LCD* lcd, Joystick* jstick)
{
    lcd->writeString(38, 5, "OK", HIGHLIGHT );

    int key = 0xFF;
    while (key != CENTER_KEY)
    {
        for (int i = 0; i < NUM_KEYS; i++)
        {
            if (jstick->getKeyState(i) !=0)
            {
                jstick->resetKeyState(i);  // reset
                if (CENTER_KEY == i) key = CENTER_KEY;
            }
        }
    }
}

void autoDemo(N3310LCD* lcd)
{
    while (true)
    {
        for (int i = 0; i < DEMO_ITEMS; i++)
        {
            lcd->cls();
            lcd->backlight(ON);
            wait(1);
                
            (*menu_funcs[i])(lcd);
        
            wait(3);
        
            lcd->backlight(OFF);
            wait(3);
        }
    }
}

int main() 
{
    Joystick jstick(N3310SPIPort::AD0);
    N3310LCD lcd(N3310SPIPort::MOSI, N3310SPIPort::MISO, N3310SPIPort::SCK,
                 N3310SPIPort::CE, N3310SPIPort::DAT_CMD, N3310SPIPort::LCD_RST,
                 N3310SPIPort::BL_ON);
    lcd.init();
    lcd.cls();
    lcd.backlight(ON);
    
    // demo stuff
    // autoDemo(&lcd);
    
    initMenu(&lcd);
    int currentMenuItem = 0;
    Ticker jstickPoll;
    jstickPoll.attach(&jstick, &Joystick::updateADCKey, 0.01);    // check ever 10ms
    
    
    while (true)
    {
    for (int i = 0; i < NUM_KEYS; i++)
    {
        if (jstick.getKeyState(i) != 0)
        {    
            jstick.resetKeyState(i);  // reset button flag
            switch(i)
            {
                case UP_KEY:
                    // current item to normal display
                    lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
                    currentMenuItem -=1;
                    if (currentMenuItem <0)  currentMenuItem = DEMO_ITEMS -1;
                    // next item to highlight display
                    lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
                    break;
                    
                case DOWN_KEY:
                    // current item to normal display
                    lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], NORMAL);
                    currentMenuItem +=1;
                    if(currentMenuItem >(DEMO_ITEMS - 1))  currentMenuItem = 0;
                    // next item to highlight display
                    lcd.writeString(MENU_X, MENU_Y + currentMenuItem, menu_items[currentMenuItem], HIGHLIGHT);
                    break;
                    
                case LEFT_KEY:
                    initMenu(&lcd);
                    currentMenuItem = 0;
                    break;
                    
                case RIGHT_KEY:
                    lcd.cls();
                    (*menu_funcs[currentMenuItem])(&lcd);
                    waitforOKKey(&lcd, &jstick);
                    lcd.cls();
                    initMenu(&lcd);
                    currentMenuItem = 0;
                    break;    
            }        
        }
    }
    }    
         
    return EXIT_SUCCESS;
}

.:Library Files:.

Joystick.h - Joystick class declaraton

Joystick.cpp - Joystick source

N3310Fonts.h - font arrays for regular characters and 'big' numbers

N3310LCD.h - N3310LCD class declaration

N3310LCD.cpp - N3310LCD source

N3310LCDDefs.h - enumerations and typedefs

N3310SPIConfig.h - SPI port and pins

N3310LCD


4 comments

10 Feb 2011
Hola, Estoy trabajando con su programa, gracias por compartirlo, ¿no se si acepta peticiones?, podría variar el código del Joystick para que sea por cinco pulsadores, uno para cada dirección y otro para aceptar, Gracias Hello, I am working with your program, thanks for sharing, do not you if you accept requests?, may vary Joystick code to be five buttons, one for each direction and another to accept Thanks
19 Feb 2011

Have you tried using J5 for the connector? I'm having no luck with getting it to put any data on the screen :(

14 Aug 2011 . Edited: 14 Aug 2011

Hiya,

Hope you all enjoying yer MBED. I am.sure beats studying a 500 page PIC data sheet!

All my PIC's are now in de bin.

Used J5 on display to make all connections from LCD to EMBED, except RST (P22 on MBED) which is connected to  J4 (RESET) Works  now

not sure if 5 volts is gud idea as I have always operated my 3310's from lithium 3.6 volts.

Cheers

RG

15 Aug 2011

To reduce contrast:

in file N3310LCD.cpp  line 47

write (0xC1,CMD);

Please log in to post a comment.