V1.0: A simple lib to driver the nokia 5110 lcd. You can select the soft/hardware spi ports by define the HW_SPI word from the "sx5110.h ".

Committer:
shower_xu
Date:
Thu Jan 08 08:04:43 2015 +0000
Revision:
0:ab1ca9a3e847
V1.0: This lib can driver the nokia 5110 lcd by soft-SPI or hardware-SPI; Just need define the HW_SPI word in the sx5110.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shower_xu 0:ab1ca9a3e847 1 #include "SX5110.h"
shower_xu 0:ab1ca9a3e847 2 #include "mbed.h"
shower_xu 0:ab1ca9a3e847 3
shower_xu 0:ab1ca9a3e847 4
shower_xu 0:ab1ca9a3e847 5 /*******************************************
shower_xu 0:ab1ca9a3e847 6 lcd-IO初始化
shower_xu 0:ab1ca9a3e847 7 ********************************************/
shower_xu 0:ab1ca9a3e847 8 Lcd5110::Lcd5110(LcdPins pinout)
shower_xu 0:ab1ca9a3e847 9 {
shower_xu 0:ab1ca9a3e847 10 // SPI
shower_xu 0:ab1ca9a3e847 11 #ifdef HW_SPI
shower_xu 0:ab1ca9a3e847 12 LcdSpi = new SPI(pinout.mosi, NC, pinout.sclk);
shower_xu 0:ab1ca9a3e847 13 LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE);
shower_xu 0:ab1ca9a3e847 14 LcdSpi->frequency(LCD_FREQ);
shower_xu 0:ab1ca9a3e847 15 #else
shower_xu 0:ab1ca9a3e847 16 SPins = new DigitalOut*[2];
shower_xu 0:ab1ca9a3e847 17 SPins[PIN_MOSI] = new DigitalOut(pinout.mosi);
shower_xu 0:ab1ca9a3e847 18 SPins[PIN_SCLK] = new DigitalOut(pinout.sclk);
shower_xu 0:ab1ca9a3e847 19 #endif
shower_xu 0:ab1ca9a3e847 20 // Control Pins
shower_xu 0:ab1ca9a3e847 21 Pins = new DigitalOut*[3];
shower_xu 0:ab1ca9a3e847 22 Pins[PIN_RST] = new DigitalOut(pinout.rst);
shower_xu 0:ab1ca9a3e847 23 Pins[PIN_SCE] = new DigitalOut(pinout.sce);
shower_xu 0:ab1ca9a3e847 24 Pins[PIN_DC] = new DigitalOut(pinout.dc);
shower_xu 0:ab1ca9a3e847 25
shower_xu 0:ab1ca9a3e847 26 }
shower_xu 0:ab1ca9a3e847 27 /*******************************************
shower_xu 0:ab1ca9a3e847 28 lcd-初始化
shower_xu 0:ab1ca9a3e847 29 ********************************************/
shower_xu 0:ab1ca9a3e847 30 void Lcd5110::InitLcd()
shower_xu 0:ab1ca9a3e847 31 {
shower_xu 0:ab1ca9a3e847 32 ResetLcd();
shower_xu 0:ab1ca9a3e847 33 #ifdef HW_SPI
shower_xu 0:ab1ca9a3e847 34 Pins[PIN_SCE]->write(1); // Chip Select goes low
shower_xu 0:ab1ca9a3e847 35 LcdSpi->format(LCD_SPI_BITS, LCD_SPI_MODE);
shower_xu 0:ab1ca9a3e847 36 LcdSpi->frequency(LCD_FREQ);
shower_xu 0:ab1ca9a3e847 37 Pins[PIN_SCE]->write(0); // Chip Select goes low
shower_xu 0:ab1ca9a3e847 38 #endif
shower_xu 0:ab1ca9a3e847 39 SendCmd(0x21);//使用扩展命令设置LCD模式chip is active & horizontal addressing (H=1)
shower_xu 0:ab1ca9a3e847 40 SendCmd(0xc0);//设置VOP值lcd 电压
shower_xu 0:ab1ca9a3e847 41 SendCmd(0x20);//使用基本命令,水平寻址 (H=0)
shower_xu 0:ab1ca9a3e847 42 SendCmd(0x0c);//设定显示模式,正常显示display in normal m
shower_xu 0:ab1ca9a3e847 43 clear();
shower_xu 0:ab1ca9a3e847 44 }
shower_xu 0:ab1ca9a3e847 45 /*******************************************
shower_xu 0:ab1ca9a3e847 46 lcd-reset
shower_xu 0:ab1ca9a3e847 47 ********************************************/
shower_xu 0:ab1ca9a3e847 48 void Lcd5110::ResetLcd()
shower_xu 0:ab1ca9a3e847 49 {
shower_xu 0:ab1ca9a3e847 50 Pins[PIN_RST]->write(0); // Reset goes low
shower_xu 0:ab1ca9a3e847 51 wait(0.01);
shower_xu 0:ab1ca9a3e847 52 Pins[PIN_RST]->write(1); // Reset goes high
shower_xu 0:ab1ca9a3e847 53 }
shower_xu 0:ab1ca9a3e847 54
shower_xu 0:ab1ca9a3e847 55 void Lcd5110::SendCmd(char cmd)
shower_xu 0:ab1ca9a3e847 56 {
shower_xu 0:ab1ca9a3e847 57 Pins[PIN_SCE]->write(0); // CE goes low
shower_xu 0:ab1ca9a3e847 58 Pins[PIN_DC]->write(0); // Data/CMD goes low
shower_xu 0:ab1ca9a3e847 59 #ifdef HW_SPI
shower_xu 0:ab1ca9a3e847 60 LcdSpi->write(cmd); // Command gets sent
shower_xu 0:ab1ca9a3e847 61 #else
shower_xu 0:ab1ca9a3e847 62 for(unsigned char i=0;i<8;i++)
shower_xu 0:ab1ca9a3e847 63 {
shower_xu 0:ab1ca9a3e847 64 if(cmd&0x80)
shower_xu 0:ab1ca9a3e847 65 SPins[PIN_MOSI]->write(1);
shower_xu 0:ab1ca9a3e847 66 else
shower_xu 0:ab1ca9a3e847 67 SPins[PIN_MOSI]->write(0);
shower_xu 0:ab1ca9a3e847 68 SPins[PIN_SCLK]->write(0);
shower_xu 0:ab1ca9a3e847 69 cmd=cmd<<1;
shower_xu 0:ab1ca9a3e847 70 SPins[PIN_SCLK]->write(1);
shower_xu 0:ab1ca9a3e847 71 }
shower_xu 0:ab1ca9a3e847 72 #endif
shower_xu 0:ab1ca9a3e847 73 Pins[PIN_DC]->write(1); // Data/CMD goes back to Data mode
shower_xu 0:ab1ca9a3e847 74 Pins[PIN_SCE]->write(1); // CE goes high
shower_xu 0:ab1ca9a3e847 75 }
shower_xu 0:ab1ca9a3e847 76
shower_xu 0:ab1ca9a3e847 77 void Lcd5110::SendData(char data)
shower_xu 0:ab1ca9a3e847 78 {
shower_xu 0:ab1ca9a3e847 79 Pins[PIN_SCE]->write(0); // CE goes low
shower_xu 0:ab1ca9a3e847 80 Pins[PIN_DC]->write(1); // Data/CMD goes low
shower_xu 0:ab1ca9a3e847 81 #ifdef HW_SPI
shower_xu 0:ab1ca9a3e847 82 LcdSpi->write(data); // Command gets sent
shower_xu 0:ab1ca9a3e847 83 #else
shower_xu 0:ab1ca9a3e847 84 for(unsigned char i=0;i<8;i++)
shower_xu 0:ab1ca9a3e847 85 {
shower_xu 0:ab1ca9a3e847 86 if(data&0x80)
shower_xu 0:ab1ca9a3e847 87 SPins[PIN_MOSI]->write(1);
shower_xu 0:ab1ca9a3e847 88 else
shower_xu 0:ab1ca9a3e847 89 SPins[PIN_MOSI]->write(0);
shower_xu 0:ab1ca9a3e847 90 SPins[PIN_SCLK]->write(0);
shower_xu 0:ab1ca9a3e847 91 data=data<<1;
shower_xu 0:ab1ca9a3e847 92 SPins[PIN_SCLK]->write(1);
shower_xu 0:ab1ca9a3e847 93 }
shower_xu 0:ab1ca9a3e847 94 #endif
shower_xu 0:ab1ca9a3e847 95 Pins[PIN_SCE]->write(1); // CE goes high
shower_xu 0:ab1ca9a3e847 96 }
shower_xu 0:ab1ca9a3e847 97
shower_xu 0:ab1ca9a3e847 98 void Lcd5110::clear()
shower_xu 0:ab1ca9a3e847 99 {
shower_xu 0:ab1ca9a3e847 100 unsigned char i,j;
shower_xu 0:ab1ca9a3e847 101 SendCmd(0x0c);//设定显示模式,正常显示
shower_xu 0:ab1ca9a3e847 102 SendCmd(0x80);//设置RAM起始地址
shower_xu 0:ab1ca9a3e847 103 for(j=0;j<LCD_Y_MAX;j++)
shower_xu 0:ab1ca9a3e847 104 {
shower_xu 0:ab1ca9a3e847 105 for(i=0;i<LCD_X_MAX;i++)
shower_xu 0:ab1ca9a3e847 106 {
shower_xu 0:ab1ca9a3e847 107 SendData(0);
shower_xu 0:ab1ca9a3e847 108 }
shower_xu 0:ab1ca9a3e847 109 }
shower_xu 0:ab1ca9a3e847 110 }
shower_xu 0:ab1ca9a3e847 111
shower_xu 0:ab1ca9a3e847 112 void Lcd5110::TestLcd(char test_pattern)
shower_xu 0:ab1ca9a3e847 113 {
shower_xu 0:ab1ca9a3e847 114 SendCmd(0x0c);//设定显示模式,正常显示
shower_xu 0:ab1ca9a3e847 115 SendCmd(0x80);//设置RAM起始地址
shower_xu 0:ab1ca9a3e847 116 for(int tick = 0; tick < (LCD_Y_MAX*LCD_X_MAX); tick++)
shower_xu 0:ab1ca9a3e847 117 {
shower_xu 0:ab1ca9a3e847 118 SendData(test_pattern); // Command gets sent
shower_xu 0:ab1ca9a3e847 119 wait(0.005);
shower_xu 0:ab1ca9a3e847 120 }
shower_xu 0:ab1ca9a3e847 121 }
shower_xu 0:ab1ca9a3e847 122
shower_xu 0:ab1ca9a3e847 123 /********************************************
shower_xu 0:ab1ca9a3e847 124 set_xy
shower_xu 0:ab1ca9a3e847 125 *********************************************/
shower_xu 0:ab1ca9a3e847 126 void Lcd5110::set_xy(unsigned char x,unsigned char y)
shower_xu 0:ab1ca9a3e847 127 {
shower_xu 0:ab1ca9a3e847 128 SendCmd(0x20);//H=0
shower_xu 0:ab1ca9a3e847 129 SendCmd(0x80|x);//x-0 to 83
shower_xu 0:ab1ca9a3e847 130 SendCmd(0x40|y);//y-0 to 5
shower_xu 0:ab1ca9a3e847 131 }
shower_xu 0:ab1ca9a3e847 132
shower_xu 0:ab1ca9a3e847 133 /*********************************************
shower_xu 0:ab1ca9a3e847 134 display a asciifont6*8
shower_xu 0:ab1ca9a3e847 135 *********************************************/
shower_xu 0:ab1ca9a3e847 136 void Lcd5110::write_char(char c)
shower_xu 0:ab1ca9a3e847 137 {
shower_xu 0:ab1ca9a3e847 138 unsigned char line;
shower_xu 0:ab1ca9a3e847 139 c-=32;
shower_xu 0:ab1ca9a3e847 140 for(line=0;line<LCD_Y_MAX;line++)
shower_xu 0:ab1ca9a3e847 141 SendData(ASCII[c][line]);
shower_xu 0:ab1ca9a3e847 142 }
shower_xu 0:ab1ca9a3e847 143
shower_xu 0:ab1ca9a3e847 144 /*********************************************
shower_xu 0:ab1ca9a3e847 145 英文字符串显示函数
shower_xu 0:ab1ca9a3e847 146 **********************************************/
shower_xu 0:ab1ca9a3e847 147 void Lcd5110::write_stringxy(unsigned char x,unsigned char y,char *p)
shower_xu 0:ab1ca9a3e847 148 {
shower_xu 0:ab1ca9a3e847 149 set_xy(x,y);
shower_xu 0:ab1ca9a3e847 150 while(*p)
shower_xu 0:ab1ca9a3e847 151 {
shower_xu 0:ab1ca9a3e847 152 write_char(*p);
shower_xu 0:ab1ca9a3e847 153 p++;
shower_xu 0:ab1ca9a3e847 154 }
shower_xu 0:ab1ca9a3e847 155 }
shower_xu 0:ab1ca9a3e847 156
shower_xu 0:ab1ca9a3e847 157 /*********************************************
shower_xu 0:ab1ca9a3e847 158 英文字符串显示函数
shower_xu 0:ab1ca9a3e847 159 **********************************************/
shower_xu 0:ab1ca9a3e847 160 void Lcd5110::write_string(char *p)
shower_xu 0:ab1ca9a3e847 161 {
shower_xu 0:ab1ca9a3e847 162 while(*p)
shower_xu 0:ab1ca9a3e847 163 {
shower_xu 0:ab1ca9a3e847 164 write_char(*p);
shower_xu 0:ab1ca9a3e847 165 p++;
shower_xu 0:ab1ca9a3e847 166 }
shower_xu 0:ab1ca9a3e847 167 }
shower_xu 0:ab1ca9a3e847 168 /*
shower_xu 0:ab1ca9a3e847 169 转换数字到字符串
shower_xu 0:ab1ca9a3e847 170 */
shower_xu 0:ab1ca9a3e847 171 char* Lcd5110::NumToStr(int num)
shower_xu 0:ab1ca9a3e847 172 {
shower_xu 0:ab1ca9a3e847 173 if(num <= 0)
shower_xu 0:ab1ca9a3e847 174 return "0";
shower_xu 0:ab1ca9a3e847 175
shower_xu 0:ab1ca9a3e847 176 double length = 0;
shower_xu 0:ab1ca9a3e847 177 int tlen = 0;
shower_xu 0:ab1ca9a3e847 178 int temp = 1;
shower_xu 0:ab1ca9a3e847 179 char c;
shower_xu 0:ab1ca9a3e847 180
shower_xu 0:ab1ca9a3e847 181 // Get number of digits
shower_xu 0:ab1ca9a3e847 182 while( temp <= num )
shower_xu 0:ab1ca9a3e847 183 {
shower_xu 0:ab1ca9a3e847 184 temp *= 10;
shower_xu 0:ab1ca9a3e847 185 length++;
shower_xu 0:ab1ca9a3e847 186 }
shower_xu 0:ab1ca9a3e847 187 tlen = length;
shower_xu 0:ab1ca9a3e847 188 char* numString = new char[tlen+1];
shower_xu 0:ab1ca9a3e847 189
shower_xu 0:ab1ca9a3e847 190 // Convert each place in number to a stand-alone representative number
shower_xu 0:ab1ca9a3e847 191 temp = 0;
shower_xu 0:ab1ca9a3e847 192 for(int idx = pow(10, length); idx>1; idx = (idx/10))
shower_xu 0:ab1ca9a3e847 193 {
shower_xu 0:ab1ca9a3e847 194 c = (char)( ((num % idx)-(num % (idx/10)))/(idx/10) + 48);
shower_xu 0:ab1ca9a3e847 195 numString[temp] = c;
shower_xu 0:ab1ca9a3e847 196 temp++;
shower_xu 0:ab1ca9a3e847 197 }
shower_xu 0:ab1ca9a3e847 198 numString[temp] = '\0';
shower_xu 0:ab1ca9a3e847 199 return numString;
shower_xu 0:ab1ca9a3e847 200 }
shower_xu 0:ab1ca9a3e847 201
shower_xu 0:ab1ca9a3e847 202 /*
shower_xu 0:ab1ca9a3e847 203 关闭5110
shower_xu 0:ab1ca9a3e847 204 */
shower_xu 0:ab1ca9a3e847 205 void Lcd5110::ShutdownLcd()
shower_xu 0:ab1ca9a3e847 206 {
shower_xu 0:ab1ca9a3e847 207 clear();
shower_xu 0:ab1ca9a3e847 208 SendCmd( 0x08 );
shower_xu 0:ab1ca9a3e847 209 SendCmd( 0x25 );
shower_xu 0:ab1ca9a3e847 210 }
shower_xu 0:ab1ca9a3e847 211
shower_xu 0:ab1ca9a3e847 212 Lcd5110::~Lcd5110()
shower_xu 0:ab1ca9a3e847 213 {
shower_xu 0:ab1ca9a3e847 214 ShutdownLcd();
shower_xu 0:ab1ca9a3e847 215 }