Simple library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website). - koristen softverski SPI umjesto hardverskog
Fork of N5110 by
N5110.cpp
00001 /** 00002 @file N5110.cpp 00003 00004 @brief Member functions implementations 00005 00006 */ 00007 #include "mbed.h" 00008 #include "N5110.h" 00009 #include "SWSPI.h" 00010 00011 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) 00012 { 00013 00014 //spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise 00015 spi = new SWSPI(mosiPin,PTC0,sclkPin); // create new SPI instance and initialise 00016 00017 initSPI(); 00018 00019 // set up pins as required 00020 led = new PwmOut(ledPin); 00021 pwr = new DigitalOut(pwrPin); 00022 sce = new DigitalOut(scePin); 00023 rst = new DigitalOut(rstPin); 00024 dc = new DigitalOut(dcPin); 00025 00026 } 00027 00028 // initialise function - powers up and sends the initialisation commands 00029 void N5110::init() 00030 { 00031 turnOn(); // power up 00032 wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset 00033 reset(); // reset LCD - must be done within 100 ms 00034 00035 // function set - extended 00036 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); 00037 // Don't completely understand these parameters - they seem to work as they are 00038 // Consult the datasheet if you need to change them 00039 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library 00040 sendCommand(CMD_TC_TEMP_2); // temperature control 00041 sendCommand(CMD_BI_MUX_48); // bias 00042 00043 // function set - basic 00044 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); 00045 normalMode(); // normal video mode by default 00046 sendCommand(CMD_DC_NORMAL_MODE); // black on white 00047 00048 // RAM is undefined at power-up so clear 00049 clearRAM(); 00050 00051 } 00052 00053 // sets normal video mode (black on white) 00054 void N5110::normalMode() { 00055 sendCommand(CMD_DC_NORMAL_MODE); 00056 00057 } 00058 00059 // sets normal video mode (white on black) 00060 void N5110::inverseMode() { 00061 sendCommand(CMD_DC_INVERT_VIDEO); 00062 } 00063 00064 // function to power up the LCD and backlight 00065 void N5110::turnOn() 00066 { 00067 // set brightness of LED - 0.0 to 1.0 - default is 50% 00068 setBrightness(0.5); 00069 pwr->write(1); // apply power 00070 } 00071 00072 // function to power down LCD 00073 void N5110::turnOff() 00074 { 00075 setBrightness(0.0); // turn backlight off 00076 clearRAM(); // clear RAM to ensure specified current consumption 00077 // send command to ensure we are in basic model 00078 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); 00079 // clear the display 00080 sendCommand(CMD_DC_CLEAR_DISPLAY); 00081 // enter the extended mode and power down 00082 sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); 00083 // small delay and then turn off the power pin 00084 wait_ms(10); 00085 pwr->write(0); 00086 00087 } 00088 00089 // function to change LED backlight brightness 00090 void N5110::setBrightness(float brightness) 00091 { 00092 // check whether brightness is within range 00093 if (brightness < 0.0) 00094 brightness = 0.0; 00095 if (brightness > 1.0) 00096 brightness = 1.0; 00097 // set PWM duty cycle 00098 led->write(brightness); 00099 } 00100 00101 00102 // pulse the active low reset line 00103 void N5110::reset() 00104 { 00105 rst->write(0); // reset the LCD 00106 rst->write(1); 00107 } 00108 00109 // function to initialise SPI peripheral 00110 void N5110::initSPI() 00111 { 00112 //spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge 00113 spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge 00114 00115 spi->frequency(4000000); // maximum of screen is 4 MHz 00116 } 00117 00118 // send a command to the display 00119 void N5110::sendCommand(unsigned char command) 00120 { 00121 dc->write(0); // set DC low for command 00122 sce->write(0); // set CE low to begin frame 00123 spi->write(command); // send command 00124 dc->write(1); // turn back to data by default 00125 sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00126 00127 } 00128 00129 // send data to the display at the current XY address 00130 // dc is set to 1 (i.e. data) after sending a command and so should 00131 // be the default mode. 00132 void N5110::sendData(unsigned char data) 00133 { 00134 sce->write(0); // set CE low to begin frame 00135 spi->write(data); 00136 sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00137 } 00138 00139 // this function writes 0 to the 504 bytes to clear the RAM 00140 void N5110::clearRAM() 00141 { 00142 int i; 00143 sce->write(0); //set CE low to begin frame 00144 for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes 00145 spi->write(0x00); // send 0's 00146 } 00147 sce->write(1); // set CE high to end frame 00148 00149 } 00150 00151 // function to set the XY address in RAM for subsequenct data write 00152 void N5110::setXYAddress(int x, int y) 00153 { 00154 // check whether address is in range 00155 if (x > 83) 00156 x=83; 00157 if (y > 5) 00158 y=5; 00159 if (x < 0) 00160 x=0; 00161 if (y < 0) 00162 y=0; 00163 00164 sendCommand(0x80 | x); // send addresses to display with relevant mask 00165 sendCommand(0x40 | y); 00166 } 00167 00168 // These functions are used to set, clear and get the value of pixels in the display 00169 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() 00170 // function must be called after set and clear in order to update the display 00171 void N5110::setPixel(int x, int y) 00172 { 00173 // calculate bank and shift 1 to required position in the data byte 00174 buffer[x][y/8] |= (1 << y%8); 00175 } 00176 00177 void N5110::clearPixel(int x, int y) 00178 { 00179 // calculate bank and shift 1 to required position (using bit clear) 00180 buffer[x][y/8] &= ~(1 << y%8); 00181 } 00182 00183 unsigned char N5110::getPixel(int x, int y) 00184 { 00185 // return relevant bank and mask required bit 00186 return buffer[x][y/8] & (1 << y%8); 00187 00188 } 00189 00190 // function to refresh the display 00191 void N5110::refresh() 00192 { 00193 int i,j; 00194 sce->write(0); //set CE low to begin frame 00195 00196 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00197 for(i = 0; i < 84; i++) { 00198 spi->write(buffer[i][j]); // send buffer 00199 } 00200 } 00201 sce->write(1); // set CE high to end frame 00202 00203 } 00204 00205 // fills the buffer with random bytes. Can be used to test the display. 00206 // The rand() function isn't seeded so it probably creates the same pattern everytime 00207 void N5110::randomiseBuffer() 00208 { 00209 int i,j; 00210 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00211 for(i = 0; i < 84; i++) { 00212 buffer[i][j] = rand()%256; // generate random byte 00213 } 00214 } 00215 00216 } 00217 00218 // function to print 5x7 font 00219 void N5110::printChar(char c) 00220 { 00221 int i; 00222 // loop through 5 columns 00223 for (i = 0; i < 5 ; i++ ) { 00224 sendData(font5x7[(c - 32)*5 + i]); 00225 // array is offset by 32 relative to ASCII, each character is 5 pixels wide 00226 // the X address is automatically incremented after each data write 00227 } 00228 sendData(0); // send an empty byte to introduce space between characters 00229 00230 } 00231 00232 // function to print string at specified position 00233 void N5110::printString(const char * str,int x,int y) 00234 { 00235 int n = 0 ; // counter for number of characters in string 00236 // loop through string and print character 00237 while(*str) { 00238 00239 setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character 00240 printChar(*str); // print the char - can probably so *str++ and remove next line 00241 str++; // go to next character in string 00242 n++; // increment index 00243 } 00244 00245 } 00246 00247 // function to clear the screen 00248 void N5110::clear() 00249 { 00250 clearBuffer(); // clear the buffer then call the refresh function 00251 refresh(); 00252 } 00253 00254 // function to clear the buffer 00255 void N5110::clearBuffer() 00256 { 00257 int i,j; 00258 for (i=0; i<84; i++) { // loop through the banks and set the buffer to 0 00259 for (j=0; j<6; j++) { 00260 buffer[i][j]=0; 00261 } 00262 } 00263 }
Generated on Wed Jul 20 2022 04:01:43 by 1.7.2