Code to interface mbed to a Nokia 3310 LCD using the Nokia 3310 LCD shield from nuelectronics. Includes joystick interface and demo.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N3310LCD.cpp Source File

N3310LCD.cpp

00001 /*
00002 * N3310LCD. A program to interface mbed with the nuelectronics
00003 * Nokia 3310 LCD shield from www.nuelectronics.com. Ported from
00004 * the nuelectronics Arduino code.
00005 *
00006 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
00007 *
00008 * This file is part of N3310LCD.
00009 *
00010 * N3310LCD is free software: you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation, either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * N3310LCD is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 *
00020 * You should have received a copy of the GNU General Public License
00021 * along with N3310LCD.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 #include "N3310LCD.h"
00025 #include "N3310Fonts.h"
00026 
00027 N3310LCD::N3310LCD (PinName mosi, PinName miso, PinName sck, 
00028                     PinName ce, PinName dat_cmd, PinName lcd_rst, PinName bl_on) : 
00029                     lcdPort(mosi, miso, sck),
00030                     ceWire(ce), dcWire(dat_cmd), rstWire(lcd_rst), blWire(bl_on)
00031 {
00032 }
00033 
00034 void N3310LCD::init()
00035 {
00036     // use default SPI format
00037     lcdPort.format(8,0);
00038     lcdPort.frequency(1000000);
00039     
00040     // lcd reset
00041     wait_ms(1);
00042     rstWire = 0;
00043     wait_ms(1);
00044     rstWire = 1;
00045     
00046     write(0x21, CMD);    
00047     write(0xc8, CMD);    
00048     write(0x06, CMD);    
00049     write(0x13, CMD);    
00050     write(0x20, CMD);    
00051     cls();            
00052     write(0x0c, CMD);
00053 }
00054 
00055 void N3310LCD::cls()
00056 {
00057     write(0x0c, CMD);            
00058     write(0x80, CMD);            
00059 
00060     for (int i = 0; i < 504; i++)
00061     {
00062         write(0, DATA);
00063     }
00064 }
00065 
00066 void N3310LCD::backlight(eBacklight state)
00067 {
00068     // switch off/on back light
00069     blWire = state;
00070 }
00071 
00072 void N3310LCD::write(BYTE data, eRequestType req_type)
00073 {
00074     // bring CS low for write
00075     ceWire = 0;
00076     
00077     if (CMD == req_type)
00078         dcWire = 0;
00079     else // DATA
00080         dcWire = 1;
00081         
00082     lcdPort.write(data);
00083     
00084     // write finished
00085     ceWire = 1;
00086 }
00087 
00088 void N3310LCD::locate(BYTE xPos, BYTE yPos)
00089 {
00090     write(0x40 | yPos, CMD);      // column
00091     write(0x80 | xPos, CMD);      // row    
00092 }
00093 
00094 void N3310LCD::drawBitmap(BYTE xPos, BYTE yPos, BYTE* bitmap, BYTE bmpXSize, BYTE bmpYSize)
00095 {
00096     BYTE row;
00097     
00098     if (0 == bmpYSize % 8)
00099         row = bmpYSize/8;  
00100     else
00101         row = bmpYSize/8 + 1;
00102     
00103     for (BYTE n = 0; n < row; n++)
00104     {
00105         locate(xPos, yPos);
00106         for(BYTE i = 0; i < bmpXSize; i++)
00107         {
00108             write(bitmap[i + (n * bmpXSize)], DATA);
00109         }
00110         yPos++;                       
00111     }
00112 }
00113 
00114 void N3310LCD::writeString(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
00115 {
00116     locate(xPos, yPos);
00117     
00118     while (*string) 
00119     {
00120         writeChar(*string++, mode);
00121     }
00122 }
00123                   
00124 void N3310LCD::writeStringBig(BYTE xPos, BYTE yPos, char* string, eDisplayMode mode)
00125 {
00126     while (*string)
00127     {     
00128         writeCharBig(xPos, yPos, *string , mode);
00129         
00130         if('.' == *string++)
00131             xPos += 5;
00132         else
00133             xPos += 12;
00134     }
00135 }
00136 
00137 void N3310LCD::writeChar(BYTE ch, eDisplayMode mode)
00138 {
00139     BYTE sendByte;
00140     
00141     unsigned char* pFont = (unsigned char*)font6_8;
00142     ch -= 32;
00143 
00144     for (BYTE line = 0; line < 6; line++)
00145     {
00146         sendByte = *(pFont + ch*6 + line);
00147         write((mode == NORMAL)? sendByte: (sendByte ^ 0xff) , DATA);
00148     }
00149 }
00150 
00151 void N3310LCD::writeCharBig(BYTE xPos, BYTE yPos, BYTE ch, eDisplayMode mode)
00152 {
00153     BYTE sendByte;
00154    
00155     unsigned char* pFont = (unsigned char *) big_number;
00156    
00157     if('.' == ch)
00158         ch = 10;
00159     else if ('+' == ch)
00160         ch = 11;
00161     else if ('-' == ch)
00162         ch = 12;
00163     else
00164         ch = ch & 0x0f;
00165     
00166     for(BYTE i = 0; i < 3; i++)
00167     {    
00168         locate(xPos, yPos + i);
00169  
00170         for(BYTE j = 0; j < 16; j++)
00171         {
00172             sendByte =  *(pFont + ch*48 + i*16 + j);
00173             write((mode == NORMAL)? sendByte : (sendByte^0xff), DATA);
00174         }
00175     }
00176 }